Require_Once () or Die() Not Working

require_once () or die() not working

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.

php require_once not working the way I want it to.. relative path issue

require(_once) and include(_once) work relative to the path of the first script, i.e. the script that was actually called.

If you need to require something in an included or required file, and would like to work relative to that file, use

dirname(__FILE__)."/path/to/file";

or as @Arkh points out, from PHP 5.3 upwards

__DIR__."/path/to/file";

require_once problem with templating

In your comment you say your code is:

<?php require_once("header.php") or die; ?>

please try leaving out the "or die" -> ist is not necessary, because require_once will trigger a fatal error if the file is not found.

Edit:
I tested it, leave out the "or die" and it will work.

Edit 2: Explanation why this happened:
The above code can also be written like this:

require_once ( ("header.php") or die() );

Because require_once is not a function, but a statement in php, the braces are optional, php interprets the above as a boolean expression, where the string "header.php" gets evaluated to the boolean "true". True gets cast to a string (resulting in "1"), and this string gets passed back to require once.

So the above code gets interpreted as

require_once("1")

Here is how it gets interpreted step by step:

(("header.php") or die()) = (TRUE or die())
(TRUE or die()) = TRUE
(string) TRUE = '1';

hope this helps ;)

includes files not being found through require_once()

Hey guys thank you all for your input. I have finally found the problem with my code. It is not because of the autoloader function.

I did not know that the order of the files being called in the init.php mattered. I have simply re-ordered the require_once() statements.

The db_object.php file is actually the file that contains the parent class for my objects (photos and users). The photo.php and users.php contain inherited classes from the db_object.php. Which would made sense on why we need to find the db_object.php first.

Before:

<?php
require_once("functions.php");
require_once("db_config.php");
require_once("database.php");
require_once("user.php");
require_once("session.php");
require_once("photo.php");
require_once("db_object.php");
?>

After:

<?php
require_once("functions.php");
require_once("db_config.php");
require_once("database.php");
require_once("db_object.php");
require_once("user.php");
require_once("session.php");
require_once("photo.php");
?>


Related Topics



Leave a reply



Submit