PHP if…else statement

PHP ifelse statement

if (…else) statement – use this statement if you want to execute a set of code when a condition is true (and another if the condition is not true).

The if…else Statement

An if statement that includes an else clause is called an if…else statement.

This statement is used to execute some code if a condition is true and another code if the condition is false.

Syntax:

if (condition)
code to be executed if condition is true;
else
code to be executed if condition is false;

Let’s see some example:

<?php
$name=”Piolo”;
if ( $name== “Piolo” ) {
        echo "You are welcome Piolo";
} else {
        echo "Sorry, you’re not Piolo";
}
?>

Result:

The above example will output You are welcome Piolo. Here is the explanation:

  • First we have declared a variable $name and set the value to Piolo.
  • Next we have compared the variable to the value stated in our if condition. Since the value in our condition is equal to the value of our variable then the statement is true and it will display You are welcome Piolo.
  • If we change the value in our condition the result would be Sorry, you’re not Piolo.
, , , , , ,

Post navigation

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.