Free Essay

Zcen

In: Computers and Technology

Submitted By zcen
Words 1002
Pages 5
Arrays
An array is a special kind of variable that contains multiple values.
Example:
$myarray = array('one', 2, 'three'); echo($myarray[0]); //output “one” echo($myarray[1]); //output “2” echo($myarray[2]); //output “three”

example 2.

$birthdays['Kevin'] = '1978-04-12';
$birthdays['Stephanie'] = '1980-05-16';
$birthdays['David'] = '1983-09-09'; echo('My birthday is: ' . $birthdays['Kevin']);

The PHP If Statement if (condition) code to be executed if condition is true;
Example1 (true statement) $my_name = "someguy"; if ( $my_name == "someguy" ) { echo "Your name is someguy!<br />"; } echo "Welcome to my homepage!";

Example2 (false statement)

$my_name = "anotherguy";

if ( $my_name == "someguy" ) { echo "Your name is someguy!<br />";
}
echo "Welcome to my homepage!";

Example calcIFrevised.html
<html>
<title>A simple math calculator</title>
<body>
Insert two numbers in the next form and hit submit button <br>
<form action="calcal.php" method="post">
Firstnumber: <input name="num1" type="text" /><br>
Secondnumber: <input name="num2" type="text" />
<input type="submit" />
</form>
</body>
</html>
Example calcIFrevised2.php
<?php
$num1 = $_POST['num1'];
$num2 = $_POST['num2'];
$a = $num1 + $num2;
$b = $num1 - $num2; echo ("The sum of the two numbers is ". $a ); echo ("<br>"); echo "The difference of the two numbers is ". $b; echo ("<br>"); if ( $a >= 10 ) { echo "above 10!<br />";
}
echo "Welcome to my homepage!";

?>
The if...else Statement if (condition) { code to be executed if condition is true; } else { code to be executed if condition is false; } example1 $number_three = 3;

if ( $number_three == 3 ) { echo "The if statement evaluated to true";
} else { echo "The if statement evaluated to false";
}

Example2 ifelse.php
<html>
<body>

<?php
$d=date("D");
if ($d=="Fri") { echo "Have a nice weekend!"; } else { echo "Have a nice day!"; }
?>

</body>
</html>
The if...elseif....else Statement

Syntax if (condition) { code to be executed if condition is true; } elseif (condition) { code to be executed if condition is true; } else { code to be executed if condition is false; } example $employee = "Bob"; if($employee == "Ms. Tanner"){ echo "Hello Ma'am";
} elseif($employee == "Bob"){ echo "Good Morning Sir!";
}else { echo "Morning";
}

example ifelseif.php
<html>
<body>

<?php
$d=date("D");
if ($d=="Fri") { echo "Have a nice weekend!"; } elseif ($d=="Sun") { echo "Have a nice Sunday!"; } else { echo "Have a nice day!"; }
?>

</body>
</html>

<Problem>

The PHP Switch Statement

Syntax

switch (n)
{
case label1: code to be executed if n=label1; break; case label2: code to be executed if n=label2; break; default: code to be executed if n is different from both label1 and label2;
}
example
$destination = "Tokyo"; echo "Traveling to $destination<br />"; switch ($destination){ case "Las Vegas": echo "Bring an extra $500"; break; case "Amsterdam": echo "Bring an open mind"; break; case "Egypt": echo "Bring 15 bottles of SPF 50 Sunscreen"; break; case "Tokyo": echo "Bring lots of money"; break; case "Caribbean Islands": echo "Bring a swimsuit"; break;
}

Example switch.php
<html>
<body>

<?php
$x=1;
switch ($x)
{
case 1: echo "Number 1"; break; case 2: echo "Number 2"; break; case 3: echo "Number 3"; break; default: echo "No number between 1 and 3";
}
?>

</body>
</html>
<Problem>
The while Loop

Syntax while (condition) { code to be executed; }

example while1.php
<html>
<body>

<?php
$i=1;
while($i<=5) { echo "The number is " . $i . "<br />"; $i++; }
?>

</body>
</html>
Example while2.php
<?php
$brush_price = 5;
$counter = 10;

echo "<table border=\"1\" align=\"center\">"; echo "<tr><th>Quantity</th>"; echo "<th>Price</th></tr>"; while ( $counter <= 100 ) { echo "<tr><td>"; echo $counter; echo "</td><td>"; echo $brush_price * $counter; echo "</td></tr>"; $counter = $counter + 10;
}
echo "</table>";
?>
Explanation: 1. We first made a $brush_price and $counter variable and set them equal to our desired values. 2. The table was set up with the beginning table tag and the table headers. 3. The while loop conditional statement was checked, and $counter (10) was indeed smaller or equal to 100. 4. The code inside the while loop was executed, creating a new table row for the price of 10 brushes. 5. We then added 10 to $counter to bring the value to 20. 6. The loop started over again at step 3, until $counter grew larger than 100. 7. After the loop had completed, we ended the table.

<Problem>
The do...while Statement

Syntax do { code to be executed; } while (condition); example dowhile1.php
<html>
<body>

<?php
$i=1;
do { $i++; echo "The number is " . $i . "<br />"; } while ($i<=5);
?>

</body>
</html>
The for Loop
Syntax
for (init; condition; increment) { code to be executed; }

example1 forloop1.php
<html>
<body>

<?php for ($i=1; $i<=5; $i++) { echo "The number is " . $i . "<br />"; }
?>

</body>
</html>
Example2 forloop2.php
$brush_price = 5;

echo "<table border=\"1\" align=\"center\">"; echo "<tr><th>Quantity</th>"; echo "<th>Price</th></tr>"; for ( $counter = 10; $counter <= 100; $counter += 10) { echo "<tr><td>"; echo $counter; echo "</td><td>"; echo $brush_price * $counter; echo "</td></tr>";
}
echo "</table>";

<Problem>
Connecting to Database:
<?php

//connecting to the database
$con = mysql_connect("localhost","root",""); if (!$con) { die('Could not connect: ' . mysql_error()); } exit(); // Select the address database
mysql_select_db("addressbook",$con);…...

Similar Documents