0

I have a function which is returning some values. I want to put those values in an array after checking if the current value exists. I 've written the following code:

$return[0]=myexec_proc($varsearch,$get_input1);

if (isset($return[0])){
$return[1]=myexec_proc($varsearch,$return[0]);
}
if (isset($return[1])){
$return[2]=myexec_proc($varsearch,$return[1]);
}
if (isset($return[2])){
$return[3]=myexec_proc($varsearch,$return[2]);
}
if (isset($return[3])){
$return[4]=myexec_proc($varsearch,$return[3]);
}

which works as I want to but I need to do it with a for loop.
I've tried this:

$return=array();

for($i=0; $i=3; $i++){
if (isset($return[$i])){
$return[$i+1]=myexec_proc($varsearch,$return[$i]);
}}

but I get no data and after a while I get a php fatal error "Maximum execution time of 30 seconds exceeded".
Any tips on what I am doing wrong would be appreciated.

3
  • That's the wrong format for a for loop; you need for($i=0; $i<=3; $i++){ - yours will loop infinitely, as you noticed.
    – andrewsi
    Commented Jul 16, 2013 at 16:34
  • thanks, it worked. I thought both ways were right so I hadn't checked the one you wrote. :S
    – georgia
    Commented Jul 16, 2013 at 16:44
  • It's an easy mistake to make - you'd think PHP would flag it as an error, but apparently it's valid syntax. I just hope never to come across code where that functionality is required....
    – andrewsi
    Commented Jul 16, 2013 at 16:46

2 Answers 2

0

The second condition of your for loop is incorrect. You are assigning $i to 3 instead of checking it as a conditional.

It should be something like this:

    for($i=0; $i<=3; $i++){
        if (isset($return[$i])){
            $return[$i+1]=myexec_proc($varsearch,$return[$i]);
        }
    }
0

For loops require a condition which they loop up until. Add a less than operator to make the for run correctly.

for ($i=0; $i<=3; $i++) {
  if (isset($return[$i])) {
    $return[$i+1]=myexec_proc($varsearch,$return[$i]);
  }
}

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.