PHP Include Statement

If you want to save your time and effort of writing the same code many times. You have to use the PHP include statement to include the file code without writing it again and again.

What is PHP Include Statement?

PHP include statement is the way of including the file code inside another file. By including the file into another file, you can add scripting codes in the required location of the file. It copies the included file code in the added location of the file which is same as writing the scripting code in the location.

Syntax

OR you can use the below syntax

The first syntax contains the parenthesis while the second syntax is without the parenthesis.

Note: We will use the first method in the examples to include the files which are with parenthesis. However, you can use any of them to include the file. Both the above-given syntax formats give the same outputs.

You don’t need to type the same code many times in the required location of the file. You have to just type and save the code in the PHP file. Next time when you have to use the code into another file, you have to just use the PHP include statement to add the required script.

How to Include File into Another File Using PHP Include

If you are writing code for your project based on PHP application. You may require to include the same piece of code many times in multiple files.

The body part of the application can contain different codes. While header, footer and menu may contains the same codes. You can reduce your time of writing same codes for header, footer and menu by using the below syntax and examples.

Examples1
Suppose that, you have to include the file with name ‘menu.php’ which contains the below code to create a menu.

You can include the above files as given in the example below.

Output

Welcome to our website! Here, you will learn with lots of examples.
This page contains PHP codes.

Examples2
Suppose that, you have to include the file with name ‘header.php’ which contains the below code to create a header.

You can include the above files as given in the example below.

Output

Tutorialdeep

Welcome to Tutorialdeep! Learn the basics of PHP with examples.
Add your PHP codes here.

Use Include_once Statement

Suppose you have a file name menu.php to include into another file. Mistakenly, you have included the file for more than one time. If you are using PHP include statement to include the file into the another file. This may give some error when you execute the code.

In that condition, you have to use the PHP include_once statement which include the file for only once. If you include the same file for more than one time by using the PHP include_once statement. It will not include the file for second time if already included for once.

Syntax

OR you can use the below syntax

See the below examples to learn the use with live demo.

Example3
Suppose you have a file name ‘footer.php’ as given below.

If you include the above file for more than one times using PHP include_once statement as given below.

Output

Welcome to our website Tutorialdeep! Use examples on your project.
Add your PHP codes here.

Copyright © 2019 Tutorialdeep

You may also like to read

Hope, you like this post of how to use the PHP include statement. If you have any query regarding the tutorial, please comment below.

Also tell me, how you have included your file into another file using PHP. Please leave a comment below.