0

I'm having a little brain fart and I just want to double check something ...

// Array of [a,b,c,d]
$array = array('a', 'b', 'c', 'd');

// Loop through array
foreach ($array AS $letter)
{
    // Break out of the loop on a specific condition
    if ($letter == 'c')
    {
        break;
    }
}

echo $letter;

I just want to double check $letter's scope. $letter exists outside of the foreach loop and retains the value of 'c', right?

4
Contributors
3
Replies
31
Views
7 Months
Discussion Span
Last Post by ryantroop
2

scope is fine, but intent is not clear in my opinion :-/

If you plan to use a variable outside of a loop, it should be declared outside even if null. That way, in the future, some fool who has to read it will understand what you meant to do, and not think "oh, well this is obviously in the wrong place...."

Just my 2c.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.