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: 2/13/06
-->
<?php
$celsius = -25.5;
if ($celsius < 0) {
	$color = "lightblue";
}
else {
	$color = "lightgreen";	
}
?>
<html>
<head>
<title>
The background depends on temperature
</title>
</head>
<body style="background: <? print $color; ?>">
<p>The current temperature is <? print $celsius?>C. 
<?php
if ($celsius < -25) {
	print "<strong>Brrr...</strong>";
}
?>
</p>
</body>
</html>

http://rynite.morris.umn.edu/~elenam/php_examples/ifelse/celsius.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>
<head>
<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://rynite.morris.umn.edu/~elenam/php_examples/ifelse/hours.php?input=13;

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>
<head>
<title>
Examples of PHP variables and their use
</title>
</head>
<body>
<h2>This page tests PHP variables and prints a bunch of nonsense</h2>
<?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";	
}
?>
</body>
</html>

http://rynite.morris.umn.edu/~elenam/php_examples/ifelse/variables.php

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: 2/13/06
-->
<?php
$date = $_GET["input"]; // Assuming February 2006
?>
<html>
<head>
<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
// Assuming that $date has the date in February 2006, 
// Make the page print what class she has on a given day, or,
// if it's a weekend, print "Have a nice weekend"

if ( ($date % 7) == 0) {
	print "Today is Tuesday<br/>\n";	
}
?>
</h2>
</body>
</html>

http://rynite.morris.umn.edu/~elenam/php_examples/ifelse/days.php?input=7;

The views and opinions expressed in this page are strictly those of the page author. The contents of this page have not been reviewed or approved by the University of Minnesota.