Control structure in php w3schools


Conditional statements are used to perform different actions based on different conditions.


PHP Conditional Statements

Very often when you write code, you want to perform different actions for different conditions. You can use conditional statements in your code to do this.

In PHP we have the following conditional statements:

  • if statement - executes some code if one condition is true
  • if...else statement - executes some code if a condition is true and another code if that condition is false
  • if...elseif...else statement - executes different codes for more than two conditions
  • switch statement - selects one of many blocks of code to be executed

PHP - The if Statement

The if statement executes some code if one condition is true.

Syntax

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

Example

Output "Have a good day!" if the current time (HOUR) is less than 20:

$t = date("H");

if ($t < "20") {
  echo "Have a good day!";
}
?>

Try it Yourself »



PHP - The if...else Statement

The if...else statement executes some code if a condition is true and another code if that condition is false.

Syntax

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

Example

Output "Have a good day!" if the current time is less than 20, and "Have a good night!" otherwise:

$t = date("H");

if ($t < "20") {
  echo "Have a good day!";
} else {
  echo "Have a good night!";
}
?>

Try it Yourself »


PHP - The if...elseif...else Statement

The if...elseif...else statement executes different codes 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

Output "Have a good morning!" if the current time is less than 10, and "Have a good day!" if the current time is less than 20. Otherwise it will output "Have a good night!":

$t = date("H");

if ($t < "10") {
  echo "Have a good morning!";
} elseif ($t < "20") {
  echo "Have a good day!";
} else {
  echo "Have a good night!";
}
?>

Try it Yourself »


PHP - The switch Statement

The switch statement will be explained in the next chapter.


PHP Exercises




In the following chapters you will learn how to repeat code by using loops in PHP.


PHP Loops

Often when you write code, you want the same block of code to run over and over again a certain number of times. So, instead of adding several almost equal code-lines in a script, we can use loops.

Loops are used to execute the same block of code again and again, as long as a certain condition is true.

In PHP, we have the following loop types:

  • while - loops through a block of code as long as the specified condition is true
  • do...while - loops through a block of code once, and then repeats the loop as long as the specified condition is true
  • for - loops through a block of code a specified number of times
  • foreach - loops through a block of code for each element in an array

The following chapters will explain and give examples of each loop type.



All the conditional statements and loop statements are collectively termed as Control Statements.

Conditional Statements:

Conditional statements are used in a code to perform an action only if a particular condition is satisfied.

PHP provides four types of conditional statements. These are:

if:

If statement is used to perform an action if a single condition is true.

..else:

If else statement is used to perform an action if a single condition is true and to perform another action if that condition is false.

..elseif….else:

If elseif else statement is used to perform different actions for different conditions.

switch:

Switch statement is used to select one of the action to perform from multiple actions, relative to the true condition.

Loop Statements:

Loop Statements are used to execute a block of code continuously as long as the condition of the loop is true, and stops only when the condition fails.

PHP provides three types of loop statements. These are:

For loop:

For loop is used to execute a group of action only for a specified number of times.

While Loop:

While loop is used to execute a group of action as long as the specified condition is true.

Do While Loop:

Do While loop is used to execute a block of code at least once and then to repeat the actions as long as the specified condition is true.

Control structure in php w3schools

What are control structures in PHP?

Control Structures are at the core of programming logic. They allow a script to react differently depending on what has already occurred, or based on user input, and allow the graceful handling of repetitive tasks. In PHP, there are two primary types of Control Structures: Conditional Statements and Control Loops.

What are the control statement in PHP?

Control statements are conditional statements that execute a block of statements if the condition is correct. The statement inside the conditional block will not execute until the condition is satisfied.

What are the 4 PHP conditional statements?

PHP Conditional Statements if statement - executes some code if one condition is true. if...else statement - executes some code if a condition is true and another code if that condition is false. if...elseif...else statement - executes different codes for more than two conditions.

What is PHP in w3schools?

PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages. PHP is a widely-used, free, and efficient alternative to competitors such as Microsoft's ASP. Start learning PHP now »