PHP examples: conditionals and variables.

First example of conditionals


<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<!--
Changing background depending on temperature
Author: Elena Machkasova elenam@morris.umn.edu
Last modified: 9/27/06
-->
<?php
$temp = $_GET["temp"];
if ($temp < 30) {
        $color = "#ADD8E6"; // lightblue 
}
else {
        $color = "#90EE90";  // lightgreen
}
?>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<title>
The background depends on temperature
</title>

</head>

<body style="background: <? print $color; ?>">
<p>The current temperature is <? print $temp?;>F.
<?php
if ($temp < 0) {
        print "<strong>Brrr...</strong>";
}
?>

</p>

</body>
</html>

http://csci1101sp09.morris.umn.edu/~elenam/1101_spring09/ifelse/background.php?temp=47

Different types of PHP data



<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<!--
Examples of PHP variables and their use
Author: Elena Machkasova elenam@morris.umn.edu
Last modified: 2/13/06
-->

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<title>
Examples of PHP variables and their use
</title>

</head>
<body>

<h2>This page tests PHP variables and prints a bunch of nonsense</h2>

<p>
<?php
// integers and what you can do with them
$n = 2;
$m = 3;
$sum = $n + $m;

// variables are a part of the string that gets printed
print "$n + $m = $sum<br/>\n";

$prod = $n * $m;
$div = $n / $m;
$remainder = $n % $m;
print "$n * $m = $prod<br/>\n $n / $m = $div<br/>\n $n % $m = $remainder<br/>\n"
;

// changing $n:
$n = $m + 1;
// using \ to print $
print "\$n = $n<br/>\n";

// changing $n using $n:
$n = 2 * $n;
print "\$n = $n<br/>\n";

// adding 1 to n:
$n++;
print "\$n = $n<br/>\n";

// comparing integers
if ($n == $m) {
        print "\$n and \$m are the same<br/>\n";
} else {
        print "\$n and \$m are not the same<br/>\n";
}

// float type and what you can do with it
$x = 3.14;
$y = 2e-2;

print "\$x = $x, \$y = $y<br/>\n";
$sum = $x + $y; // $sum was used for an integer, now is used for a float
$div = $x / $y;
print "$x + $y = $sum<br/>\n $x / $y = $div<br/>\n";

// adding an integer and a float gives you a float
$sum = $x + $m;
print "$x + $m = $sum<br/>\n";

// strings and what you can do with them
$greeting1 = "Hello";
$greeting2 = "Hi there!";
$monkeys = "5 little monkeys";
$digits = "1234"; // this is a string, not a number

print $greeting2;
print "<br/>\n";

// string concatenation - glue strings together!
$new_string = $greeting1.", ".$monkeys."<br/>\n";
print $new_string;

// strange things you can do with strings:
$sum = $m + $monkeys; // since $monkeys starts with 5, it's added as 5
print "$sum<br/>\n";
$sum = $m + $greeting1; // $greeting1 doesn't start with a number, added as 0
print "$sum<br/>\n";

// booleans: true and false. They are used in conditions
$passed_test = false;
if ($passed_test) {
        print "Congratulations! You passed the test.<br/>\n";
} else {
        print "Sorry, you didn't pass the test.<br/>\n";
}

// ! means "not"
if (!$passed_test) {
        print "You need to take the test again<br/>\n";
}

$homework_grades = 83;
// && means AND: both conditions must be true
if ($passed_test && ($homework_grades > 60)) {
        print "You passed the course<br/>\n";
} else {
        print "Sorry, you didn't pass the course<br/>\n";
}

print "Let's try it again<br/>\n";
// || means OR: one true condition is enough
if ($passed_test || ($homework_grades > 60)) {
        print "You passed the course<br/>\n";
} else {
        print "Sorry, you didn't pass the course<br/>\n";
}

// changing types (type casting)
$howmany = (int) $monkeys;
print "\$howmany = $howmany<br/>\n";
$trueorfalse = (bool) $howmany;
print "\$trueorfalse = $trueorfalse<br/>\n";

// unsetting variables and checking whether they are set
if (isset($howmany)) print "\$howmany is set<br/>\n";

if (isset($banana)) print "\$banana is set<br/>\n";

unset($howmany);

if (isset($howmany)) print "\$howmany is set<br/>\n";
?>

</p>
</body>
</html>

http://csci1101sp09.morris.umn.edu/~elenam/1101_spring09/ifelse/variables.php

Cascading if/else


<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<!--
Cascading if/else
Author: Elena Machkasova elenam@morris.umn.edu
Last modified: 2/13/06
-->
<?php
$hour = $_GET["input"];
?>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<title>
Greetings
</title>
</head>

<body>

<h2>
<?php
if ($hour < 9) {
	print "Good night!";	
} elseif ($hour < 12) {
	print "Good morning!";	
} elseif ($hour < 18) {
	print "Good day!";	
} else {
	print "Good evening!";	
}
?>
</h2>
</body>

</html>


http://csci1101sp09.morris.umn.edu/~elenam/1101_spring09/ifelse/hours.php?input=13

Exercise on conditionals


<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<!--
Exercise on if/else statements
Author: Elena Machkasova elenam@morris.umn.edu
Last modified: 9/27/06
-->

<?php
$day = $_GET["input"]; 
?>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<title>
Days of the week
</title>
</head>
<body>

<h2>
<?php
// Sally has a Computer Science class on Tuesdays and Thursdays
// and Biology on Mondays, Wednesdays, Fridays.
// She also has a biology lab on Thursdays.
// Assuming that $day is 0 for Sunday, 1 for Monday, etc.,
// make the page print what class she has on a given day, or,
// if it's a weekend, print "Have a nice weekend"

if ( $day == 2) {
        print "Today is Tuesday<br/>\n";
}
?>

</h2>
</body>
</html>

http://csci1101sp09.morris.umn.edu/~elenam/1101_spring09/ifelse/days.php?input=2

A switch statement


<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<!--
php switch statement
Author: Elena Machkasova elenam@morris.umn.edu
Last modified: 2/25/08
-->

<?php
$date = $_GET["input"]; 
?>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<title>
Days of the week
</title>

</head>
<body>

<h2>

<?php
switch ($date) {
	case 0: 
		$day = "Sunday";
		break;
	case 1: 
		$day = "Monday";
		break;
	case 2: 
		$day = "Tuesday";
		break;
	case 3:
		$day = "Wednesday";
		break;
	case 4:
		$day = "Thursday";
		break;
	case 5:
		$day = "Friday";
		break;
	case 6:
		$day = "Saturday";
		break;
	default: 
		$day = "no such day";	
}
print "Today is $day\n";
?>
</h2>
</body>
</html>

http://csci1101sp09.morris.umn.edu/~elenam/1101_spring09/ifelse/switch.php?input=4


CSci 1101 course web site.