PHP If Else Statement

PHP if else statement is a conditional statement where specific code is executed if either condition is evaluated to true or false.

The shortest form of an if statement is using only if statement on it's own, when you want to match a condition and perform some action based on it, without capturing any additional conditions

$answer = 42; if ( $answer == 42 ) { echo "The ultimate answer is 42"; } //output: The ultimate answer is 42

So if the conditional expression ( $answer == 42 ) evaluates to true, the code between the curly brackets of an if statement is executed, and if it evaluates to false, the program continues running without printing anything.

If we wanted to capture the false condition, we would add an else to our if statement.

$answer = 50; if ( $answer == 42 ) { echo "The ultimate answer is 42"; } else { echo "You don't have life figured out yet! Keep trying!"; } //output: You don't have life figured out yet! Keep trying!

Next, if we wanted to expand our if statement further and evaluate an additional condition, we would simply add an elseif.

$answer = 13; if ( $answer == 42 ) { echo "The ultimate answer is 42"; } elseif ( $answer == 13 ) { echo "Today is your lucky day!"; } else { echo "Your answer is just plain wrong!"; } //output: Today is your lucky day!

So the first if condition checks if the $answer is 42, it's false, so it moves to the next elseif condition, this time it matches the number 13, so it prints the lucky message. You can add as many elseif conditions as you want to an if statement.

Additionally, you can add as many if statements within other if statements, in fact you can have infinite nested if statements, giving you a lot of flexibility to execute code based on all sorts of conditions:

$answer = 42; $truth = 1; if ( $answer == 42 ) { echo "The ultimate answer is 42. "; //using if statement in this manner assumes the expression is boolean ( 0 or 1) if ( $truth ) { echo "You speak the truth!"; } else { echo "That is a lie!"; } } elseif ( $answer == 13 ) { echo "Today is your lucky day! "; if ( $truth ) { echo "You speak the truth!"; } else { echo "That is a lie!"; } } else { echo "Your answer is just plain wrong!"; } //output: The ultimate answer is 42. You speak the truth!

Alternative If Statement Syntax

There is a slightly different way to write the if statement, as with many PHP statements, which makes it easier to embed in HTML code. We simply remove all the curly brackets and use the colon instead, also an additional keyword endif is used at the end of the if statement.

<html> <title>Welcome to My Web Page</title> <body> <?php if (condition): ?> <!-- html code to run if condition is true --> <?php elseif (condition): ?> <!-- html code to run if elseif condition is true --> <?php else: ?> <!-- html code to run if all other conditions are false --> <?php endif ?> </body> </html>

Ternary Operator

A ternary operator is a short version of an if else statement, there are no elseif in the ternary operator, it's just true or false. A good way to memorize this operator is this: Is this true? Yes : No. The statement consists of a question mark and a colon and is written on one line.

$are_you_okay = true; echo $are_you_okay ? "Yeah I'm good" : "Leave me alone"; //output: Yeah I'm good

So if the condition is true, the code on the left side of the colon is executed, if it's false, the code on the right side would be run. In the example above, I used the echo statement to print out the result right back to the screen. Alternately, the result can be stored in a variable:

$are_you_okay = false; $message = $are_you_okay ? "Yeah I'm good" : "Leave me alone"; echo $message; //output: Leave me alone