PHP If…Else Statements

If…ElseĀ  statements are used to perform different actions based on different conditions.

Think about the decisions you make before you go to bed. If you have homework to do the next day, then you will have to finish before go to bed. Otherwise, you will read book or watch TV as you like!

In programming, whenever you want to make a decision given that something is true and be sure that you take the appropriate action, you are using an if..else relationship.

In PHP we have the following conditional statements:

  • if statement – use this statement to execute some code only if a specified condition is true
  • if…else statement – use this statement to execute some code if a condition is true and another code if the condition is false
  • if…elseif….else statement – use this statement to select one of several blocks of code to be executed
  • switch statement – use this statement to select one of many blocks of code to be executed

The if Statement Example:

$my_name = “vn4000″;

if ( $my_name == “vn4000″ ) {
echo “Hello vn4000!<br />”;
}
echo “Web programming!”;

The if…else Statement Example:

<?php
$my_name = “vn4000″;
if ($my_name == “vn4000″)
echo “Have a nice day, vn4000!”;
else
echo “Have a nice day everybody!”;
?>







Related Posts:

Comments are closed.