I have PHP file where I have defined the server access variables as well as the mysql_connect
and mysql_select_db
, as this functions are regularly used in almost every page in backend, while I am using include()
which is perfectly working for me now, which method or function would you suggest and I would like to know if there is any flaw if I use include()
or is it safe to use it?
Edit : Keeping in mind I'll be using $_SESSION
variable too.
The only difference between the two is that require and its sister require_once throw a fatal error if the file is not found, whereas include and include_once only show a warning and continue to load the rest of the page.
The include() function is used to include a PHP file into another irrespective of whether the file is included before or not. The include_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.
These functions are the same if but they have one difference. The difference is that the include() function produces a warning, but the script will continue execution, while the require() function produces a warning and a fatal error i.e. the script will not continue execution.
Definition and Usage. The include_once keyword is used to embed PHP code from another file. If the file is not found, a warning is shown and the program continues to run. If the file was already included previously, this statement will not include it again.
The only difference between the two is that require
and its sister require_once
throw a fatal error if the file is not found, whereas include
and include_once
only show a warning and continue to load the rest of the page. If you don't want PHP to attempt to load the rest of your page without the database info (which I would assume), then use require_once
. You don't need to include the file more than once, so there is no need to use the regular require
function.
Functional Work : All functions perform similar work. All functions will include and evaluates the specific file while executing the code.
Functional Difference :
include vs include_once : There is only one difference between include() and include_once(). If the code from a file has been already included then it will not be included again if we use include_once(). Means include_once() include the file only once at a time.
include vs require : if include() is not able to find a specified file on location at that time it will throw a warning however, it will not stop script execution. For the same scenario, require() will throw a fatal error and it will stop the script execution.
require vs require_once : There is only one difference between require() and require_once(). If the code from a file has been already included then it will not be included again if we use require_once(). Means require_once() include the file only once at a time.
To get the detailed knowledge with example please review these amazing articles
(1) http://www.readmyviews.com/include-vs-include-once/
(2) http://www.readmyviews.com/include-vs-require/
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