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
 [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-2020 The PHP Group
All rights reserved.
Last updated: Wed Dec 02 21:01:25 2020 UTC