#26734 [Bgs]: (or) and (||) not the same
| From: | alan_k@php.net | Date: | Thu, 01 Jan 2004 02:53:56 +0000 |
| Subject: | #26734 [Bgs]: (or) and (||) not the same | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-52647@lists.php.net to get a copy of this message | ||
ID: 26734
Updated by: alan_k@php.net
Reported By: red at ixney dot net
Status: Bogus
Bug Type: Scripting Engine problem
Operating System: Irrelevant
PHP Version: 4.3.4
New Comment:
Yeap - I was reading the engine source - as that is what they are
called there..
correct - the real difference is the precidence as documented pretty
well in the manual (and comments)
Previous Comments:
------------------------------------------------------------------------
[2003-12-31 21:10:40] elmicha@php.net
"boolean" and "logical" operators are the same thing (probably you
thought of bitwise operators like "|"). The difference between "||" and
"or" is the precedence.
<http://www.php.net/manual/en/language.operators.logical.php>
<http://www.php.net/manual/en/language.operators.php#language.operators.precedence>
------------------------------------------------------------------------
[2003-12-28 22:14:16] alan_k@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to
report
a bug at http://bugs.php.net/how-to-report.php
|| is boolean or
'or' is logical or
they are not the same....
------------------------------------------------------------------------
[2003-12-28 21:37:58] eru@php.net
Nothing to do with Mysql, but more like a parser problem
<?php
$handle1 = fopen("test.php", "r") or die("fopen failed");
$handle2 = fopen("test.php", "r") || die("fopen failed");
var_dump($handle1, $handle2);
// yields resource(stream) / bool(true)
?>
Apparently || casts the resource to bool (true) and assigns it to the
variable. IMHO the || should either produce a parse error in this
context or behave like 'or'.
------------------------------------------------------------------------
[2003-12-28 18:11:51] red at ixney dot net
Description:
------------
I always thought '||' and 'or' was the same.
Combined with a mysql_query() however, they are not.
These lines are the same:
mysql_query("$var") or die("help!");
mysql_query("$var") || die("help!");
where $var is a DROP, INSERT or CREATE query, but they are *not* the
same if $var is a SELECT query; the returned value will not be a
'Resource ID' in the latter line.
Reproduce code:
---------------
mysql_query("DROP TABLE IF EXISTS foobar") || die('Cannot drop table');
mysql_query("CREATE TABLE IF NOT EXISTS foobar (foo INT NOT NULL
AUTO_INCREMENT, bar TINYINT(1), PRIMARY KEY (foo))") || die('Cannot
make table');
// Let's insert some random stuff
mysql_query("INSERT INTO foobar (bar) VALUES (0)") || die('Cannot add
values 1');
mysql_query("INSERT INTO foobar (bar) VALUES (1)") || die('Cannot add
values 2');
mysql_query("INSERT INTO foobar (bar) VALUES (0)") or die('Cannot add
values 3');
mysql_query("INSERT INTO foobar (bar) VALUES (1)") or die('Cannot add
values 4');
$handle1 = mysql_query("SELECT * FROM foobar WHERE 1") || die('Cannot
select');
$handle2 = mysql_query("SELECT * FROM foobar WHERE 1") or die('Cannot
select');
echo '$handle1 is '.$handle1.'<br><br>';
echo '$handle2 is '.$handle2.'<br><br>';
Expected result:
----------------
$handle1 is Resource id #2
$handle2 is Resource id #3
Actual result:
--------------
$handle1 is 1
$handle2 is Resource id #3
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=26734&edit=1