Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

include, include_once, require or require_once?

Tags:

include

php

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.

like image 515
Ibrahim Azhar Armar Avatar asked Aug 23 '10 09:08

Ibrahim Azhar Armar


People also ask

What is difference between include require include_once () require_once ()?

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.

Should I use include or include_once?

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.

What are include () and require () functions?

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.

What is include Include_once?

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.


2 Answers

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.

like image 107
Sasha Chedygov Avatar answered Sep 22 '22 23:09

Sasha Chedygov


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/

like image 42
ReadMyViews Com Avatar answered Sep 22 '22 23:09

ReadMyViews Com