1

I just noticed that iterating an array using a while and array_shift behaves differently from a simple for loop when encounters an empty string. Here's a sample:

While loop:

$arr=[1, "",1,""];
while ($elm = array_shift($arr))
{
    var_dump($elm);
}

For loop:

$arr=[1, "",1,""];
for ($i = 0; $i<count($arr); $i++)
{
    var_dump($arr[$i]);
}
die();

Why does the while exit the loop?

3
  • 2
    because the expression evaluates to false ? Commented Mar 10, 2018 at 19:10
  • 3v4l.org/h94FE Commented Mar 10, 2018 at 19:13
  • to make while loop keep running, Its must to provide true value in while(). In first array, you second value is null, Which consider as false OR 0 value. Hence while loop getting stop. Commented Mar 10, 2018 at 19:21

4 Answers 4

6

Because an empty string is a falsy value in PHP:

var_dump((bool) ""); // bool(false)

The PHP Manual

To make the first example work with empty strings you should check against array_shift returning NULL:

$arr = [1, "", 1, ""];
while (($elm = array_shift($arr)) !== NULL) {
    var_dump($elm);
}

Explanation

array_shift removes the first element from the array and returns its value:

$arr = [1, ""];
array_shift($arr); // we get 1
array_shift($arr); // we get ""
array_shift($arr); // we get NULL because the array is empty now

In your first example you have while ($elm = array_shift($arr)) which means:

  • remove the first element and save its value inside $elm
  • interpret $elm as a boolean (this is done implicitly) and only continue if it evaluates to TRUE.

Let's manually go through each iteration to make it more clear:

  • first iteration: while ($elm = 1) -> while (true) because 1 is truthy. We continue the loop.

  • second iteration: while ($elm = "") -> while (false) because an empty string is falsy. We exit the loop.

Sign up to request clarification or add additional context in comments.

3 Comments

For clarity, it's $elm = array_shift($arr) is equivalent to $elm = "" which is evaluated by the left-side variable value set in the operation, e.g., "", which evaluates to falsey.
I guess that performance-wise, the for loop it's better than the while plus null test, correct?
@sabas Definitely. array_shift changes the original array (which takes additional time), the for loop doesn't: $arr = [1,2,3]; array_shift($arr);array_shift($arr);array_shift($arr); var_dump($arr); outputs an empty array.
2

Because the empty string evaluates to false

Comments

1

Because the second value of your array is empty string which converts to bool false

Try to use another array and everything will be good:

$arr=[1, "non-empty value", 1, "another non-empty value"];
while ($elm = array_shift($arr))
{
    var_dump($elm);
    echo '<br>'; // this is bonus ;)
}

Comments

0

The solutions proposed by others are still prone to error if an array contains a null. If it's really necessary to modify the array rather than do a simple foreach, a robust way to construct this would be:

while ($arr)
{
    $elm = array_shift($arr)
    var_dump($elm);
}

This is because arrays are always truthy unless they're empty, regardless of what values they contain.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.