PHP Conditional Statements

We have the following conditional statements in PHP language:

  • if statement - in if condtion the code is executes when condition is true

  • if...else statement - if a condition is true executes the code and if that condition is false executes another code

  • if...elseif....else statement - in this condirion executes different codes for more than two conditions

  • switch statement - selects one of many blocks of code to be executed

PHP - The if Statement:
The if statement executes some code if a condition is true .

dzone img Syntax:
if (condition) {
if condition is true code to be executed; }

Example:

<?php
$t = 30;

if ($t < "30") {
    echo "no is less then 30!";
}
?>

PHP - The if...else Statement:
The if....else statement executes some code if a condition is true and another code if that condition is false.

Syntax:
if (condition) {
code to be executed if condition is true;
} else {
code to be executed if condition is false;
} if else if loop in php

Example:

<?php
$t = 30;

if ($t < "30") {
    echo "no is less then 30!";
} else {
    echo "no is greate then 30!";
}
?>

PHP - The if Statement:
if statement in php The if....elseif...else statement executes different codes for more than two conditions.

Syntax:
if (condition) {
code to be executed if this condition is true;
} elseif (condition) {
code to be executed if this condition is true;
} else {
code to be executed if all conditions are false;
}

Example:

<?php
$t = 30;

if ($t < "30") {
    echo "no is less then 30!";
}
elseif {
echo "no is greate then 30!";
}
else {
echo "no is not in range of 30!";} ?>

Switch Conditional Statements

switch case in php

Use the switch statement to select one of many blocks of code to be executed.

Syntax:
switch (x) {
    case 1:
        code to be executed if x=case1;
        break;
    case 2:
        code to be executed if if x=case2;
        break;
    case 3:
        code to be executed if if x=case3;
        break;
    ...
    default:
        code to be executed if x is not exist in all conditions;
}

Example:

<?php
$city = "jaipur";

switch ($city) {
    case "jaipur":
        echo "Your favorite city is jaipur!";
        break;
    case "delhi":
        echo "Your favorite city is delhi!";
        break;
    case "agra":
        echo "Your favorite city is agra!";
        break;
    default:
        echo "Your favorite city is neither jaipur, delhi, nor agra!";
}
?>
Learn PHP If-else Conditions:   We have the following conditional statements in PHP language
Learn PHP loops:  For loops execute the code number of times.
PHP Form Submission:  For post data superglobals $_GET and $_POST are used in php.
Learn PHP array:  An array stores multiple values in one single variable