Jump to content

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]

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.