Syntax Strange for loop

I have been programming php for about 2 years now.

I just stumbled upon 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 should work.

Can someone explain this to me?

+5
source share
3 answers

Late, but no one seemed to cache this: this is equivalent for a loop:

 $i = 1; 

Why? Since in terms of the for loop condition you have 3 conditions related to AND :

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

In the first iteration, $i++ evaluates to 0, which is equivalent to false and only increases after. Thus, the loop stops only after one iteration, and $i is 1, and you have an error. If this is not a typo in your code ...

+3
source

This is just a normal three-section cycle without its main operator and an empty third part.

From manual :

 for (expr1; expr2; expr3) statement 

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

At the beginning of each iteration, expr2 is expr2 . If it evaluates to TRUE, the loop continues and the nested statement (s) is executed. If it evaluates to FALSE, 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 expr2 is true, the loop continues. Of course, there is no instruction or block to execute, so it simply proceeds to the next iteration, that is, expr2 will be executed several times until at some point false is considered.

As R. Chappell noted in the comments, this is likely to find a position in the line. You can rewrite this using 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 
+7
source

This is another example (not an answer as such) of using for for without a third statement. This is 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 repeat it, and this is only possible using the increment operator in PHP.

First, set $i to zero;

Secondly, we check and increase $i to make sure that it is equal to or greater than itself when it is less than or equal to 10.

Thirdly, we do nothing ... it makes no sense ...

However, ordinary people will write the same thing as:

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

You have to imagine the best use case, and yes, you can just do $i = 10; but this does not go to the explanation of the question.

+2
source

Source: https://habr.com/ru/post/1260293/


All Articles