943,969 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Marked Solved
  • Views: 1051
  • PHP RSS
May 5th, 2007
0

PHP4: stupid loop + and 'optimization'??

Expand Post »
Consider the following code:

PHP Syntax (Toggle Plain Text)
  1. <?php
  2.  
  3. function important_function( $i )
  4. {
  5. printf ("<p>Called for the %sth time.</p>\n", $i);
  6. return ($i < 3);
  7. }
  8.  
  9. function loop_with_and( )
  10. {
  11. $ret = true;
  12. for( $i = 0; $i < 10; $i ++ )
  13. {
  14. $ret = $ret && important_function( $i );
  15. }
  16. return $ret;
  17. }
  18.  
  19. function loop_without_and( )
  20. {
  21. $ret = true;
  22. for( $i = 0; $i < 10; $i ++ )
  23. {
  24. if( ! important_function( $i ) )
  25. {
  26. $ret = false;
  27. }
  28. }
  29. return $ret;
  30. }
  31.  
  32. ?>
  33.  
  34. <div>
  35. <p><b>Call with And</b></p>
  36. <?php loop_with_and( ) ?>
  37. </div>
  38.  
  39. <div>
  40. <p><b>Call without And</b></p>
  41. <?php loop_without_and( ) ?>
  42. </div>

Can anyone give me a good reason why the output is:

HTML Syntax (Toggle Plain Text)
  1. <div>
  2. <p><b>Call with And</b></p>
  3. <p>Called for the 0th time.</p>
  4. <p>Called for the 1th time.</p>
  5. <p>Called for the 2th time.</p>
  6. <p>Called for the 3th time.</p>
  7. </div>
  8.  
  9. <div>
  10. <p><b>Call without And</b></p>
  11. <p>Called for the 0th time.</p>
  12. <p>Called for the 1th time.</p>
  13. <p>Called for the 2th time.</p>
  14. <p>Called for the 3th time.</p>
  15. <p>Called for the 4th time.</p>
  16. <p>Called for the 5th time.</p>
  17. <p>Called for the 6th time.</p>
  18. <p>Called for the 7th time.</p>
  19. <p>Called for the 8th time.</p>
  20. <p>Called for the 9th time.</p>
  21. </div>

To me, that makes no sense; I didn't instruct the loop to finish on the first encounter of a 'false'; if that's what I'd wanted; I'd have put in a conditional break. All I can assume is that this is some kind of bug (where the loop continuation becomes dependant on the validity/return from an inner statement), or that this might actually be some kind of deliberate loop optimization attempt (which I'd consider to be thoroughly stupid; given that I'm collecting a single validity result from many unrelated , but essential, validation functions )...

Either way; can anyone test the same code on PHP5 for me? My hosting plan uses PHP 4.4.6.
Similar Threads
Moderator
Featured Poster
Reputation Points: 522
Solved Threads: 64
Veteran Poster
MattEvans is offline Offline
1,091 posts
since Jul 2006
May 5th, 2007
1

Re: PHP4: stupid loop + and 'optimization'??

It's just a case of lazy evaluation. if $ret is false, then $ret = $ret && foo() will never evaluate foo. The loop will execute 10 times, but the function call only as long as $ret is true.
Last edited by Infarction; May 5th, 2007 at 5:56 pm.
Reputation Points: 683
Solved Threads: 53
Posting Virtuoso
Infarction is offline Offline
1,580 posts
since May 2006
May 5th, 2007
0

Re: PHP4: stupid loop + and 'optimization'??

Hm.. That's a bit too lazy for my liking.. it certainly took a lot of messing around to diagnose that type of statement as being the error cause!

I thought this might be a PHP thing, but apparently not; the same applies in C++ aswell.

Swapping the two operands to && around seems to have the desired results; in PHP and C++.

Thanks for the info.
Moderator
Featured Poster
Reputation Points: 522
Solved Threads: 64
Veteran Poster
MattEvans is offline Offline
1,091 posts
since Jul 2006
May 6th, 2007
0

Re: PHP4: stupid loop + and 'optimization'??

The thing you experienced is also known as Short Circuit Evaluation. If you want to find a way around it, try using the logical version of Bitwise AND. Something like this:

$ret = $ret & foo(); //notice single &

This way you can be always sure that the second part is evaluated irrespective of the outcome of the first one. Though normally in such situations, the preferred way would be to place the return value of the function in another variable and use it in the real equation.
Last edited by ~s.o.s~; May 6th, 2007 at 3:36 pm.
Super Moderator
Featured Poster
Reputation Points: 3233
Solved Threads: 720
Failure as a human
~s.o.s~ is offline Offline
8,872 posts
since Jun 2006
May 6th, 2007
0

Re: PHP4: stupid loop + and 'optimization'??

Isn't & bitwise and && logical? It is that way in C/++ as far as I know; but is it different in PHP? What about 'and'; is that another way of writing '&', '&&' or something else?

I would think that bitwise would be more likely to work as I'd like it to; since a bitwise AND is always dependant on a myriad of true / falses; thus, the value can never be determined from either operand alone... Saying that; one case : 00..0 bitAnd f() is always going to evaluate to 00..0, and that's a case I want to be fully evaluated..

I wouldn't like to risk unpredictability due to trying to one-line as much as I can; it's already a waste of process to AND on each iteration.

For now, I have gone with if(!validate($i)){$ret=false;} but I suppose I could go with:
PHP Syntax (Toggle Plain Text)
  1. validate( $i ) or ( $ret = false );
Using a short-circuit to my advantage; I can see it more clearly there.

Thanks for reading and replying both. I distrust PHP for some valid reasons, this would have been an invalid one; but finding this nearly drove me to move the related project into another language for want of not crashing into more weirdness.

However; I can't really argue with C++'s interprettation of the same construct.
Moderator
Featured Poster
Reputation Points: 522
Solved Threads: 64
Veteran Poster
MattEvans is offline Offline
1,091 posts
since Jul 2006
May 7th, 2007
0

Re: PHP4: stupid loop + and 'optimization'??

using if(!validate($i)) $ret=false; makes the intent of the code obvious. Hackish code using lazy evaluation for an assignment is very poor style IMHO.

Also, the logical operators and and or are both logical, but have lower precedence than pretty much everything else (here is a list of operator precedences, only the comma is below these).
Reputation Points: 683
Solved Threads: 53
Posting Virtuoso
Infarction is offline Offline
1,580 posts
since May 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: Short guide to include RSS on your website
Next Thread in PHP Forum Timeline: download & extract .tar.bz2 to w/s from a http





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC