![]() ![]() In PHP, this organization is accomplished by separating your scripts into multiple files and including them when appropriate. In this respect, and as you accumulate an ever-growing library of functions, the need to organize them becomes more and more paramount. PHP _DIR_ returns the directory of a file or the directory of the include file when the file is used as an include.It is always good practice to make your scripts as modular as possible, designing your functions in such a way that they can be used in other PHP scripts.Therefore, it’s a good practice to use the _DIR_ constant when you include a file. This is because when PHP loads the inc/header.php file from either public/index.php or admin/dashboard/index.php, the _DIR_ inside the inc/header.php always returns C:\xampp\htdocs\web\inc. The index.php in the admin/dashboard will work correctly. To fix this issue, you can use the _DIR_ when you include the config.php in the header.php file like this: Code language: PHP ( php ) Since the admin directory doesn’t have the config/app.php file, PHP issues an error. When the index.php in the admin/dashboard directory loads the code from the header.php, it’ll find the config/app.php inside the admin directory, not root directory. The error message shows that the header.php cannot load the './config/app.php' file. When you browse the page you’ll see the following error: Warning: require(./config/app.php): failed to open stream: No such file or directory in C:\xampp\htdocs\web\inc\header.php on line 1įatal error: require(): Failed opening required './config/app.php' (include_path= '\xampp\php\PEAR') in C:\xampp\htdocs\web\inc\header.php on line 1 Code language: PHP ( php ) The index.php in the admin/dashboard also includes the header.php and footer.php files: So the index.php page in the public directory works as expected. If you navigate to the you’ll see the following output when you view the source of the page: The public/index.php file includes both header.php and footer.php files: The footer.php contains the closing tags of the body and html elements: The config/app.php contains the application’s configuration: Suppose that you have a project with the following directory structure. The _DIR_ in the index.php returns the current directory of the index.php C:\xampp\htdocs\web while the _DIR_ in the footer.php file returns the directory of the footer.php file C:\xampp\htdocs\web\inc. The following shows the output on the web browser: HomeĬ:\xampp\htdocs\web\inc Code language: PHP ( php ) It also shows the value of the _DIR_ constant: The index.php includes both header.php and footer.php files. ![]() The footer.php contains the code of the footer part. The header.php contains the code of the header part: Suppose you have the following directory structure. However, using the _DIR_ is more concise than the dirname(_FILE_). Technically speaking, the _DIR_ is equivalent to the dirname(_FILE_). When you use the _DIR_ inside an include, the _DIR_ returns the directory of the included file. The _DIR_ doesn’t include a trailing slash e.g., / or \ except it’s a root directory. When you reference the _DIR_ inside a file, it returns the directory of the file. PHP 5.3 introduced a new magic constant called _DIR_. Introduction to the PHP _DIR_ magic constant Summary: in this tutorial, you’ll learn about how to use the PHP _DIR_ magic constant when including a PHP file. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |