PHP Example Code
Created Thursday 23 May 2019
Math Operations:
<html> <head> <title>Mathops with Variables</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>
IF
<?php $value = 14; if ($value < 12 ) { echo "the value is LESS than 12"; } else { echo "it's MORE than or equal to 12"; } echo "<br>and we're done"; ?>
IF with ELSE
<?php //Notice the lack of { } for one-line blocks of if/else code. $age = 2; $playground_min_age = 5; $playground_max_age = 12; if ($age < $playground_min_age) echo "You are too young to play on the playground. Go away!"; elseif ($age > $playground_max_age) echo "You are too old to play on the playground. Weirdo."; else echo "You can come play on the playground"; ?>
FOR
<?php $count = 15; for ($i = 0; $i < $count; $i++) { echo "FOR STYLE! We are on $i in this loop!<br>"; } ?>