Broad Network


PHP foreach and goto structures

PHP Control Structures with Security Considerations - Part 4

Foreword: In this part of the series, I talk about PHP foreach and goto loop statements.

By: Chrysanthus Date Published: 17 Oct 2018

Introduction

This is part 4 of my series, PHP Control Structures with Security Considerations. In this part of the series, I talk about PHP foreach and goto loop statements. You should have read the previous parts of the series before reaching here, as this is the continuation.

foreach
The foreach construct is a variant of the for-statement. It simply gives an easy way to iterate over arrays. foreach works only on arrays (and objects), and will issue an error when you try to use it on a variable with a different data type or an uninitialized variable. There are two syntaxes; the second is a minor but useful extension of the first:

foreach (array_name as $value)
    statement

foreach (array_name as $key => $value)
    statement

The first form loops over the array given by array_name. On each loop, the value of the current element is assigned to $value and the internal array pointer is advanced by one (so on the next loop, you'll be looking at the next element). $value is a name of your choice. Try the following code:

<?php

    $arr = array('John' => "accountant", 'Paul' => "teacher", 'Peter' => "doctor", 'Mary' => "lawyer", 'Susan' => "nurse", 35 => "businessman", 42 => "contractor");

    foreach ($arr as $value)
        {
            echo $value, '<br>';
        }

?>

The output is:

    accountant
    teacher
    doctor
    lawyer
    nurse
    businessman
    contractor

The second syntax does the same thing, except that the current element's key will be assigned to the variable $key on each loop. $key is a name of your choice. Try the following code:

<?php

    $arr = array('John' => "accountant", 'Paul' => "teacher", 'Peter' => "doctor", 'Mary' => "lawyer", 'Susan' => "nurse", 35 => "businessman", 42 => "contractor");

    foreach ($arr as $key => $value)
        {
            echo $key, '=>', $value, '<br>';
        }

?>

The output is:

    John=>accountant
    Paul=>teacher
    Peter=>doctor
    Mary=>lawyer
    Susan=>nurse
    35=>businessman
    42=>contractor

When foreach first starts executing, the internal array pointer is automatically reset to the first element of the array. This means that you do not need to call reset() before a foreach loop.

Note: for the index array, the key is an integer (not in quotes).

goto
The goto operator can be used to jump to another section in the program. The target point is specified by a label followed by a colon, and the instruction is given as, goto followed by the desired target label. This is not a full unrestricted goto. The target label must be within the same file and context, meaning that you cannot jump out of a function or method, nor can you jump into one. You also cannot jump into any type of loop or switch structure. You may jump out of these (loop or switch structure), but not into.

Try the following code, where 'Foo' is not printed:

<?php

    goto a;
    echo 'Foo';

    //some code

    a:
    echo 'Bar';

?>

The output is:

    Bar

Note that the label, a or any other name, is not preceded by $.

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