• Control statements in Php are code statements used to control the flow of execution of a program based on certain conditions or criteria.
  • PHP provides various types of control statements, including:

Types of Control Statements in Php

    1. Conditional/Decision Control Statements in Php:

      1. Conditional statements allow the execution of specific code blocks based on certain conditions.

      2. The most commonly used conditional statements in PhP are if, if-else, if-else-elseif, and switch.

      3. If Statement 
        • Syntax:
          • if (condition) {
                 // codes for the condition is true
      4. If-else Statement
        • Syntax:

if (condition) {
      // codes for the condition is true
} else {
     // codes for the condition is false
}

      • If – else – else if Statement
        • Syntax:
if (condition1) {
     // codes for condition1 is true
} elseif (condition2) {
     // codes for condition2 is true
}
elseif (condition n) {
     // codes for condition n is true
} else {
// codes for false condition
} 
      1. Switch Statement 
        • Syntax:
          •  switch (expression/variable) {
                 case value1:
                      // code for expression equals value1
                      break;
                 case value2:
                      // code for expression equals value2
                      break;
          •       case value n:
                      // code for expression equals value n
                      break;
                 default:
                      // code for false/does not match any of the values of the above case
              }
    1. Looping Control Statements in Php:

      1. Looping statements are used to execute a block of code repeatedly until a certain condition is met/condition is true.

      2. The most commonly used looping statements in PhP are for, while, do-while, and for-each.

For Loop Statement

Syntax:

for (initialization; terminate_condition; increment/decrement) {
     // code to be executed repeatedly while the condition is true
}

While Loop Statement

Syntax:

while (condition) {
     // codes for the true condition
}

Do-While Loop Statement

Syntax:

do {
     // code to be executed at least once and repeatedly while the condition is true
} while (condition);

For Each Loop Statement (Used with Array mainly)

Syntax:

foreach ($array_name as $variable_name {
     // code to be executed for each element in the array
}

3. Jumping Control Statements in Php:

      1. Jump/jumping statements allow the programmer to change the normal flow of execution of a program suddenly.
      2. The most commonly used jump statements in PHP are break, continue, and goto.

Break Statement

        • used to exit a loop prematurely/forcibly.

Syntax:

while (condition) {
     // code to be executed repeatedly while the condition is true
     if (some condition) {
          break;
     }
}

Continue Statement 

        • used to skip the current iteration and move to the next one

while (condition) {
     // code to be executed repeatedly while the condition is true
     if (some condition) {
          continue;
     }
     // code after the continue statement will not be executed for that iteration
}

4. Error handling statements:

      1. Error handling statements allow the programmer to handle errors and exceptions that occur during the execution of a program.

      2. The most commonly used error-handling statements in PHP are try, catch, and throw.

Loading

Categories: Php Theory

0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.