PHP If Statements
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
| Code |
|---|
|
|
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.
| Code |
|---|
|
|
The above example results in the following:
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:
| Code |
|---|
|
|
The above example results in the following:
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:
| Code |
|---|
|
|
The above example results in the following:

