php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #41228 Side-effects for logical operators
Submitted: 2007-04-29 18:29 UTC Modified: 2007-04-30 17:02 UTC
From: terminatorul at gmail dot com Assigned: colder (profile)
Status: Closed Package: Documentation problem
PHP Version: Irrelevant OS:
Private report: No CVE-ID: None
 [2007-04-29 18:29 UTC] 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.

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-04-30 09:00 UTC] rquadling@php.net
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-30 10:08 UTC] arpad@php.net
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 11:42 UTC] rquadling@php.net
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 11:44 UTC] rquadling@php.net
Good ole MS! http://support.microsoft.com/kb/817250

 [2007-04-30 11:50 UTC] terminatorul at gmail dot com
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.
 [2007-04-30 12:03 UTC] arpad@php.net
My point was that the term "short-circuit" is already mentioned twice on the page, if you didn't know what it meant in this context then a google search for "short-circuit operator" would yield a page full of relevant results.

Of course it could be spelled out in the text, but even if I was in favour of doing so, I don't have phpdoc karma. That's why I just stated my opinion and left the bug open.
 [2007-04-30 17:02 UTC] colder@php.net
This bug has been fixed in the documentation's XML sources. Since the
online and downloadable versions of the documentation need some time
to get updated, we would like to ask you to be a bit patient.

Thank you for the report, and for helping us make our documentation better.


 [2020-02-07 06:10 UTC] phpdocbot@php.net
Automatic comment on behalf of colder
Revision: http://git.php.net/?p=doc/en.git;a=commit;h=35a5e1fa5ddc67b766d11565f0c6e0938570c48f
Log: Fix #41228 (Mention short-circuit property of some logical operators)
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Wed Nov 26 08:00:01 2025 UTC