Reputation: 53
$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 ( ) ?
Upvotes: 3
Views: 382
Reputation: 2229
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.
Upvotes: 2
Reputation: 1282
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.
Upvotes: 0
Reputation: 57121
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");
}
Upvotes: 0