PHP Decision Making Statements

We saw in the previous lesson, how to work with PHP loops, while, do..while, for, and for…each. Now, we learn PHP statements with examples and illustrations. With PHP decision-making statements, you can easily take decisions based on different conditions.

Let’s discuss the decision-making statements,

  • if statement
  • if…else statement
  • if…else if…else statement
  • switch statement

if statement

The if decision-making statement executes the code, if the condition is true.

Here’s the syntax,

if (condition) {
    code will execute if condition is true;
}

Example

Here, the code executes if the condition $a > 5 is true. The value of a is greater than 5, so the if statement executes and prints, We’re learning PHP,

<!DOCTYPE html>
<html>
<body>

<?php
   $a = 10;

   if ($a > 5) {
     echo "We’re learning PHP!";
   }

?>

</body>
</html>

Here’s the output,

PHP decision making statements - if statement

if…else statement

The if…else decision-making statement executes some code if the condition is true and another code if the condition is false. The false condition comes under else.

Here’s the syntax,

if (condition) {
    code will execute if condition is true;
} else {
    code will execute if condition is false;
}

Example

The value of $a = 10, so the first statement won’t execute, since its condition is $a < 5.
However, the second statement else executes and prints We’re learning Python as shown below,

<!DOCTYPE html>
<html>
<body>

<?php

   $a = 10;

   if ($a < 5) {
      echo "We’re learning PHP!";
   }

  else {
     echo "We’re learning Python!";
  }

?>

</body>
</html>

Here’s the output,

PHP if else decision making statement

if…else if…else Statement

The if…else if…else statement executes code for different conditions. If more than 2 conditions are true, you can use else for the false condition.

Here’s the syntax,

if (condition) {
    code will execute if condition is true;
} else if (condition) {
    code will execute if another condition is true;
} else {
    code will execute if all the above given conditions are false;
}

Example

Here, $a = 5, so the else condition gets executed.

<!DOCTYPE html>
<html>
<body>

<?php
$a = 5;

if ($a < 5) {
    echo "We’re learning PHP!";
}
else if ($a > 5) {
    echo "We’re learning Python!";
}
else {
    echo "We’re learning Java!";
}
?>

</body>
</html>

Here’s the output,

PHP elseif decision making statement

PHP Switch Statement

If you want to avoid using long code for if…elseif…else, then use the switch statement. Under switch, you can add many blocks of code and select one at a time.

Here’s the syntax,

// c is the expression

switch (c) {
    case label1:
        code to be executed if c=label1;
        break;

    case label2:
        code to be executed if c=label2;
        break;

    case label3:
        code to be executed if c=label3;
        break;

    case label4:
        code to be executed if c=label4;
        break;
    ...
    default:
        this get  executed if c is different from all labels;
}

Example

Here, the Java topic gets printed as output.

<!DOCTYPE html>
<html>
<body>

<?php

$topics = "Java";

switch ($topics) {

    case "Android":
        echo "Topic: Android";
        break;

    case "Java":
        echo "Topic: Java";
        break;

    case "Python":
        echo "Topic: Python";
        break;

    default:
        echo "Topic: Management";

}

?>

</body>
</html>

Here’s the output,

PHP switch decision making statement

Here, we saw how to work with PHP Decision-making statements, if, if..else, if…else…elseif, switch, etc.

PHP Loops
PHP String Handling
Studyopedia Editorial Staff
[email protected]

We work to create programming tutorials for all.

No Comments

Post A Comment