0

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';
}
2
  • 1
    And the value of $foo is? Commented Sep 11, 2015 at 15:17
  • 3
    and probably file 'bar.php' really not exists? :) or you need setup right path to this file... Commented Sep 11, 2015 at 15:18

1 Answer 1

1

Normally I don't like wrapping but try restructuring like this. This way you have a better idea of what's failing

if(isset($foo)) {
    if(file_exists('bar.php')) {
        echo '1';
    } else {
        echo '2';
    }
} else {
    echo '3';
}
Sign up to request clarification or add additional context in comments.

5 Comments

Maybe I'm missing something, but does the restructuring actually help / answer the question?
Wrapping should make it more obvious where the problem is (which seems to be in the file_exists portion)
Probably should be a comment then, but given it's a bit big for a comment, fair doos. :)
Thanks a lot buddy, I found the problem, restructuring did help :)
@JonStirling no restructuring advice is not the answer to the question but an advice to lead to the answer and it did help me narrow down to the problem area, otherwise I wasted all my time thinking I was not using conditional statements correctly, whereas the problem lied somewhere else. I would encourage such advice, and yes having code in an answer is better then in a comment.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.