简体   繁体   中英

Array Pointer in PHP

I'm a bit confused about array pointers in PHP. The below code worked fine:

$ages = [1, 3, 5];
while($age = current($ages)) {
  echo $age . ", ";
  next($ages);
}

But I've no idea why the below code didn't print any thing out:

$ages = [];
for($i = 0; $i < 10; $i++) {
  $ages[] = $i;
}
while($age = current($ages)) {
  echo $age . ", ";
  next($ages);
}

I also tried to print with for loop, but in below code only the for loop printed, the while loop still didn't print.

$ages = [];
for($i = 0; $i < 10; $i++) {
  $ages[] = $i;
}
for($i = 0; $i < 10; $i++) {
  echo $ages[$i] . ", ";
}
while($age = current($ages)) {
  echo $age . ", ";
  next($ages);
}

I'm really not sure why it behaved like this, anyone could help me out?

why it behaved like this

Tl;dr: Because of the first element of the newly created array is 0 and the assignment operator returns the value assigned to the variable, which causes the expression evaluated to false , hence the while-loop's body never run.


After running the for-loop, the received array would be [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] .

At first, the invocation of current($ages) will return the first element of the newly created array which is 0 , then the assignment operator = will return the value assigned to the $age variable, which is 0 . Then, the code will be:

while(0) {
  echo $age . ", ";
  next($ages);
}

0 is evaluated to false, that's why the loop's body will never be run. Hence no output to the screen.

You will need to check if the result of current() is different from the boolean false (means the cursor did not find the element), not just assign its value. As when the value is 0 , you get while(0) , which breaks the loop.

$ages = [];
for($i = 0; $i < 10; $i++) {
  $ages[] = $i;
}
while($age = current($ages) !== false) {
  echo $age . ", ";
  next($ages);
}

https://3v4l.org/61WoL

However, this will fail should any of the elements in the array have the value of boolean false . Its therefor not recommended to iterate over the array like this at all, you should instead be using the proper tools, by using a foreach loop. This doesn't actually move the cursor, but you can "make" it move the cursor by calling next() for each iteration.

$ages = [];
for($i = 0; $i < 10; $i++) {
  $ages[] = $i;
}
foreach ($ages as $age) {
    echo current($ages).", ";
    next($ages);
}

If you're just looking to print the values, the best way would be either printing directly from the foreach loop, or by using implode() .

foreach ($ages as $age) {
    echo $age.", ";
}

or

echo impolode(",", $ages);

First, the syntax of your php code is wrong:

Replace Line 5 with while($age == current($ages)) {

You used = which isn't for comparisons but == is. Try doing that, then see.

Also , you could do it in a much simpler way:

foreach($ages as $age) {
  echo "$age, ";
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM