Conditional Statement confusion PHP

时间:2015-09-11 15:14:41

标签: php if-statement conditional

EDIT : Damn... It was all about Path to the file and somehow I didn't thought about restructuring :| thanks to @Machavity I found the problem.


What is wrong with this code ?

If $foo is set and file exist file_exist() result should be 1. else if $foo is set but file does not exist file_exist() result should be 2. else result should be 3.

But I am only getting result 2 for all the three conditions. There gotta be something wrong with the second part of the elseif.

if ( isset ( $foo )  && file_exists ( 'bar.php' ) )
{
    echo '1';
}
else if ( isset ( $foo )  &&  ( ! file_exists ( 'bar.php' )  ) )
{
    echo '2';
}
else
{
    echo '3';
}

1 个答案:

答案 0 :(得分:1)

通常情况下,我不喜欢包装,但尝试这样重组。通过这种方式,您可以更好地了解失败的原因

if(isset($foo)) {
    if(file_exists('bar.php')) {
        echo '1';
    } else {
        echo '2';
    }
} else {
    echo '3';
}