ID:               26734
 Updated by:       [EMAIL PROTECTED]
 Reported By:      red at ixney dot net
-Status:           Open
+Status:           Verified
-Bug Type:         MySQL related
+Bug Type:         Scripting Engine problem
-Operating System: Win XP Pro
+Operating System: Irrelevant
-PHP Version:      4.3.2
+PHP Version:      4.3.4
 New Comment:

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'.



Previous Comments:
------------------------------------------------------------------------

[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

Reply via email to