User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the PHP section within the Web Development category of DaniWeb, a massive community of 456,196 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,924 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our PHP advertiser: Lunarpages PHP Web Hosting
Views: 562 | Replies: 5 | Solved
Reply
Join Date: Jul 2006
Location: Deptford, London
Posts: 971
Reputation: MattEvans has a spectacular aura about MattEvans has a spectacular aura about 
Rep Power: 5
Solved Threads: 48
Moderator
Featured Poster
MattEvans's Avatar
MattEvans MattEvans is offline Offline
Posting Shark

PHP4: stupid loop + and 'optimization'??

  #1  
May 5th, 2007
Consider the following code:

  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>
  43.  

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

  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.
Plato forgot the nullahedron..
AddThis Social Bookmark Button
Reply With Quote  
Join Date: May 2006
Location: Bellevue, WA
Posts: 1,548
Reputation: Infarction has a spectacular aura about Infarction has a spectacular aura about Infarction has a spectacular aura about 
Rep Power: 8
Solved Threads: 51
Sponsor
Infarction's Avatar
Infarction Infarction is offline Offline
Battle Programmer

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

  #2  
May 5th, 2007
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.
Reply With Quote  
Join Date: Jul 2006
Location: Deptford, London
Posts: 971
Reputation: MattEvans has a spectacular aura about MattEvans has a spectacular aura about 
Rep Power: 5
Solved Threads: 48
Moderator
Featured Poster
MattEvans's Avatar
MattEvans MattEvans is offline Offline
Posting Shark

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

  #3  
May 5th, 2007
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.
Plato forgot the nullahedron..
Reply With Quote  
Join Date: Jun 2006
Location: India
Posts: 7,012
Reputation: ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold 
Rep Power: 25
Solved Threads: 368
Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Lazy, Useless & Apathetic

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

  #4  
May 6th, 2007
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.
I don't accept change. I don't deserve to live.

Happiness corrupts people.

Failing to value the lives of others cheapens your own.
Reply With Quote  
Join Date: Jul 2006
Location: Deptford, London
Posts: 971
Reputation: MattEvans has a spectacular aura about MattEvans has a spectacular aura about 
Rep Power: 5
Solved Threads: 48
Moderator
Featured Poster
MattEvans's Avatar
MattEvans MattEvans is offline Offline
Posting Shark

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

  #5  
May 6th, 2007
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:
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.
Plato forgot the nullahedron..
Reply With Quote  
Join Date: May 2006
Location: Bellevue, WA
Posts: 1,548
Reputation: Infarction has a spectacular aura about Infarction has a spectacular aura about Infarction has a spectacular aura about 
Rep Power: 8
Solved Threads: 51
Sponsor
Infarction's Avatar
Infarction Infarction is offline Offline
Battle Programmer

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

  #6  
May 7th, 2007
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).
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb PHP Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the PHP Forum

All times are GMT -4. The time now is 3:49 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC