Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Strange for loop syntax

Tags:

php

for-loop

I've been programming in php for about 2 years now.

I just stumbled into this for loop:

    // Check the URI namespace for a context
    $wsDir = basename(dirname(__FILE__));
    $uriArr = explode("/", $_SERVER['REQUEST_URI']);

    for (
        $i = 0, $uriSize = sizeof($uriArr);
        $i < $uriSize && $uriArr[$i] != $wsDir && $i++;
    );

    $i++;
    self::$executionContext = isset($uriArr[$i]) && !empty($uriArr[$i]) && substr($uriArr[$i], 0, 1) != '?'
        ? strtoupper($uriArr[$i]) : 'SOAP';

and I have no idea how this is supposed to work.

Can someone explain this to me ?

like image 769
Vasil Rashkov Avatar asked Nov 24 '16 16:11

Vasil Rashkov


3 Answers

It is just a normal three-part for loop without its main statement and an empty third part.

From the manual:

for (expr1; expr2; expr3)
      statement

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

In the beginning of each iteration, expr2 is evaluated. If it evaluates to TRUE, the loop continues and the nested statement(s) are executed. If it evaluates to FALSE, the execution of the loop ends.

At the end of each iteration, expr3 is evaluated (executed).

So:

for (
    # initializes two variables
    $i = 0, $uriSize = sizeof($uriArr);

    # conditional, expr2
    $i < $uriSize && $uriArr[$i] != $wsDir && $i++;

    # no expr3
);

If the expr2 evaluates to true the loop continues. Of course there is no statement or block to execute, so it just jumps to the next iterarion, meaning expr2 will be executed repeatedly until it evaluates to false at some point.

As pointed out by R. Chappell in the comments, this is probably to find a position in a string. You could rewrite this with a similar logic but in a more "descriptive" way:

$uriSize = sizeof($uriArr)

for ($i = 0; $i < $uriSize; $i++) {
    if ($uriArr[$i] == $wsDir) break;
}

# now $i will be the index of the first $wsDir occurrence in the $uriArr array
like image 68
sidyll Avatar answered Nov 20 '22 09:11

sidyll


Coming late, but none seems to have cached this : this for loop is equivalent to :

$i = 1;

Why ? Because in the condition part of the for loop, you have 3 conditions that are bound with AND:

$i < $uriSize 
&&
$uriArr[$i] != $wsDir 
&&
$i++;

In the first iteration, $i++ evaluates to 0 which is equivalent to false, and is incremented only after. So the loop stops after only one iteration, and $i is 1, and you have a bug. Unless it's a typo in your code...

like image 42
Zimmi Avatar answered Nov 20 '22 09:11

Zimmi


This is another example (not an answer as such) of using a for without a third statement. It's a little clearer than the original question.

for ($i=0; $i >= $i++ && $i <= 10; /* third statement */ );
echo $i;

This will basically count to 10 and echo it out, and it's only made possible with the increment operator in PHP.

First we set $i to zero;

Second we check and increment $i to ensure it's equal to or greater than itself whilst less than or equal to 10.

Third we do nothing... no point really...

However, normal people would write the same thing as:

for ($i = 0; $i <= 10; $i++);
echo $i;

You'll have to imagine a better use case though and yes you can just do $i = 10; but it doesn't go as far as to explaining the question.

like image 2
R. Chappell Avatar answered Nov 20 '22 11:11

R. Chappell