PHP if-else


In this lesson, you will learn how to use conditional statements in PHP. Conditional statements can change the flow of the program depending on the conditions being met.

PHP if statement

The ‘if statement’ is a conditional statement in PHP which evaluates an expression between parenthesis. If this expression is evaluated as true, the block of the code associated with if statement is executed, skipped otherwise. This statement enables developers to control the flow of the program. ‘if statement’ in PHP is similar to that of C.

The syntax of ‘if statement.’

if(expression){
statement
}
“if Statement” Flowchart

Example I of if statement in PHP

if($age < 18) 
    echo "You need to be at least 18 years old to buy our membership";

This example uses a comparison operator to compare a variable with an integer. The ‘if statement’ will evaluate the condition given in the parenthesis and display the string if the condition is true.

Example II of if statement in PHP

<?php
$x = 4;
if($x < 10){
 echo '$x is less than 10';
}
?>

The if-else statement

We have seen ‘if statement’ which execute the associated code block in case if-condition is met. But what if we want the other case as well. Well, PHP gives you an if-else statement which allows you to execute one block of the code if the given condition is met or it will perform the other block of the code if it evaluates to false.

The syntax of ‘if-else statement.’

if(condition){
  Statement
}
else {
 Statement 
}
“if-else Statement” Flowchart

Example of if-else statement

Let’s say you have two integer values, now compare these and return the largest one or if these are equal, display the appropriate message.

<?php

$x = 4;
$y = 4;

if($x > $y){
    echo "x is greater than y";
}else if($x < $y){
    echo "x is smaller than y";
}else {
    echo "x and y are equal";
}
    
?>

The Ternary operator

The ternary operator gives you a shorthand way of writing the if-else statement. It is represented by the question mark symbol and a colon symbol. To understand the ternary operator, consider the following example.

<?php

$x = 10;
if($x%2==0){
    echo "x is even";
}
else {
    echo "x is odd number";
}
    
?>

Example of Ternay operator

Now write this code, using the ternary operator.

<?php

$x = 11;
echo ($x%2==0) ? "x is even": "x is odd number";    

?>
Tip: Writing conditional statements using ternary operator is easy, but it makes the code difficult to read.

The Null coalescing operator

PHP 7 has introduced a new null coalescing operator (??) which is a shorthand of a situation when you have to use a ternary operator in conjunction with isset() function.

<?php
$username="admin";
$name = isset($username) ? $username : 'anonymous';
echo $name;
?>

Example of Null coalescing operator

Now write the above piece of code using the null coalescing operator.

<?php
$username="admin";
$name =  $username ?? 'anonymous';
echo $name;
?>

As you can see the code is more compact and easy to read now.

PHP Operators Tutorial Home PHP Switch statement
Tutorials Panel

About Tutorials Panel

programming tutorial and source code for Software Engineers and developers. https://twitter.com/TutorialsPanel

View all posts by Tutorials Panel →