English
German

PHP If Else

If Else Conditional Statment

Has someone ever told you, "if you work hard, then you will succeed"? And what happens if you do not work hard? Well, you fail! This is an example of an if/else conditional statement.

    * If you work hard then you will succeed.
    * Else, if you do not work hard, then you will fail.



How does this translate into something useful for PHP developers? Well consider this:

Someone comes to your website and you want to ask this visitor her name if it is her first time coming to your site. With an if statement this is easy. Simply have a conditional statement to check, "are you visiting for the first time". If the condition is true, then take them to the "Insert Your Name" page, else let her view the website as normal because you have already asked her for her name in the past.that is know as if else conditions


If Else an Example

Using these conditional statements can add a new layers of "cool" to your website. Here's the basic form of an if/else statement in PHP.

Example:

$number_three = 3;

if ( $number_three == 3 ) {
    echo "The if statement evaluated to true, only for 3 value ";
} else {
    echo "The if statement evaluated to false";
}
 

Output
The if statement evaluated to true, only for 3 value

This is a lot to digest in one sitting, so let us step through the code, line by line.

    * We first made a PHP variable called $number_three and set it equal to 3.
    * In this example we compared a variable to an integer value. To do such a comparison we use "==", which in English means "Is Equal To".
    * $number_three is indeed Equal To 3 and so this statement will evaluate to true.
    * All code that is contained between the opening curly brace "{" that follows the if statement and the closing curly brace "}" will be executed when the if statement is true.
    * The code contained within the else segment will not used.
 


Execute Else Code with False

On the other hand, if the if statement was false, then the code contained in the else segment would have been executed. Note that the code within the if and else cannot both be executed, as the if statement cannot evaluate to both true and false at one time! Here is what would happen if we changed to $number_three to anything besides the number 3.

first of all condition will tested . if the condition will true the body of loop will be executed otherwise the else statement will be executed.

Example:

$number_three = 419;

if ( $number_three == 3 ) {
    echo "The if statement evaluated to true";
} else {
    echo "The if statement evaluated to false";
}
 

Output
The if statement evaluated to false

The variable was set to 419, which is not equal to 3 and the if statement was false. As you can see, the code segment contained within the else was used in this case.


PHP Tutorial,PHP If Else, PHP If Else example, learn PHP If Else,explain example PHP If Else online free training PHP Tutorial, PHP Tutorial example, learn PHP If Else, online tutorial, download tutorial, PHP Tutorial books, PHP Tutorial videos, live videos PHP Tutorial, learn PHP Tutorial, PHP Tutorial topic PHP If Else, live training PHP Tutorial, download free tutorial