ph6

PHP includes conditional statements like the If statement. The If statement can be used to run different code depending on the value of the variable supplied to it.
You use PHP If statements when you want your program to execute a block of code only if a particular condition is true. In other words, you can tell your program "if something is true, then execute this piece of code".

Syntax

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

Example

In the following example, we create a variable called "favoriteFruit" and assign a value to it. We then compare the value with a string: "Pomegranate". If the two values are the same, we output some text.
The two equals signs (==) is a comparison operator (it compares the two values). If this condition is true, it displays the code within the curly braces ({ and }). The curly braces are only necessary if you're outputting multiple lines of code . If you're only outputting one line of code (like we are) they're optional.
$favoriteFruit = "Pomegranate";
if ( $favoriteFruit == "Pomegranate" )
  {
  echo "Your favorite fruit contains around 7% fibre.";
  }
The above example results in the following:
Your favorite fruit contains around 7% fibre.

If Else Statement

We can add an else to our if statement to make our application do something else if the condition is not true.
Example:
$favoriteFruit = "Fig";
  if ( $favoriteFruit == "Pomegranate" )
    {
    echo "Your favorite fruit contains around 7% fibre.";
    }
  else
    {
    echo "Sorry, I don't know how much fibre that fruit contains.";
    }
The above example results in the following:
Sorry, I don't know how much fibre that fruit contains.

If... Elseif Statement

Let's say we learn the fibre content of another fruit. We could then add an elseif to our if statement. That way, we could include a custom message for the new fruit. In fact, we could use elseif as many times as we like.
Example:
$favoriteFruit = "Lychee";
  if ( $favoriteFruit == "Pomegranate" )
    {
    echo "Your favorite fruit contains around 7% fibre.";
    }
  elseif ( $favoriteFruit == "Lychee" )
    {
    echo "Your favorite fruit contains around 1.5% fibre.";
    }
  else
    {
    echo "Sorry, I don't know how much fibre that fruit contains";
    }
The above example results in the following:


Your favorite fruit contains around 1.5% fibre.

No comments:

Post a Comment

Hey, It's Been Grabbed