BCA / B.Tech 11 min read

Flow Control Statements in PHP

Flow Control Statements  in Hindi | Flow Control Statements in PHP in Hindi: 


In PHP, flow control statements are used to control the flow of code. These statements help in making decisions, repeating tasks, and executing code in special circumstances. In PHP, there are mainly three types of flow control statements:

Conditional Statements in Hindi |  Conditional Statements:

Conditional statements are used to decide that a particular code block will run only when a specific condition is true.


 Flow Control Statements in Hindi |  PHP Flow Control Statements in Hindi  

(i) if statement
This is the simplest conditional statement. If the condition is true, the code block will run.

  $age = 20;
  if ($age >= 18) {
    echo "You are eligible to vote.";
  }
?>

(ii) if-else statement
If the condition is true, the if block will run, otherwise the else block.

  $age = 16;
  if ($age >= 18) {
    echo "You are eligible to vote.";
  } else {
    echo "You are not yet eligible to vote.";
  }
?>

(iii) if-elseif-else statement
This checks multiple conditions.

  $marks = 75;
  if ($marks >= 90) {
    echo "You have got A+ grade.";
  } elseif ($marks >= 75) {
    echo "You have got A grade.";
  } elseif ($marks >= 50) {
    echo "You have got B grade.";
  } else {
    echo "You have failed.";
  }
?>

(iv) switch statement
This compares a variable against multiple values and takes action.

  $day = "Monday";
  switch ($day) {
    case "Monday":
      echo "Today is the beginning of the week.";
      break;
    case "Saturday":
      echo "Today is a holiday.";
      break;
    default:
      echo "Today is a normal day.";
  }
?>

 Loops in Hindi | Loops:

Loops are used to run code repeatedly. There are four types of loops in PHP:

(i) for loop
A for loop is used when you know how many times to run the code.

  for ($i = 1; $i <= 5; $i++) {
    echo "This is number: $i
";
  }
?>

(ii) while loop
This runs as long as the condition remains true.

  $i = 1;
  while ($i <= 5) {
    echo "This is number: $i
";
    $i++;
  }
?>

(iii) do-while loop
This runs at least once, even if the condition is false.

  $i = 1;
  do {
    echo "This is number: $i
";
    $i++;
  } while ($i <= 5);
?>

(iv) foreach loop
This loop is especially useful for Arrays.

  $colors = ["Red", "Blue", "Green"];
  foreach ($colors as $color) {
    echo "Color: $color
";
  }
?>

3. Jump Statements

Jump statements are used to control the flow of code. These can interrupt or control the action of a loop or other statements.

(i) break statement
This immediately stops the loop.

  for ($i = 1; $i <= 10; $i++) {
    if ($i == 5) {
      break; // The loop will stop here
    }
    echo "Number: $i
";
  }
?>

(ii) continue statement
This skips the current iteration of the loop and goes to the next one.

  for ($i = 1; $i <= 5; $i++) {
    if ($i == 3) {
      continue; // Will skip 3
    }
    echo "Number: $i
";
  }
?>

(iii) goto statement
This allows the code to jump to a label.

  $i = 1;
  goto skip;
  echo "This code will not run.";

  skip:
  echo "This code will jump to the label.";
?>
Importance of PHP flow controls in Hindi | Importance of PHP flow control statements:

  • Decision-making ability: By using these statements, the code can be made intelligent and capable of making decisions.
  • Dynamic processes: These statements make the application dynamic, such as form processing and use of user data.
  • Control in repetition: By using loops and jump statements, you can save time and effort.
  • By correctly understanding PHP's flow control statements, you can control the flow of the code and make it more effective.
  • Conditional statements help in making decisions, loops simplify repetitive tasks, and jump statements control the action of the code.
  • All this together makes PHP a powerful and useful language.