PHP If…Else Statements

Conditional statements are used to perform different actions based on different conditions. Use the if....else statement to execute some code if a condition is true and another code if a condition is false.
Syntax
1
2
3
4
 if (condition)
  code to be executed if condition is true;
else
  code to be executed if condition is false;
The following example will output "Get ready for the week!" if the current day is Monday, otherwise it will output "Have a good day!":
1
2
3
4
5
6
7
8
9
10
11
<html>
<body>
<?php
$d=date("D");
if ($d=="Mon")
  echo " Get ready for the week!";
else
  echo "Have a good day!";
?>
</body>
</html>
Use the if....elseif...else statement to select one of several blocks of code to be executed.
Syntax
1
2
3
4
5
6
7
8
9
10
11
if (condition)
 
  code to be executed if condition is true;
 
elseif (condition)
 
  code to be executed if condition is true;
 
else
 
  code to be executed if condition is false;
Basic PHP Syntax
PHP Arrays
PHP For Loops
PHP Forms and User Input