php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #26734 (or) and (||) not the same
Submitted: 2003-12-28 18:11 UTC Modified: 2003-12-31 21:53 UTC
Votes:1
Avg. Score:2.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:0 (0.0%)
Same OS:1 (100.0%)
From: red at ixney dot net Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 4.3.4 OS: Irrelevant
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: red at ixney dot net
New email:
PHP Version: OS:

 

 [2003-12-28 18:11 UTC] 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

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2003-12-28 21:37 UTC] 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 22:14 UTC] 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-31 21:10 UTC] 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-31 21:53 UTC] alan_k@php.net
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)

 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Sep 12 04:01:28 2024 UTC