Broad Network


PHP Loop Structures

PHP Control Structures with Security Considerations - Part 2

Foreword: In this part of the series, I explain PHP loop statements.

By: Chrysanthus Date Published: 17 Oct 2018

Introduction

This is part 2 of my series, PHP Control Structures with Security Considerations. In this part of the series, I explain PHP loop statements. You should have read the previous part of the series before coming here, as this is the continuation.

The do-while Statement
The syntax for the do-while statement is:

    do Statement while ( Expression );

It can have one or more internal statements in the place of Statement. The internal statements end with semicolon. If there are more than one internal statements, they will be placed in a block (within curly brackets). Here, Expression is the condition. An example of the do-while statement is in the code:

    $n = 0;

    do
        ++$n;
    while ($n<5);

Both the internal statement and the whole do-while statement have been terminated by semicolon. The first statement in the above code is not part of the do-while statement.

The while Statement
The syntax for the while statement is:

    while ( Expression ) Statement

Here, Expression is the condition. The statement can have one or more internal statements in place of Statement. An example of the while statement is in the code:

   $n = 0;

    while ($n<5)
        ++$n;

The first statement here is not part of the while statement.

For the do-while structure, the statement is executed first before the condition is tested. However, for the while structure, the condition is tested first before the statement is executed.

The for Statement
The syntax for the for-statement is:

        for (initialStatement; Condition; nextIterationStatement) Statement

An example of the for-statement is:

    for ($n=0; $n<5; ++$n)
            echo $n;

The output of this code should be;

0
1
2
3
4

There is one internal statement in the example. The internal statement in the example is a print statement. For the syntax, Statement can be replaced by one or more internal statements in a block.

The break Statement
The “break;” statement can be used to terminate a loop before its determined end. The syntax for this is:

    break;

Try the following code and note that the loop ends after $n is 2.

<?php

    for ($n=0; $n<5; ++$n)
        {
            echo $n, '<br>';
            if ($n == 2)
                {
                    break;
                }
        }

?>

The output is:

0
1
2

Each time in the loop, the if-condition is checked for the value of true. When $n is 2, the if-condition will return true; making the if-block to execute. In the if-block, you have just one statement, the break statement. It is just one word, break. Always end the break statement and other statements with a semicolon. The break statement stops the loop from repeating. In this case it stopped the loop when the internal if-condition occurred (was true).

The break reserved word can also be followed by a positive integer which tells how many nested enclosing structures are to be broken out. Try the following code, where the interger is 0.

<?php

    for ($i=0; $i<3; ++$i)
        {
            echo 'i: ', $i, ' ';
            for ($j=0; $j<3; ++$j)
                {
                    echo 'j: ', $j, ' ';
                    for ($k=0; $k<3; ++$k)
                        {
                            echo 'k: ', $k, ' ';
                            if ($k == 1)
                                {
                                    break 0;
                                }
                        }
                }
        }

?>

Repeat the code three more times replacing the integer with 1, 2, and 3. Note that the integers, 0 and 1 take you to the immediate outer loop. The integer, 2 takes you to the preceding outer loop. The integer, 3 takes you to the preceding preceding outer loop, and so on.

Without integer, you are taken to the outer loop. Integers start having their effects as from the number 2.

The continue Statement
You can cause an iteration to be skipped as the loop is repeating. You use the continue statement for this. The syntax for this is:

        continue;

The following code illustrates this, when $n is 2. The iteration for, $n equal 2, is skipped.

<?php

    for ($n=0; $n<5; ++$n)
        {
            if ($n == 2)
                {
                    continue;
                }
            echo $n, '<br>';
        }

?>

The output is:

0
1
3
4

In order to skip the iteration (passage) of the whole block, place the continue statement and its condition at the beginning of the block.

The continue reserved word can also be followed by a positive integer which tells how many nested enclosing structures are to be skipped through. Try the following code, where the interger is 0.

<?php

    for ($i=0; $i<3; ++$i)
        {
            echo 'i: ', $i, ' ';
            for ($j=0; $j<3; ++$j)
                {
                    echo 'j: ', $j, ' ';
                    for ($k=0; $k<3; ++$k)
                        {
                            echo 'k: ', $k, ' ';
                            if ($k == 1)
                                {
                                    continue 0;
                                }
                        }
                }
        }

?>

Repeat the code three more times replacing the integer with 1, 2, and 3. Note that the integers, 0 and 1 take you to the immediate outer loop. The integer, 2 takes you to the preceding outer loop. The integer, 3 takes you to the preceding preceding outer loop, and so on.

Without integer, you are taken to the outer loop. Integers start having their effects as from the number 2.

That is it for this part of the series. We stop here and continue in the next part.

Chrys


Related Links

Basics of PHP with Security Considerations
White Space in PHP
PHP Data Types with Security Considerations
PHP Variables with Security Considerations
PHP Operators with Security Considerations
PHP Control Structures with Security Considerations
PHP String with Security Considerations
PHP Arrays with Security Considerations
PHP Functions with Security Considerations
PHP Return Statement
Exception Handling in PHP
Variable Scope in PHP
Constant in PHP
PHP Classes and Objects
Reference in PHP
PHP Regular Expressions with Security Considerations
Date and Time in PHP with Security Considerations
Files and Directories with Security Considerations in PHP
Writing a PHP Command Line Tool
PHP Core Number Basics and Testing
Validating Input in PHP
PHP Eval Function and Security Risks
PHP Multi-Dimensional Array with Security Consideration
Mathematics Functions for Everybody in PHP
PHP Cheat Sheet and Prevention Explained
More Related Links

Cousins

BACK NEXT

Comments