CS 334

Course Notes:


Chapter Three:

Conditions & Functions:

You will find that the conditional statements and functions in PHP look very similar to those you find in JavaScript.

Creating a Random Number with the random function:

<html>
<head>
<title>Roll Em!</title>
</head>
<body>
<h1>Roll Em!</h1>
<h3>Demonstrates rolling a die</h3>

<?php

$roll=0;
$roll = rand(1,6);
print "You rolled a $roll";
print "<br>";
print "<img src = die$roll.jpg>";
?>
<br>
Refresh this page in the browser to roll another die.

</body>
</html>

Conditional Statements: Remeber the "if" statement?

<html>
<head>
<title>Ace!</title>
</head>
<body>
<h1>Ace!</h1>
<h3>Demonstrates if statement</h3>

<?php

$roll=0;
$roll = rand(1,6);
print "You rolled a $roll";

if ($roll == 1){
print "<h1>That's an ace!!!!!</h1>";
} // end if

print "<br>";
print "<img src = die$roll.jpg>";
?>
<br>
Refresh this page in the browser to roll another die.

</body>
</html>

Comparison Operators:

== IS equal to?
< less than
> greater than
<= less than or equal to
>= greater than or equal to
!= Is NOT equal too?

if else statement - look familiar?

<html>
<head>
<title>Binary Dice</title>
</head>
<body>
<h1>Binary Dice</h1>
<h3>Demonstrates multiple if structure</h3>

<?php
$roll = rand(1,6);
print "You rolled a $roll";
print "<br>";

if ($roll == 1){
$binValue = "001";
} else if ($roll == 2){
$binValue = "010";
} else if ($roll == 3){
$binValue = "011";
} else if ($roll == 4){
$binValue = "100";
} else if ($roll == 5){
$binValue = "101";
} else if ($roll == 6){
$binValue = "110";
} else {
print "I don't know that one...";
} // end if

print "<br>";
print "<img src = die$roll.jpg>";
print "<br>";
print "In binary, that's $binValue";
print "<br>";
print "<br>";
print "<br>";

?>
<br>
Refresh this page in the browser to roll another die.

</body>
</html>

Switch Statement

<html>
<head>
<title>Switch Dice</title>
</head>
<body>
<h1>SwitchDice</h1>
<h3>Demonstrates switch structure</h3>

<?php
$roll = rand(1,6);
print "You rolled a $roll";
print "<br>";

switch ($roll){
case 1:
$romValue = "I";
break;
case 2:
$romValue = "II";
break;
case 3:
$romValue = "III";
break;
case 4:
$romValue = "IV";
break;
case 5:
$romValue = "V";
break;
case 6:
$romValue = "VI";
break;
default:
print "This is an illegal die!";
} // end switch

print "<br>";
print "<img src = die$roll.jpg>";
print "<br>";
print "In Roman numerals, that's $romValue";
print "<br>";
print "<br>";
print "<br>";

?>
<br>
Refresh this page in the browser to roll another die.

</body>
</html>

if I had an empty() funtion - Do you want Fries with that?

THE HTML FILE:

<html>
<head>
<title>Checkbox Demo</title>
</head>
<body>
<h1>Checkbox Demo</h1>

<h3>Demonstrates checkboxes</h3>

<form action = "checkDemo.php" method="post" >

<h3>What would you like with your order?</h3>
<ul>
<li><input type = "checkbox" name = "chkFries" value = "1.00">Fries
</li>
<li><input type = "checkbox" name = "chkSoda" value = ".85">Soda
</li>
<li><input type = "checkbox" name = "chkShake" value = "1.30">Shake
</li>
<li><input type = "checkbox" name = "chkKetchup" value = ".05">Ketchup
</li>

</ul>

<input type = "submit">
</form>

</body>
</html>

THE PHP FILE:

<html>
<head>
<title> Check Box </title>

<body>
<h3> Reading Checkboxes </h3>

<?php
$chkFries = $_REQUEST["chkFries"];
$chkSoda = $_REQUEST["chkSoda"];
$chkShake = $_REQUEST["chkShake"];
$chkKetchup = $_REQUEST["chkKetchup"];

print "chkFries:$chkFries<br>";

print "chkSoda:$chkSoda<br>";

print "chkShake:$chkShake<br>";

print "chkKetchup:$chkKetchup<br>";

$total = 0;

if(!empty($chkFries)){
print("You chose Fries <br/>");
$total = $total + $chkFries;
} // end if

if(!empty($chkSoda)){
print("You chose Soda <br/>");
$total = $total + $chkSoda;
} // end if

if(!empty($chkShake)){
print("You chose Shake <br/>");
$total = $total + $chkShake;
} // end if

if(!empty($chkKetchup)){
print("You chose Fries <br/>");
$total = $total + $chkKetchup;
} // end if

print "The total cost is $total";

?>

</body>
</html>