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 .

if (condition) {
  if condition is true code to be executed;
}
<?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.

if (condition) {
  code to be executed if condition is true;
} else {
  code to be executed if condition is false;
}
<?php
$t = 30;

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

PHP - The if Statement:

The if....elseif...else statement executes different codes for more than two conditions.

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;
}
<?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

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

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;
}
<?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

8432830240