Solved

PHP - For Loop Question

Posted on 2016-09-12
3
44 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 57

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 110

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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
This article discusses how to implement server side field validation and display customized error messages to the client.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

691 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