#15438 [Com]: include_once fails when comparing output to a value
| From: | m dot maika at charter dot net | Date: | Tue, 08 Apr 2003 02:08:35 +0000 |
| Subject: | #15438 [Com]: include_once fails when comparing output to a value | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-37320@lists.php.net to get a copy of this message | ||
ID: 15438
Comment by: m dot maika at charter dot net
Reported By: michael at gamepoint dot net
Status: Verified
Bug Type: Scripting Engine problem
Operating System: FreeBSD/Linux
PHP Version: 4CVS-2002-10-14
New Comment:
I'm think I know the cause, don't know the fix though. From reading
and trying the examples it seems to be a precedence problem.
include_once is running on the return value of the boolean operation.
Similar to the reason why this doesn't work:
$i=0;
while ($row = mysql_fetch_object($result) && $i <= 10){
There, $row would be a true/false value because the && operator has a
higher precedence than = and is executed first. I'm almost positive
that this is the reason that include_once is failing when doing the
comparison.
Here you can see an example:
====t.php====
<?php
if (include_once('s.php')==true){
echo 'It was included';
}
?>
==============
====s.php=====
<?php
echo 'This file should be included';
?>
==============
(Name file file 1, no extension)
====1=========
<?php
echo 'This file is included, when it was never mentioned.';
?>
==============
Results of running:
[kicken@dell ~]$
php -q t.php
This file gets included, when it was never mentionedIt was included
All look at the php source, but I don't have high hopes for figure out
how to fix it. Thought I'd mention this fact though incase anyone does
know how to fix it.
Tested on:
PHP 4.3.0 (cgi), Copyright (c) 1997-2002 The PHP Group
Zend Engine v1.3.0, Copyright (c) 1998-2002 Zend Technologies
and
PHP 4.3.2-RC1 (cli) (built: Apr 7 2003 18:50:44)
Copyright (c) 1997-2003 The PHP Group
Zend Engine v1.3.0, Copyright (c) 1998-2003 Zend Technologies
Previous Comments:
------------------------------------------------------------------------
[2002-12-10 11:16:28] rogierbiba at hotmail dot com
Because michael did mention this bug again...
mfisher already did describe what happens when you use
var_dump(include_once("someinclude.inc")) twice.
So what's only left is the weird behaviour from the
if(include_once("test.php") == false) or == true.
when adding a few ()'s it works as expected.
if((include_once("test.php"))== $bool){
echo("file test.php included");
}
when $bool is 'true', it will echo the line, if $bool is 'false' it
will not echo the line.
if the file is not found it will produce a warning, and it will display
the line when $bool is 'false'.
In any case, adding the () around the include() will make the strange
Warning: Failed opening '' for inclusion, or Warning: Failed opening
'1' for inclusion, go away.
------------------------------------------------------------------------
[2002-12-09 06:25:34] andi@php.net
I still don't understand what the problem is. includee() and friends
are supposed to return whatever you return from within the included
file. It's not supposed to return true or false.
If you have a concrete reproducing script which doesn't work the way I
mentioned please post it and email it to me (andi@zend.com)
------------------------------------------------------------------------
[2002-10-14 17:51:04] mfischer@php.net
> Updated Version
The include*/require* functions are a bit inconsistent.
In the simple tests done, the filename printed after "Running" is the
statement included in the source and executed by PHP, e.g.
var_dump(require("file2include"));
Every script ran exeutes the same statement twice in a row.
First tests done by including the following:
<?php
echo "File got included\n";
?>
--> Running include.php
File got included
int(1)
File got included
int(1)
--> Running include_once.php
File got included
int(1)
bool(true)
--> Running require.php
File got included
UNKNOWN:0
File got included
UNKNOWN:0
--> Running require_once.php
File got included
int(1)
bool(true)
Same tests ran with:
<?php
echo "File got included\n";
return "Returning a string";
?>
--> Running include.php
File got included
string(18) "Returning a string"
File got included
string(18) "Returning a string"
--> Running include_once.php
File got included
string(18) "Returning a string"
bool(true)
--> Running require.php
File got included
UNKNOWN:0
File got included
UNKNOWN:0
--> Running require_once.php
File got included
string(18) "Returning a string"
bool(true)
------------------------------------------------------------------------
[2002-10-08 06:29:23] cynic@php.net
confirmed with 4.4.0-dev (cli)/ ZE 1.4.0. i'm including a repro recipe
# This is a shell archive. Save it in a file, remove anything before
# this line, and then unpack it by entering "sh file". Note, it may
# create directories; files and directories will be owned by you and
# have default permissions.
#
# This archive contains:
#
# foo.php
# bar.inc
#
echo x - foo.php
sed 's/^X//' >foo.php << 'END-of-foo.php'
X#!/usr/bin/env php
X<?
X
X if (include('bar.inc') == true) {
X print "1. ok\n";
X }
X
X if (true == include('bar.inc')) {
X print "2. ok\n";
X }
X
X $rv = include('bar.inc');
X if (true == $rv) {
X print "3. ok\n";
X }
END-of-foo.php
echo x - bar.inc
sed 's/^X//' >bar.inc << 'END-of-bar.inc'
X<?
X print "Hello world\n";
X
END-of-bar.inc
exit
------------------------------------------------------------------------
[2002-10-08 06:02:46] michael at gamepoint dot net
I already forgot about this bug, but received the email today that this
was considered bugus.
Well i'm surprised, and tested it a few times more (with a newer php
version), and i'm still convinced that this is a bug.
A few new examples:
<?
if( include("include.txt") == true )
{
print "File included\n";
}
?>
The code above returns this fatal error:
Warning: Failed opening '1' for inclusion
(include_path='.:/usr/local/lib/php') in
/usr/local/apache/htdocs/test.php on line 2
And now i do it the longer way:
<?
$status = include("include.txt");
if( $status == true )
{
printf("File included!\n");
}
?>
Works perfectly
And another way that works:
<?
if( true == include("include.txt") )
{
print "File included\n";
}
?>
The problem really seems what derick@php.net said, a scripting engine
bug.
PS. Your reply about me asking a support question here is rather weird
to me, i was just reporting a bug..
------------------------------------------------------------------------
The remainder of the comments for this report are too long. To view
the rest of the comments, please view the bug report online at
http://bugs.php.net/15438
--
Edit this bug report at http://bugs.php.net/?id=15438&edit=1