Jump to content

array check ahead loop


calmchess

Recommended Posts

I need to run a for loop on an array and read ahead 1 index  so that if that spot is null then i can break out of the for loop something like this but it doesn't work.....

$whopro = array("slot0","slot1","slot2");

for($l=17; $l < 34; $l++){

if($whopro[$i+1]==null){
break;
}
echo $whopro[i]; 
}

now obviously the loop loops mor times than there are array indexes.The whole point of this is to stop getting php notice errors without changing the for loop integers.

 

 

Link to comment
https://forums.phpfreaks.com/topic/149723-array-check-ahead-loop/
Share on other sites

what is $i?

why is $l way out of the array bounds

 

$whopro has three elements, starting with 0, 1, 2

so trying to reference $l=17 gives ya an notice, about a variable not being declared.

 

php dusn assign non existant variables to null.

if the variable dusn exist why not just use isset?

 

 

ya can perform a check within the for loop

for($i=0;$i<=20;$i++) $slot[$i]="Slot{$i}";

for($l=17;$i<34 && issset($slot[$i]);$i++)
  echo $slot[$l];

 

but if u really need it within the loop, the lookahead (note that by program flow, the last valid entry wont display, as the lookahead breaks out of the loop)

 

[/code]

for($l=17;$i<34;$i++)

{

  if(i!sset($slot[$i+1])) break;

  echo $slot[$l];

}

[/code]

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.