Solved

PHP - For Loop Question

Posted on 2016-09-12
3
43 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 56

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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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…

749 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