php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #15438 include_once fails when comparing output to a value
Submitted: 2002-02-07 17:21 UTC Modified: 2004-07-27 21:54 UTC
Votes:5
Avg. Score:3.6 ± 0.8
Reproduced:4 of 5 (80.0%)
Same Version:2 (50.0%)
Same OS:4 (100.0%)
From: michael at gamepoint dot net Assigned:
Status: Closed Package: Documentation problem
PHP Version: 4.3.2RC4-dev OS: FreeBSD/Linux
Private report: No CVE-ID: None
 [2002-02-07 17:21 UTC] michael at gamepoint dot net
When trying to check if include_once succeeded i came up the following bug (i believe):

if( include_once("test1.php") == false ) {
   print "Failed to include file";
}

Warning: Failed opening '' for inclusion (include_path='.:/usr/local/lib/php') in /usr/home/michael/www/bug.php on line 6

Removing '== false' removes the problem.

My configuration is:
FreeBSD 4.5
Apache 1.3.22
PHP 4.1.1

Configure options:  './configure' '--with-mysql' '--with-apxs'

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2002-02-08 09:48 UTC] sander@php.net
RTM! You can't include_once check for succes on include/include_once. include(_once) is not a function.
 [2002-02-08 10:51 UTC] derick@php.net
Actually you can check if an include failed or not like this, but I think the parser gets confused if you use the '== false'.
Making this a scripting engine problem.

Derick
 [2002-02-09 06:17 UTC] some at gamepoint dot net
I'm not sure this is actually a bug. I gather the returned value from an include is the one you specify in the included file with 'return $var'
Read more at http://www.php.net/manual/en/function.include.php about return() in included files.

If you want to evaluate if an include was succesfull you could add:

$var = true;
return $var

at the end of the included file, but this should also do the trick:

((int) @include_once("../lib/test.php")) or die("Don't exist");

which is probably just a check if there's some warning text just like michael already demonstrated.
 [2002-10-07 22:17 UTC] iliaa@php.net
Sorry, but the bug system is not the appropriate forum for asking
support questions. Your problem does not imply a bug in PHP itself.
For a list of more appropriate places to ask for help using PHP,
please visit http://www.php.net/support.php

Thank you for your interest in PHP.

You can only check if include/include_once failed or succeeded if the included file returns a value. This is clearly documented at:
http://www.php.net/manual/en/function.include.php
 [2002-10-08 06:02 UTC] 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..
 [2002-10-08 06:29 UTC] 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-14 17:51 UTC] 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-12-09 06:25 UTC] 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-12-10 11:16 UTC] 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.
 [2003-05-18 13:21 UTC] sniper@php.net
Simple explanation for Andi:

This does not work:

if (include('bar.inc') == true) {
  print "1. ok\n";
}

But this does:

if ((include('bar.inc')) == true) {
  print "1. ok\n";
}

Bug or just documentation issue?

 [2003-05-18 16:31 UTC] derick@php.net
Just a doc problem... (afaik we came to this conclusion before...)
 [2004-05-20 17:46 UTC] sean@php.net
it seems that this is actually happening because the following statement:
if (include_once('file') == FALSE)
is actually being parsed as:
if (include_once ('file' == FALSE))
becasue include_once isn't a true function.

Is that what the docs should say?

S

 [2004-05-20 17:56 UTC] nlopess@php.net
I would just mark this bug as Won't fix.
 [2004-07-27 21:54 UTC] vrana@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.

"Because include() is a special language costruct, parentheses are not needed around its argument. Take care when comparing return value.
<?php
// won't work, evaluated as include(('vars.php') == 'OK'), i.e. include('')
if (include('vars.php') == 'OK') {
    echo 'OK';
}

// works
if ((include 'vars.php') == 'OK') {
    echo 'OK';
}
?>

 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 26 15:01:56 2024 UTC