Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to conditionally require_once, set globals, or set constants?

I have a particular php class that I want to be able to upload identical copies to two different servers. Depending on the server though, the requires will be located in different places. (the constants and globals are slightly different as well) Can I conditionally set require_once, Globals, or constants at the beginning of the file?

like image 922
stormist Avatar asked Apr 20 '10 19:04

stormist


People also ask

How to require_ once in PHP?

require_once() function can be used to include a PHP file in another one, when you may need to include the called file more than once. If it is found that the file has already been included, calling script is going to ignore further inclusions. If a. php is a php script calling b.

What is the difference between require and require_once in php?

The require() function is used to include a PHP file into another irrespective of whether the file is included before or not. The require_once() will first check whether a file is already included or not and if it is already included then it will not include it again.

Should I always use require_once?

You should use require_once when you do not need the file's contents included multiple times. It is also helpful if you are working on a script and are unsure if the file has already been included and you do not want it included twice.

How does require_once work in PHP?

The require_once keyword is used to embed PHP code from another file. If the file is not found, a fatal error is thrown and the program stops. If the file was already included previously, this statement will not include it again.


1 Answers

Of course:

<?php

if (/* some conditions */) {
    require_once('some.file.php');
} else {
    require_once('another.file.php');
}

?>
like image 86
Michał Mech Avatar answered Sep 28 '22 01:09

Michał Mech