Solved

PHP - For Loop Question

Posted on 2016-09-12
3
37 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 51

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 108

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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Introduction Many web sites contain image galleries; a common design for these galleries includes a page with a collection of thumbnail images.  You can click on each of the thumbnail images to see the larger version of the image.  This is easily i…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

747 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now