IF-ELSE In PHP

The PHP If Statement
 
The if statement is necessary for most programming, thus it is important in PHP. Imagine that on January 1st you want to print out “Happy New Year!” at the top of your personal web page. With the use of PHP if statements you could have this process automated, months in advance, occurring every year on January 1st.
 
This idea of planning for future events is something you would never have had the opportunity of doing if you had just stuck with HTML.
 
If Statement Example
 
IF (conditional statement) { 
  [code if condition is true] 
}
 
The PHP if statement tests to see if a value is true, and if it is a segment of code will be executed. See the example below for the form of a PHP if statement.
 
PHP Code:
 
<?php
$my_name = "phpguy";

if ( $my_name == "phpguy" ) {
	echo "Your name is phpguy!<br />";
}
echo "Welcome to eBIZ Education!";
?>
 
Display:
 
Your name is phpguy!
Welcome to eBIZ Education!
 
Did you get that we were comparing the variable $my_name with “phpguy” to see if they were equal? In PHP you use the double equal sign (==) to compare values. In addition, notice that because the if statement turned out to be true, the code segment was executed, printing out “Your name is phpguy!”.
 
Let’s go a bit more in-depth into this example to iron out the details.
 
• We first set the variable $my_name equal to “phpguy”.

• We next used a PHP if statement to check if the value contained in the variable $my_name was equal to “phpguy”

• The comparison between $my_name and “phpguy” was done with a double equal sign “==”, not a single equals”=”! A single equals is for assigning a value to a variable, while a double equals is for checking if things are equal.

• Translated into english the PHP statement ( $my_name == “phpguy” ) is ( $my_name is equal to “phpguy” ).

• $my_name is indeed equal to “phpguy” so the echo statement is executed.

 
A False If Statement
 
Let us now see what happens when a PHP if statement is not true, in other words, false. Say that we changed the above example to:
 
PHP Code:
 
<?php
$my_name = "LAMPGuy";

if ( $my_name == "phpguy" ) {
	echo "Your name is phpguy!<br />";
}
echo "Welcome to eBIZ Education!";
?>

 
Display:
 
Welcome to eBIZ Education!
 
Here the variable contained the value “LAMPGuy”, which is not equal to “phpguy”. The if statement evaluated to false, so the code segment of the if statement was not executed. When used properly, the if statement is a powerful tool to have in your programming arsenal!
 
IF.. ELSE statement
 
IF..ELSE is used in PHP to provide conditional judgements.
 
The basic syntax is as follows:
 
IF (conditional statement) { 
  [code if condition is true] 
} 
ELSE { 
  [code if condition is false] 
} 
 
Let’s see an example. Assuming we have the following piece of code:
 
$num = 7; 
IF ($num > 5) { 
  print "Number is greater than 5"; 
} 
ELSE { 
  print "Number is less than 5"; 
} 
 
The output of the above code is:
 
Number is greater than 5
 
This is because the condition, ($num > 5), is true. Therefore, the code in the bracket after IF is executed