PHP Include File

PHP include() function tutorial will explain you how to include file from an external file? with example.

Keeping up or redesigning an impressive site is not straightforward in the event that you need to make updates to the header, navigation or footer sections.

You will edit every page in parts. This will take quite a while particularly depending on if you have number of pages to update.

PHP include() function permit you to import HTML/PHP code from external files to your present PHP script.

PHP has four commands that might be include code from an external file:

    • PHP include() function – creates a warning if the external file is absent, however the script will proceed execution.
    • PHP include_once() function – anticipate the same file from being included more than once in a page. In the event that the external file is lost, creates a warning and attempts to keep handling the script.
    • PHP require() function – If the external file is absent, produces a fatal error, and the script will stop.
    • PHP require_once() function -counteract the same file from being embedded more than once in a page. Provided that the external file is lost, quits preparing the script and tosses a fatal error.

PHP include() Function

PHP include() Function assists you in effortlessly redesigning all your pages from one file, which safeguards time and work.

Syntax for PHP include() Function:

Example on PHP include() Function:

Output will be:

PHP include_once() Function

PHP include_once() Function is comparative to the PHP include() Function, with the main distinction being that if the code from a file has as of recently been included, it won’t be included again.

PHP include_once() Function will prevent problems with function redefinitions, variable value reassignments, etc.

Example on include_once() in PHP:

The output will be:

So here in the above code the echo command displaying Hello will be displayed three times and not five times. PHP include_once() Function will not include the data.php file again.

PHP require()

PHP require() Function is comparative to the PHP include() Function, the main contrast between the PHP include() Function and PHP require() Function is that the PHP require() Function triggers a fatal error when the file included can’t be discovered, while the PHP include() Function just issues a cautioning.

Syntax for PHP require() Function:

Example on PHP require() Function:

Now the header.php file doesn’t exist, so you get the following error:

PHP require_once() Function

PHP require_once() Function is identical to PHP require() function except that PHP will check if the file has already been included, and if so, not include (require) it again.

If PHP require_once() function fails it will generate a fatal error and the execution will stop.

Example on PHP require_once() Function:

The output will be:

And if file doesn’t exist output will be:

SHARE
Previous articlePHP Date() Function
Next articlePHP File Handling
I am a young Information Systems Security Engineer(CEH). I had started with penetration testing when i am 17 years old. It all started with Kali linux and Metasploit.In free time i will play with Security holes/Bug bounty/Pentesting and Hacking !!!