If Else Conditionals in Php?

Posted by

Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps!

We spend hours scrolling social media and waste money on things we forget, but won’t spend 30 minutes a day earning certifications that can change our lives.
Master in DevOps, SRE, DevSecOps & MLOps by DevOps School!

Learn from Guru Rajesh Kumar and double your salary in just one year.


Get Started Now!

If Else statement is the most important feature of any programming language, PHP is used to run any Code Of Block according to If Else conditions. This means when we have to run some other code when the condition is true, then we use If Else if the condition is false.

Types of If Else Conditionals

  1. if Statement
  2. if…else Statement
  3. if…elseif…else Statement

1. if Statement

The if statement is executed only when the condition is true. If the condition is false then nothing is executed.

Example:-

<?php

	$value = 14;

	if ($value < 20) {
	
		echo "Have a nice day";
	}

?>

Output:-

2. If-else statement

There are two types of conditions in the if….else statement. One, when the condition is true then the code should be executed and when the condition is false then the statements should be executed.

Example:-

<?php
	$num = 70;

    	  if ( $num > 7 ) {

        echo "Code to be executed if condition is true";
        
        } else {
        
        echo "Code to be executed if condition is false";
        
        }
?>

Outputs:-

3. if…elseif…else Statement

It is a combination of conditional statements if…else. The only difference is that the if…else statement has been used multiple times. And more than two conditions can be executed in this.

Example:-

<?php
    $age = 19;

    //If else ladder
    if ($age>18){
         echo "You are drink alcohol";
    }
    elseif($age>13){
         echo "You are drink chai with water";
    }
    else{
        echo "You can drink water only";
    }
?>

Output:-

Leave a Reply

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

0
Would love your thoughts, please comment.x
()
x