•
•
•
•
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
![]() |
•
•
Join Date: Jul 2006
Location: Deptford, London
Posts: 971
Reputation:
Rep Power: 5
Solved Threads: 48
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..
•
•
Join Date: May 2006
Location: Bellevue, WA
Posts: 1,548
Reputation:
Rep Power: 8
Solved Threads: 51
•
•
Join Date: Jul 2006
Location: Deptford, London
Posts: 971
Reputation:
Rep Power: 5
Solved Threads: 48
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.
Happiness corrupts people.
Failing to value the lives of others cheapens your own.
Happiness corrupts people.
Failing to value the lives of others cheapens your own.
•
•
Join Date: Jul 2006
Location: Deptford, London
Posts: 971
Reputation:
Rep Power: 5
Solved Threads: 48
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: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..
•
•
Join Date: May 2006
Location: Bellevue, WA
Posts: 1,548
Reputation:
Rep Power: 8
Solved Threads: 51
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).
![]() |
•
•
•
•
•
•
•
•
DaniWeb PHP Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
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



Linear Mode