ID: 41228 User updated by: terminatorul at gmail dot com Reported By: terminatorul at gmail dot com Status: Open Bug Type: Documentation problem PHP Version: Irrelevant New Comment:
My point exactly. A programing language may or may not guarantee side-effects from evaluation of operands to logical operators. Or at least would say that it is undefined if the side-effect occurs. The proffesional C++ programmer will find it normal for the language to shortcut the expression, while a beginner will not understand what's wrong with his/her code. Previous Comments: ------------------------------------------------------------------------ [2007-04-30 11:44:35] [EMAIL PROTECTED] Good ole MS! http://support.microsoft.com/kb/817250 ------------------------------------------------------------------------ [2007-04-30 11:42:27] [EMAIL PROTECTED] Two opposing thoughts. Delphi - http://info.borland.com/techpubs/delphi/delphi5/oplg/syntlmnt.html#10548 In Delphi, shortcut/short-circuit is optional, though as standard it is enabled. VB - http://www.informit.com/guides/content.asp?g=dotnet&seqNum=480&rl=1 Seemingly VB does NOT have a short circuit. Who do you believe. For the seasoned programmer, this is perfectly obvious. But if you're upgrading your skill set from, say, VB, then this REALLY odd behaviour is completely new. And what debugging would you provide? This should be documented and probably in several places as there are many references to boolean logic throughout the manual. It is the LACK of documentation on this standard feature within PHP that is the issue. ------------------------------------------------------------------------ [2007-04-30 10:08:31] [EMAIL PROTECTED] This isn't a side-effect, but an intrinsic property of these operators. As two out of the four user notes on the page mention, the first dating back to 1999, these operators short-circuit. Perhaps this could be explained in the text to make it clearer for people new to programming, but I don't think there's any great need; I assume most people can look up terms they're not familiar with. ------------------------------------------------------------------------ [2007-04-30 09:00:14] [EMAIL PROTECTED] Agreed. <?php // Echo's F and does NOT set $a. echo (False && ($a=1)) ? 'T' : 'F'; // Echo's T, sets $b but does not set $c. echo (($b=2) || False || ($c=3)) ? 'T':'F'; // Turn off notices about undefined variables for $a and $c. error_reporting(E_ALL & ~E_NOTICE); // Rather than "1 2 3", we get just " 2 ". echo "$a $b $c"; ?> ------------------------------------------------------------------------ [2007-04-29 18:29:26] terminatorul at gmail dot com Description: ------------ The manual page for logical operators http://www.php.net/manual/en/language.operators.logical.php should warn users that the right-hand operand of && or || will never be evaluated if that is not necesary For example when doing: $result = FALSE && mysql_query($query); than the mysql $query will never get executed. Or at least the page should advise users to never rely on side-effects. Reproduce code: --------------- http://www.php.net/manual/en/language.operators.logical.php Expected result: ---------------- The page should say something like: For the binary logical operators the second operand will never be evaluated when it is clear from the first operand what the result will be. For example the code $prev_status = FALSE; $crt_status = $prev_status && mysql_query($query); echo mysql_affected_rows(); will never execute the query, simply because $crt_status will be FALSE no matter what mysql_query() would return. Actual result: -------------- Right now I can see nothing about side-effects in the page. ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/?id=41228&edit=1
