Solved

PHP - For Loop Question

Posted on 2016-09-12
3
42 Views
Last Modified: 2016-09-12
Hi'

Just researching how 'For loops' behave and I wrote the following ...
<?php

$t = 0;
    
    for($i=1, $j=1; $i<=3, $j>=-3; $i++, $j--)
        
    {
        
        $t = $i * $j;
        echo "$i x $j = $t<br>";
    
    }
?>

Open in new window


I thought I'd get the following results: -

1 * 1 = 1
2 * 0 = 0
3 * -1 = -3

And yet the following happens: -

1 * 1 = 1
2 * 0 = 0
3 * -1 = -3
4 * -2 = -8
5 * -3 = -15

Why doesn't the for loop stop when the first statement for $i hits 3? Equally, if I change $i to <=12 the loop is affected by $j being a negative int and doesn't go below -3.

Hope that makes sense?
J
0
Comment
Question by:Ridgejp
3 Comments
 
LVL 6

Expert Comment

by:pritaeas
ID: 41793929
According to the manual:

http://php.net/manual/en/control-structures.for.php

"Each of the expressions can be empty or contain multiple expressions separated by commas. In expr2, all expressions separated by a comma are evaluated but the result is taken from the last part."

So basically that would mean your code will run while $j>=-3
1
 
LVL 55

Accepted Solution

by:
Julian Hansen earned 500 total points
ID: 41793959
The comma separation of expressions means each one will be run sequentially after the other - as pritaeas points out - only the last will be evaluated.

This is because there is nothing telling the compiler that you want the evaluation to work on all expressions. To do that you need to use boolean operators

i.e.
for($i=1, $j=1; $i<=3 && $j>=-3; $i++, $j--)

Open in new window


This is inherited from PHP's C roots where you could almost write an entire application inside the parts of a for loop - not always good practice but possible.

By including the && boolean operator you are changing the execution from running a sequential set of instructions and evaluating each on its own (the last man wins) to a Boolean expression that considers the output value of all the sub-expressions.
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 41794028
Footnote: I'm a fairly experienced programmer, and I've never seen anyone do anything quite like what you've posted with this question.  If it's purely academic, I understand.  But if it's going to be part of a deployed application, you might want to post a new question here, and ask about best practices to solve the problem!
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

790 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question