Does anyone know why my require_once () or die(); is not working. It's always shown the Fatal error instead of the error message that I key in into the die(). See below for my code:
require_once ('abc.php') or die("oops");
Error message display as below
"Fatal error: controller::require_once() [function.require]: Failed opening required '1' (include_path='....."
instead of the message ("oops") I key in.
or has a higher precedence than require/require_once. Therefore php evaluates
('abc.php') or die("oops")
before passing the result to require_once. Or takes two boolean operands. ('abc.php') evaluates to true therefore the whole expression is true and
require_once true;
is invoked. require_once takes a string, bool(true)->string => 1 =>
Failed opening required '1'You don't need the or die(...) there. If the file can't be read require_once will stop the php instance anyway.
As include
is a special language construct and not a function, it doesn’t need paranthesis for the paremeter list:
Because
include()
is a special language construct, parentheses are not needed around its argument. Take care when comparing return value.
In fact it has just one parameter and wrapping it in additional parenthesis doesn’t change anything:
1 ≡ (1) ≡ ((1)) ≡ (((1))) ≡ …
So your statement is identical to this (the paremter is just wrapped):
require_once (('abc.php') or die("oops"));
So we have a boolean expression as parameter that is either true or false. And that values have the string equivalent of "1"
and ""
respectively:
var_dump((string) true === "1");
var_dump((string) false === "");
That’s the reason why get this Failed opening required '1' error message.
But using parenthesis on the right place like this makes it work like you want it:
(@include_once 'abc.php') or die("oops");
Here 'abc.php'
is clearly the parameter and the disjunction with die("oops")
is performed on the return value of include_once
. The @
operator is just to ignor the error message includ_once
will throw if the file does not exist.
PS: print
is also a special language construct and works the same way.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With