PHP Control Structures: If else, Switch Case

In today’s topic, we will learn various PHP control structures. These are very important structures that are necessary to remember before writing a PHP program. Before these, we also discussed Data Type in PHP:, Operators In PHP, if you don’t know then Hurry up Now!

Learn About PHP Control Structures?

In simple terms, a control structure in PHP is used to control the flow of code. Generally, a program is executed sequentially and a control structure allows you to alter that flow, usually depending upon conditions.

Control structures are the core features in the PHP language that allow your script to respond differently to different inputs or situations. This will allow scripting to give different responses based on user input, or some other data.

Remember that to perform different actions based on different conditions we used conditional statements.

In PHP there are some conditional statements:

  • if statement –  if the condition is true then it executes statement1.
  • if…else statement –  if a condition is true then it executes the statement1 and if that condition is false then it executes statement2.
  • if…elseif…else statement –There are more than two conditions and executes the different statements.
  • switch statement – It performs different actions based on different conditions.

PHP – if Statement

It executes some statements if one condition is true.

Syntax:-  :-  if (condition) {
              statement to be executed if condition is true;
              }

Example: Output “GeektoCode.com!” if the 5 is less than 20

<!DOCTYPE html>
<html>
<body>

<?php
$t =5;

if ($t < "20") {
  echo "GeektoCode.com!";
}
?>
 
</body>
</html>
 OUTPUT: GeektoCode.com!

PHP – if…else Statement

It executes statements1 if one condition is true and if the condition is false then it executes statement2.

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

Example:

<!DOCTYPE html>
<html>
<body>

<?php
$t =45;

if ($t < "20") {
  echo "GeektoCode.com!";
}else{
echo “Hello GeektoCode.com!”;
}
?> 
</body>
</html>
 OUTPUT: HelloGeektoCode.com!

PHP – if…elseif…else Statement

It executes different statements for more than two conditions.

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

Example:

<!DOCTYPE html>
<html>
<body>

<?php
$t =45;

if ($t > "20") {
  echo "GeektoCode.com!";
}else if ($t < "50"){
  echo "Hello GeektoCode.com!";
}else{
  echo "Bye GeektoCode.com!";
}
?> 
</body>
</html>
 OUTPUT: GeektoCode.com!

PHP – Switch Statement

switch statement is to select one of many blocks of statement to be executed. break statement to prevent the statement from running into the next case automatically.

Syntax :-  switch (n) {
        case label1:
        code to be executed if n=label1;
        break;
       case label2:
       code to be executed if n=label2;
       break;
      case label3:
      code to be executed if n=label3;
      break;
      ...
     default:
    code to be executed if n is different from all labels;
    }

Example:

<!DOCTYPE html>
<html>
<body>

<?php
$favbike = "Hero";

switch ($favbike) {
  case "Hero":
    echo "Your favorite bike is Hero!";
    break;
  case "Honda":
    echo "Your favorite bike is Honda!";
    break;
  case "TVS":
    echo "Your favorite bike is TVS!";
    break;
  default:
    echo "Your favorite bike is neither Hero, Honda, nor TVS!";
}
?>
 
</body>
</html>
 OUTPUT: Your favorite bike is Hero!

You Might Like

Leave a Reply

Your email address will not be published.

Sorry
Sorry
Sorry
Sorry
Sorry
Sorry
Sorry