Search code examples
phpperformanceif-statementfor-loopcontinue

if-statement in a for(each)-loop. continue vs nothing


Let's consider following array:

$data = array(
    '0' => array(
        'id' => '0',
        'guid' => '22dd39bf-f6d6-4283-b87c-370354a7c2dd',
        'age' => '32',
        'name' => 'Harriet Vazquez',
        'gender' => 'female',
        'email' => '[email protected]',
        'tags' => array(
            '0' => 'sit',
            '1' => 'mollit',
            '2' => 'cillum',
            '3' => 'irure',
        ),
        'friends' => array(
            '0' => array(
                'id' => '0',
                'name' => 'Long Dejesus',
            ),
            '1' => array(
                'id' => '1',
                'name' => 'Carrillo Hodge',
            ),
            '2' => array(
                'id' => '2',
                'name' => 'Coffey Greene',
            ),
            '3' => array(
                'id' => '3',
                'name' => 'Stephanie Chavez',
            ),
            '4' => array(
                'id' => '4',
                'name' => 'Richmond Mitchell',
            ),
        )
    ),
    '1' => array(
        'id' => '1',
        'guid' => '3df3ae55-03f3-4d7d-9c70-c7010a100886',
        'age' => '36',
        'name' => 'David Lynch',
        'gender' => 'male',
        'email' => '[email protected]',
        'tags' => array(
            '0' => 'id',
            '1' => 'ad',
            '2' => 'labore',
            '3' => 'ad',
            '4' => 'veniam',
            '5' => 'nulla',
        ),
        'friends' => array(
            '0' => array(
                'id' => '0',
                'name' => 'Diana Watts',
            ),
            '1' => array(
                'id' => '1',
                'name' => 'Patty Crawford',
            ),
            '2' => array(
                'id' => '2',
                'name' => 'Terrell Larson',
            ),
        )
    ),
    '2' => array(
        'id' => '2',
        'guid' => 'da2c9f3f-ac85-4dfd-a43c-e55e476596ca',
        'age' => '25',
        'name' => 'Hardin Murphy',
        'gender' => 'male',
        'email' => '[email protected]',
        'tags' => array(
            '0' => 'laborum',
            '1' => 'labore',
            '2' => 'dolor',
            '3' => 'excepteur',
            '4' => 'est',
        ),
        'friends' => array(
            '0' => array(
                'id' => '0',
                'name' => 'Mandy Roberts',
            ),
            '1' => array(
                'id' => '1',
                'name' => 'Walker Young',
            ),
            '2' => array(
                'id' => '2',
                'name' => 'Middleton Baldwin',
            ),
            '3' => array(
                'id' => '3',
                'name' => 'Tillman Harmon',
            ),
        )
    )
);

Let us now create a loop with some conditions

$accepted = array('age', 'name', 'email');
foreach ($data as $idx => $row) 
{
    foreach ($row as $key => $value) 
    {
        if(!is_array($value) && in_array($key, $accepted))
        {
            var_dump($value) . PHP_EOL;

        }
    }
}

Code above will produce output like this:

string(2) "32"
string(15) "Harriet Vazquez"
string(26) "[email protected]"
string(2) "36"
string(11) "David Lynch"
string(22) "[email protected]"
string(2) "25"
string(13) "Hardin Murphy"
string(24) "[email protected]"

And now the question: Is there are any differences (and i mean performance) if I'll add an continue statement within else statement ?

$accepted = array('age', 'name', 'email');
foreach ($data as $idx => $row) 
{
    foreach ($row as $key => $value) 
    {
        if(!is_array($value) && in_array($key, $accepted))
        {
            var_dump($value) . PHP_EOL;

        }
        else
        {
            continue;
        }
    }
}

Solution

  • Continue skips the rest of the code in the current iteration, since there is no code to skip, your second solution will be insignificantly slower (one more operation, and it's useless).

    From the PHP doc :

    continue is used within looping structures to skip the rest of the current loop iteration and continue execution at the condition evaluation and then the beginning of the next iteration.

    It it basically a goto that goes to the end of your current loop iteration, it would be useful in a case like this :

    foreach ($row as $key => $value) 
    {
        if ($value == "whatever")
        {
            continue;
        }
    
        function1($value);
        // Whatever code here
    
        // The continue will skip the above code
    }