PHP Decision Making

PHP decision making involves executing certain blocks of code based on certain conditions. In other words, decision making in PHP allows us to control the flow of execution based on certain conditions or situations.

In PHP, decision making can be achieved using a number of control structures. Some of the most commonly used control structures for decision making in PHP include:

if statement:

The if statement is used to execute a block of code if a certain condition is true. The syntax of the if statement is as follows:

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

Here’s an example of the if statement in action:

<?php
// If statement
$age = 20;
if ($age >= 18) {
  echo "You are an adult.";
}
?>

Output:

You are an adult.

In the above code, we have used the if statement to check if the value of $age is greater than or equal to 18. If it is, the code inside the if block will be executed, which simply prints “You are an adult” using the echo statement.

if-else statement:

The if-else statement is used to execute one block of code if a certain condition is true, and another block of code if it is false. The syntax of the if-else statement is as follows:

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

Here’s an example of the if-else statement in action:

<?php
// If-else statement
$age = 15;
if ($age >= 18) {
  echo "You are an adult.";
} else {
  echo "You are not an adult.";
}
?>

Output:

You are not an adult.

In the above code, we have used the if-else statement to check if the value of $age is greater than or equal to 18. If it is, the code inside the if block will be executed, which prints “You are an adult” using the echo statement. Otherwise, the code inside the else block will be executed, which prints “You are not an adult” using the echo statement.

switch statement:

The switch statement is used to execute different blocks of code based on different possible values of a variable. The syntax of the switch statement is as follows:

switch (variable) {
  case value1:
    // code to be executed if variable is equal to value1
    break;
  case value2:
    // code to be executed if variable is equal to value2
    break;
  ...
  default:
    // code to be executed if variable does not match any of the cases
    break;
}

Here’s an example of the switch statement in action:

<?php
// Switch statement
$color = "red";
switch ($color) {
  case "red":
    echo "The color is red.";
    break;
  case "blue":
    echo "The color is blue.";
    break;
  default:
    echo "The color is not red or blue.";
    break;
}
?>

Output:

The color is red.

In the above code, we have used the switch statement to check the value of $color. If it is “red”, the code inside the first case block will be executed, which prints “The color is red” using the echo statement. If it is “blue”, the code inside the second case block will be executed, which prints “The color is blue”.

Wordpress Social Share Plugin powered by Ultimatelysocial
Wordpress Social Share Plugin powered by Ultimatelysocial