Insert file in PHP

You can insert the content of a PHP file into another PHP file before the Server executes it. There are two functions in PHP that can be used to do this.

  1. Function include ()
  2. Require function ()

This is a strong point of PHP that helps in creating functions, headers, footers or elements that can be reused in multiple pages. This will help developers easily change the layout of the web. If any changes are required, instead of having to change thousands of files, just change the included file.

The include () function in PHP

The include () function takes all the text in the specified file and copies them into the file that uses the include function. If there are any problems during file loading, the include () function generates an alert but the script continues executing.

Suppose you want to create a general menu for the Website. Then create a file as menu.php in htdocs with the following content:

 Home - 
PHP -
JAVA -
HTML

Now create as many pages as you want and insert this file to create the header. For example, test.php may have the following content.




The example illustrates how to include files in PHP!



Save the above program in a file named test.php in htdocs , then open the browser and type the address http:/// localhost: 8080 / test.php will result:

Insert file in PHP Picture 1

The require () function in PHP

The function require () takes all the text in the specified file and copies them into the file using the require function. If there is any problem during file loading, the require () function generates a fatal error (Fatal Error) and prevents the execution of the script.

So there is no difference between require () and include () besides they handle error conditions. We encourage you to use require () function instead of include (), because the script should not continue executing if the file is missing or the name is wrong.

You can use the above example with the require () function and it will generate the same result. But if you try to follow the following two examples, and if with a file does not exist, you will get different results.




The example illustrates how to include files in PHP!



Save the program in a file named test.php in htdocs , then open the browser and type in the address http:/// localhost: 8080 / test.php will result (done on Google Chrome):

Insert file in PHP Picture 2

Now try the above example with the require () function in PHP.




The example illustrates how to include files in PHP!



The executable file this time pauses and does not display anything.

Save the program in a file named test.php in htdocs , then open the browser and type in the address http:/// localhost: 8080 / test.php will give the result (I do it on Google Chrome):

Insert file in PHP Picture 3

Note - You may receive a warning message or error message or nothing. This depends on your PHP Server configuration.

Follow tutorialspoint

Previous lesson: String (String) in PHP

Next lesson: File & I / O in PHP

5 ★ | 1 Vote

May be interested

  • Insert image into PDF file with Foxit ReaderInsert image into PDF file with Foxit Reader
    foxit reader pdf file reader supports you a lot of features, including the ability to insert images into pdf files very useful. the following article will guide you to use foxit reader to insert images into pdf files.
  • How to insert images into Powerpoint 2016How to insert images into Powerpoint 2016
    adding photos can make your presentation more interesting and engaging. you can insert images from a file on your computer into any slide. powerpoint even includes tools to find images online and add screenshots to presentations.
  • How to use the Insert key to insert copied text in Word?How to use the Insert key to insert copied text in Word?
    the insert key is also used as a shortcut to insert text to copy or cut text content at the current cursor position. if you don't know how to use the insert key to insert copied text in word, please refer to the following article of
  • Insert Audio audio file into Slide in PowerPointInsert Audio audio file into Slide in PowerPoint
    introduce how to insert an audio audio file into a slide in powerpoint. 1. insert audio into slide. step 1: go to the insert - audio tab - include the following options: - online audio: select the audio files to search online. - audio of my pc: select files â
  • How to insert Logo into WordHow to insert Logo into Word
    how to insert logo into word. inserting a logo into a word file also makes it easy for readers of word files to recognize where and whose file it is and quickly they will be able to communicate quickly when needed.
  • How to Insert Images into PDF FilesHow to Insert Images into PDF Files
    today's tipsmake will show you how to insert your own images into any pdf file using an online pdf editor, adobe acrobat or preview for macos.
  • How to insert watermark, logo sink into ExcelHow to insert watermark, logo sink into Excel
    inserting a logo into an excel file will help stamp documents, avoid copying content.
  • How to insert Excel into CAD quickly and correctlyHow to insert Excel into CAD quickly and correctly
    instead of calculating in cad, we usually calculate the size of the drawing through excel and insert the excel file into the cad. if you do not know how to insert excel into cad, you should follow the tutorial below!
  • How to insert and copy PDF files into Word quickly and standardlyHow to insert and copy PDF files into Word quickly and standardly
    when you receive the file or data in pdf format that you want to edit, or simply copy a paragraph in the pdf file but it is not done because the pdf file does not allow users to intervene to edit. so how to chu?
  • Instructions to insert flash into PowerPointInstructions to insert flash into PowerPoint
    insert flash file into powerpoint to make presentations more vivid and effective. you can install the support software but this will be very complicated for those you are less exposed to the download and installation of software.