-3

I am looking to see if there is a smart or optimal way to try and do this for loop which so far I cannot get it to work.

I am trying to do it this way to stop me repeating code however if its not possible then I will just have to repeat code.

Here is what I tried

if(!isset($staff_rows)
for ($i=0; $i < 5; $i++)
else
for ($i=0; $i < $staff_rows; $i++)

So basically I want my forloop to use a variable $staff_rows if it is set otherwise just use default value of 5.

This is the error message

Parse error : syntax error, unexpected T_FOR in

Any ideas?

Thanks

sqlmole
  • 997
  • 7
  • 17
  • 31

5 Answers5

4
$min = (isset($staff_rows)) ? $staff_rows : 5 ;
for ($i=0; $i < $min; $i++) {
  //loop logic
}
Luigi
  • 866
  • 13
  • 34
4

the simplest way.

        $staff_rows = 5;

        // your code to change staff_rows 

        for ($i=0; $i <  $staff_rows; $i++) {
          // do something
        }
blejzz
  • 3,349
  • 4
  • 39
  • 60
1
for($i=0,$s=(isset($staff_rows)?$staff_rows:5);$i<$s;++$i) //do

Added:
More about ternary operator:
What is ?: in PHP 5.3?

Some benchmark.Manual page in gion_13's answer

$array=range(1,1000000);
$i=17;

$start= microtime(true);
$s=sizeof($array);
for($i=0;$i<$s;++$i){
;
}
print microtime(true)-$start;
print chr(10);



$start= microtime(true);
for($i=0,$s=sizeof($array);$i<$s;++$i){
;
}
print microtime(true)-$start;
print chr(10);

$start= microtime(true);
for($i=0;$i<sizeof($array);++$i){
;
}
print microtime(true)-$start;
print chr(10);

Output

0.18430280685425
0.1843409538269
1.3922038078308
Community
  • 1
  • 1
RiaD
  • 46,822
  • 11
  • 79
  • 123
1

Rewrite your code to be the following:

$limit = 5;
if (isset($staff_rows)) {
    $limit = $staff_rows;
}
for ($i=0; $i < $limit; $i++) { }

Your code will also be clearer with braces. Also you were missing a closing parentheses in the code sample that was in your question.

Matty
  • 33,203
  • 13
  • 65
  • 93
0

besides the fact that you have a missing ")", you can do a single for loop:

for($i=0,$l=isset($staff_rows)?$staff_rows:5;$i<$l;$i++){
    // do your thing
}

it is efficient because you use only one for-loop.
The fact that I used logic($l=isset($staff_rows)?$staff_rows:5;) in my loop does not affect its execution... but don't take my word for it :
From the php.net docs :

for (expr1; expr2; expr3)
    statement

"The first expression (expr1) is evaluated (executed) once unconditionally at the beginning of the loop."

AlienWebguy
  • 76,997
  • 17
  • 122
  • 145
gion_13
  • 41,171
  • 10
  • 96
  • 108
  • @Alien if you can't understand this code, it'snot means that code is bad – RiaD Aug 06 '11 at 15:36
  • @Matt: Please think about what you writing first. This site is sh1t. – Micromega Aug 06 '11 at 15:44
  • I have not selected this function although it is clever - (a) because it does not seperate the logic of two seperate functions easily and is (b) would be very hard to debug this is there was errors in value etc – sqlmole Aug 06 '11 at 15:52