The if Statement

The if statement offers the ability to execute a block of code if the consition is TRUE; otherwise, the code block doesn't execute.

The condition can be any expression, including tests for nonzero, null, equality, variables, and returned values from functions.

if (conditional expression) {
  code to be executed if condition is true;
}

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="EN" lang="EN">
<head>
<title>PHP</title>
<meta Name="Author" Content="Hann So">
</head>
<body>
<p>
<?php
// compare a string
$wheather = "sunny";
if ($wheather == "sunny") {
   echo "Well today is a $wheather day.<br />";
}

// use comparison operators
$temperature = 75;
if ($temperature == 75) {
   echo "$temperature degrees today is good.<br />";
}

if ($temperature != 80) {
   echo "It's not 80 degrees today.<br />";
}

if ($temperature < 80) {
   echo "Good warm day.<br />";
}

// use logical operators
if ($temperature > 74) {
  if ($temperature < 80) {
   echo "It's a good comfort zone for the day.<br />";
  }
}

if ($temperature > 74 && $temperature < 80) {
   echo "We have a good temperature with $temperature degrees.<br />";
}
?>
</p>
</body>
</html>

View the effect


Conditional Tests | The if Statement | The else Statement | The elseif Statement | The switch Statement
© 2008: Hann So
email: hso@voyager.deanza.edu