PHP do while Loop

The do...while loop – Loops through a block of code once, and then repeats the loop as long as the specified condition is true.

The PHP do…while Loop

The do...while loop will always execute the block of code at least once, it will then check the condition, and repeat the loop while the specified condition is true. Continue reading PHP do while Loop

PHP while Loop

The while loop – Loops through a block of code as long as the specified condition is true

The PHP while Loop

The while loop executes a block of code as long as the specified condition is true.

Example

Print $i as long as $i is less than 6:

$i = 1;
while ($i < 6) {
  echo $i;
  $i++;
}

Note: remember to increment $i, or else the loop will continue forever.

The while loop does not run a specific number of times, but checks after each iteration if the condition is still true.

The condition does not have to be a counter, it could be the status of an operation or any condition that evaluates to either true or false. Continue reading PHP while Loop

PHP Loops

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. Continue reading PHP Loops

PHP switch Statement

The switch statement is used to perform different actions based on different conditions.


The PHP switch Statement

Use the switch statement to select one of many blocks of code to be executed. Continue reading PHP switch Statement

PHP Nested if Statement

Nested If

You can have if statements inside if statements, this is called nested if statements. Continue reading PHP Nested if Statement

PHP Shorthand if Statements

Short Hand If

To write shorter code, you can write if statements on one line.

Example

One-line if statement:

$a = 5;

if ($a < 10) $b = "Hello";

echo $b

Continue reading PHP Shorthand if Statements

PHP if…else Statements

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. Continue reading PHP if…else Statements

PHP if Operators

Comparison Operators

If statements usually contain conditions that compare two values.

Example

Check if $t is equal to 14:

$t = 14;

if ($t == 14) {
  echo "Have a good day!";
}

To compare two values, we need to use a comparison operator. Continue reading PHP if Operators

PHP if Statements

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

Continue reading PHP if Statements

PHP Operators

PHP Operators

Operators are used to perform operations on variables and values.

PHP divides the operators in the following groups:

  • Arithmetic operators
  • Assignment operators
  • Comparison operators
  • Increment/Decrement operators
  • Logical operators
  • String operators
  • Array operators
  • Conditional assignment operators

Continue reading PHP Operators