![]() ![]() ![]() The Database.php file also loads the Logger.php file. Image that you have a file called index.php that loads two other files: In the example above, if you use the include_once construct, the script will work properly: PHP include_once Demo Code language: HTML, XML ( xml ) Why use the PHP include_once construct ![]() Simply put, the include_once loads the file just once regardless of how many times the file is included. The include_once behaves like the include statement except that if the file is included again, the include_once won’t load the file and returns true. To avoid including a file more than once, you can use the include_once statement: include_once 'path_to_file' Code language: PHP ( php ) In the index.php file, if you include the header.php file twice, you’ll see that the page will have two headers: PHP include_once Demo Code language: HTML, XML ( xml ) The functions.php has the dd() function definition: ' Īnd in the index.php file, you include the functions.php file twice: PHP include_once Code language: HTML, XML ( xml )įooter.php Code language: HTML, XML ( xml ) └── index.php Code language: plaintext ( plaintext ) Suppose that you have the following project directory. If the included file has a function, you’ll get a fatal error because the function is already redeclared in the first load. Sometimes, you may have a file that is included more than once. In the include tutorial, you learned how to load the code from another file using the include construct. Introduction to the PHP include_once construct Summary: in this tutorial, you will learn how to use the PHP include_once construct to include a file once. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |