[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

PHP - For Loop Question

Posted on 2016-09-12
3
Medium Priority
?
46 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 59

Accepted Solution

by:
Julian Hansen earned 2000 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 111

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

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…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
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…
The viewer will learn how to dynamically set the form action using jQuery.
Suggested Courses

649 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