Conditional statements in PHP with examples

PHP contingent proclamations: In this article, we will find out about the different restrictive articulations in PHP programming language with examples.

PHP Conditional Statements

While coding, you may arrive at a point where your outcomes must be gotten when a condition is substantial. We utilize contingent articulations. Restrictive proclamations are articulations that must be executed dependent on the satisfaction of a specific condition(s).

There are fundamentally 4 distinct kinds of restrictive explanations in PHP,

1) The if articulation

With the if articulation your code possibly executes just when the condition is valid.

Syntax:

    if(condition){
        //code to be executed when condition is true
    }

Example:

How about we check if an imprint entered is more noteworthy than or equivalent to 80. On the off chance that genuine An evaluation is given.

PHP Code:

<?php
	//defining a variable
	$mark = 120;
	if($mark >= 80){
		echo "you have an A";
	}
?>

Output:

you have an A

2) The if…else explanations

The if…else explanation is utilized when a condition is fulfilled and when it isn’t fulfilled. So it’s pre-owned when the condition is either valid or bogus.

Syntax:

    if (condition){
	    //code to be executed when true }
    else {
	    //code to be executed when false
    }

Example:

Here, we are going to check if the letter entered is an F which will show female else we show male.

PHP Code:

<?php
    //defining a variable
    $gender = 'F';
    if ($gender == 'F'){
        echo "FEMALE";
    }
    else { 
        echo "MALE";
    }
?>

Output:

FEMALE

3) The if…elseif…else explanations

In a circumstance where you have a few conditions, for example, a program to review understudies dependent on their imprints with the letters A, B, C, D, F. the if…elseif…else is utilized for this.

Syntax:

    if (condition1){
	    //code 1 to be executed
    }
    elseif(condition2) {
	    //code 2 to be executed 
    }
    else{
	    //code to be executed if code 1 and code 2 are not true
    }

Example:

We are going to review understudies with the letters A, B, C, D, F dependent on their imprints on 100.

PHP Code:

<?php
    //defining a variable
    $marks = 75;
    
    if ($marks>79){
        echo "A";
    }
    elseif($marks<=79&&  $marks>60) { 
        echo "B";
    }
    elseif($marks<=60&& $marks>50) { 
        echo "C";
    }
    elseif($marks=50) { 
        echo "D";
    }
    else{
        echo "F";
    }

?>

Output:

B

4) The settled if…else articulations

At the point when you find if…else articulations inside an if…else explanation, the announcements are settled. With this announcement, you can get choices results when a condition is valid or false.

Syntax:

    if (condition 1 )
    {
	    if (condition 2 )
	    {
		    //  code1 to be executed
	    }
	    else
	    {
		    // code 2 to be executed
	    }
    }
    else
    {
	    // code 4 to be executed
    }

Example:

We should think about tow numbers utilizing the settled if proclamation.

PHP code:

<?php

// defining variables
$number1 = 40;
$number2 = 12;

if ($number1 != $number2) {
    echo 'number1 is different from number2';
    echo '<br>';
    if ($number1 > $number2) {
        echo 'number1 is greater than number2';
    } else {
        echo 'number2 is greater than number1';
    }
} else {
    echo 'number1 is equal to number2';
}
?>

Output

number1 is different from number2
number2 is greater than number1

5) The switch explanation

The switch explanation is fundamentally the same as the if…else proclamation. In any case, in the situations where your conditions are confused like you have to check a condition with numerous steady values, a switch articulation is liked to an if…else. The examples underneath will assist us with bettering comprehend the switch articulations.

Syntax:

    switch (n)
    {
	    case constant1:
		    // code to be executed if n is equal to constant1;
		    break;
	    case constant2:
		    // code to be executed if n is equal to constant2;
		    break;
	    .
	    .
	    .
	    default:
		    // code to be executed if n doesn't match any constant
    }

Example:

<?php
//variable definition
$gender = 'M';
switch ($gender) {
    case 'F':
        echo 'F is FEMALE';
    break;
    case 'M':
        echo 'M is MALE';
    break;
    default:
        echo 'Invalid choice';
}
?>

How about we rework the example of if… ..else proclamations utilizing switch explanations,

<?php
//variable definition
$gender = 'M';
switch ($gender) {
    case 'F':
        echo 'F is FEMALE';
    break;
    case 'M':
        echo 'M is MALE';
    break;
    default:
        echo 'Invalid choice';
}
?>

Output:

M is MALE

Leave a Comment

error: Alert: Content is protected!!