3
$file= @fopen("ssssss.php", r) or die("not found");
(@include("ssssss.php")) or die("not found");

In the first statement we don't put ( ) around @fopen and it is working fine. But in the second if I didn't put these () it does't show any message.

so why with include I must round it with ( ) ?

3 Answers 3

2

I agree with the suggestions in the other answers but the actual answer to your question is this:

In the PHP documentation they say to take care when comparing the return value of include.

That's because it is a special construct and parentheses are not needed.

So when you do this (without wrapping parentheses):

@include("ssssss.php") or die("not found");

You're actually doing this, because or is evaluated first:

@include (("ssssss.php") or die("not found"));

Now, "ssssss.php" is a non empty string that evaluates logically to true.

or is a logical operator that gives true if any of the parameters is true (or both of them). Also, this operator is short-circuit: if the first parameter is true, php already knows that the operator or will return true, so it doesn't waste time evaluating the second parameter, and die() is not executed.

So finally, or gives true and your sentence becames this:

@include (1);

Php tries to "include 1", and it would raise a warning but it does not because of the @.

Here you have a similar example in php.net.


Your first sentence is not the same case.

$file= @fopen("ssssss.php", r) or die("not found");

fopen is just a regular Php's function with its parentheses. Here you need to have in mind two operators: = and or.

= has higher precedence than or, so, if fopen's result is correctly assigned to $file (and it is), that operation will return true. And, as I explained before, "true or anything else", gives true but die() is not executed because of the short-circuit operator.

Sign up to request clarification or add additional context in comments.

5 Comments

many thanks @nanocv. I see now . but why it becomes '@include(1)' ?
but the same concept can apply to the first code too!! : $file= @fopen( ("ssssss.php", r) or die("not found") ); but it doesn't !!!
You're welcome @Mahmoud! I edited my answer to explain your questions in both comments (for the record). Hope it helps!
that is wonderful. Great answer . I'm so grateful . last question does , "ssssss.php" evaluates logically to true always ? even this file doesn't exist ? doesn't that seem little weird ?!!
Yes, always. In that case, php doesn't see a file name, it just sees a string. If you try to do this if("hello.php"), the condition gives true always. Strings only evaluate to false when they are empty or "0". Glad to help!
0

You should be using file_exists instead of using the @ as the later covers all sorts of issues. A better solution would be...

if (file_exists("ssssss.php")) {   
    $file= @fopen("ssssss.php", r);                       
}

and

if (file_exists("ssssss.php")) {   
   include("ssssss.php");                       
}

2 Comments

thanks Nigel . the 2 statements aren't related at all . they are seperate. the only thing that related is the error handling . that was my question . why I have to use () with include and the other not !
There are a lot of things in PHP which can be unpredictable and 'odd'. But using include or die logic is just bad practice. IMHO using die anywhere is bad practice and you should put more effort into good practice than trying to work out why this is odd.
0

That's not really a good use of include. If you need to include a php file and generate an error on failure, use require or require_once.

If you need to get the contents of the whole file, you could use file_get_contents().

Also, I agree with Nigel Ren about the use of @ - it is a dangerous practice and should be avoided.

1 Comment

Thanks Rich, I wasn't concern on getting the content of the file. I'm just want to know the difference between the 2 statements in error handling. I noticed that Include use special way so I want to figure the reason behind that

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.