PHP Examples
Created Friday 14 April 2017
MATH OPERATIONS
<!DOCTYPE html> <head> <title> Mathops </title> </head> <body> <?php $x = 20; $y = 3; $add = $x + $y; $sub = $x - $y; $mult = $x * $y; $div = $x / $y; $mod = $x % $y; echo "Stuff we can do with VARIABLES. <br>"; echo "x is $x, y is $y<br>"; echo "add ".$add; echo "<br>"; echo "subtract ".$sub; echo "<br>"; echo "multiply ".$mult; echo "<br>"; echo "divide ".$div; echo "<br>"; echo "modulus ".$mod; echo "<br>"; ?> </body> </html>
CONDITIONALS
IF
<!DOCTYPE html>
<head>
<title> CONDITIONALS </title>
</head>
<body>
<?php
$value = 12;
if ( $value < 12 )
{
echo "Value is less than 12!!!";
}
else
{
echo "Value is not less than 12.";
}
?>
</body>
</html>
IF/ELSE
<!DOCTYPE html> <head> <title> CONDITIONALS </title> </head> <body> <?php $age = 65; $playground_minimum_age = 5; $playground_maximum_age = 13; if ($age < $playground_minimum_age) echo "You are too young for the playground. Go away."; elseif ($age > $playground_maximum_age) echo "You are too old. Weirdo."; else echo "Come on in!"; ?> </body> </html>
WHILE
<!DOCTYPE html>
<head>
<title> CONDITIONALS </title>
</head>
<body>
<?php
$count = 10;
while ($count < 15)
{
echo "<br> Count is now $count </br";
$count++;
}
?>
</body>
</html>
FOR
<!DOCTYPE html>
<head>
<title> CONDITIONALS </title>
</head>
<body>
<?php
echo "<ol>";
for ($i = 1; $i <= 15; $i++)
{
echo "<li> We are LOOPING! on count $i </li>";
}
echo "</ol>";
?>
</body>
</html>
Backlinks: FSU Courses:LIS5364 FSU Courses:LIS5367