php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #74349 cannot use include return string in OR condition
Submitted: 2017-03-31 12:24 UTC Modified: 2017-03-31 12:27 UTC
From: liviu dot gelea at gmail dot com Assigned:
Status: Not a bug Package: Filesystem function related
PHP Version: 7.1.3 OS: Windows 10
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
MUST BE VALID
Solve the problem:
38 - 16 = ?
Subscribe to this entry?

 
 [2017-03-31 12:24 UTC] liviu dot gelea at gmail dot com
Description:
------------
When "returning" a string from a file, the result behaves differently if used in a condition containing the  || operator

setup an "environment.php" file that contains:

<?php 
return "production";

then run the script attached:

Test script:
---------------
<?php

// determine application environment
$environmentFile = 'environment.php';
if (!file_exists($environmentFile)) {
    $environment = 'development';
} elseif ($environment = include $environmentFile) {
    // do nothing
} else {
    $environment = 'development';
}

echo $environment . " ";

if (!file_exists($environmentFile) || ($environment = include $environmentFile)) {
    $environment = 'development';
}

echo $environment;

Expected result:
----------------
the script should echo the string
"production production"

Actual result:
--------------
"production development"

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2017-03-31 12:27 UTC] requinix@php.net
-Status: Open +Status: Not a bug
 [2017-03-31 12:27 UTC] requinix@php.net
>|| ($environment = include $environmentFile)
Your logic is backwards.
 [2017-03-31 12:33 UTC] kelunik@php.net
Your logic should be:

if (!file_exists($environmentFile) || !($environment = include $environmentFile))
                                      ^
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 19 08:01:28 2024 UTC