| | |
PHP4: stupid loop + and 'optimization'??
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved |
Consider the following code:
Can anyone give me a good reason why the output is:
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.
PHP Syntax (Toggle Plain Text)
<?php function important_function( $i ) { printf ("<p>Called for the %sth time.</p>\n", $i); return ($i < 3); } function loop_with_and( ) { $ret = true; for( $i = 0; $i < 10; $i ++ ) { $ret = $ret && important_function( $i ); } return $ret; } function loop_without_and( ) { $ret = true; for( $i = 0; $i < 10; $i ++ ) { if( ! important_function( $i ) ) { $ret = false; } } return $ret; } ?> <div> <p><b>Call with And</b></p> <?php loop_with_and( ) ?> </div> <div> <p><b>Call without And</b></p> <?php loop_without_and( ) ?> </div>
Can anyone give me a good reason why the output is:
HTML Syntax (Toggle Plain Text)
<div> <p><b>Call with And</b></p> <p>Called for the 0th time.</p> <p>Called for the 1th time.</p> <p>Called for the 2th time.</p> <p>Called for the 3th time.</p> </div> <div> <p><b>Call without And</b></p> <p>Called for the 0th time.</p> <p>Called for the 1th time.</p> <p>Called for the 2th time.</p> <p>Called for the 3th time.</p> <p>Called for the 4th time.</p> <p>Called for the 5th time.</p> <p>Called for the 6th time.</p> <p>Called for the 7th time.</p> <p>Called for the 8th time.</p> <p>Called for the 9th time.</p> </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..
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.
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..
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:
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.
$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.
Jo Tujhe Jagaaye, Nindein Teri Udaaye Khwaab Hai Sachcha Wahi.
Nindon Mein Jo Aaye Jise To Bhul Jaaye Khawab Woh Sachcha Nahi.
Khwaab Ko Raag De, Nind Ko Aag De
Jo Tujhe Jagaaye, Nindein Teri Udaaye Khwaab Hai Sachcha Wahi.
Nindon Mein Jo Aaye Jise To Bhul Jaaye Khawab Woh Sachcha Nahi.
Khwaab Ko Raag De, Nind Ko Aag De
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
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.
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)
validate( $i ) or ( $ret = false );
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..
using
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).
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).
![]() |
Similar Threads
- infinite loop... (C++)
- MySQL query to loop through results distinctly (MySQL)
- C program (not C++) in/out putting a entire sentence (C++)
- Help with loop (C++)
Other Threads in the PHP Forum
- Previous Thread: Short guide to include RSS on your website
- Next Thread: download & extract .tar.bz2 to w/s from a http
Views: 853 | Replies: 5
| Thread Tools | Search this Thread |
Tag cloud for PHP
.htaccess access ajax apache api array beginner binary broken cakephp checkbox class cms code cron curl database date directory display download duplicates dynamic echo email error file files folder form forms function functions google href htaccess html htmlspecialchars image include insert integration ip java javascript joomla limit link links login loop mail menu methods mlm mod_rewrite multiple mysql oop parse paypal pdf php phpvotingscript problem query radio random recursion regex remote replace script search select server session sessions sms soap source space speed sql structure syntax system table tutorial update updates upload url validation validator variable video web xml youtube






