Detailed explanation of PHP's require_once path problem
My site directory is as follows:
****************************************** ***************************
wwwroot //The absolute path to the website root directory is: F:/wwwroot
-- folder_a // Folder A
file_a_a.php file_a_b.php file_a_c.php
-- folder_b // Folder B
file_b_a.php file_b_b.php file_b_c.php
-- index.php
******** *************************************************** ***
This directory hierarchy is already very clear:
wwwroot is the root directory, and below there are the index.php file and two folders folder_a and folder_b
There are 3 php files in each folder
Let’s first look at the contents of the index.php file:
<?php require_once("folder_a/file_a_a.php"); echo "文件folder_a_a.php被包含成功"; ?>
Then look at the contents of the folder_a/folder_a_a.php file:
<?php require_once("../folder_b/file_b_a.php"); $x = new X(); $x.printInfo(); ?>
Finally, let’s take a look at the contents of the folder_b/folder_b_a.php file:
<?php class X{ function printInfo(){ echo 'success; } } ?>
ok If I run floder_a/file_a_a.php
directly now, it will output: success
If I When running index.php
under wwwroot, an error will be reported because the included file cannot be found:file_b_a.php
But if I am in all require_once() Add dirname(FILE).'/'
Then whether you run file_a_a.php or index.php, you can output
********** normally ************************************************
Problem:
The first time I used a relative path, so an error occurred when I repeated the inclusion
The second time I used an absolute path, so there was no error . But I am still a little confused:
I first analyzed the following reasons for the error when using relative paths:
I run index.php, it can find the folder_a directory, and it can also find the folder_a directory. file_a_a.php, so it copies the contents of folder_a/file_a_a.php to the first line of index.php (the line containing the statement), and then continues to run (that is, runs the included content), so this It is equivalent to running require_once('../folder_b/file_b_a.php') in file_a_a.php in index.php; It looks for this path file (file_b_a.php) based on the current location of index.php, and of course it cannot find it. Here it is, so it went wrong.
But isn’t it the same when I use absolute paths? But why doesn’t it go wrong? Maybe everyone is a little confused about this sentence, let me explain in detail (according to The program running sequence will be explained).
The program runs index.php first (note that I added dirname (FILE) at this time, so the current is an absolute path),
index.php first Run the first code: require_once(dirname(FILE).'/'.'folder_a/file_a_a.php');
dirname(FILE) is f:/wwwroot/, so the path included in this code is also That is:
f:/wwwroot/folder_a/file_a_a.php
This path is correct, so there is no problem, right?
ok The first step is completed correctly
Then it copies the code in file_a_a.php to this location in index.php:
Then it continues to run: This is all the code in file_a_a.php running in index.php code, then let’s take a look at which codes it runs?
<?php require_once(dirname(FILE).'/'."../folder_b/file_b_a.php"); $x = new X(); $x.printInfo(); ?>
Yes, that’s it. It should be noted that these codes have been copied to index.php, that is to say, the content of index.php is actually now It becomes:
<?php require_once(dirname(FILE).'/'."../folder_b/file_b_a.php"); $x = new X(); $x.printInfo(); echo "文件folder_a_a.php被包含成功"; ?>
Then still analyze the above code according to the execution order of the program:
dirname(FILE) should be f:/wwwroot (because these codes are now in index.php Execution, the same as relative paths, so what you get is the directory where index.php is located)
Then the included path should be: f:/wwwroot/../folder_b/file_b_a.php
So let's check if there is a file_b_a.php file in this path? The answer is no, because f:/wwwroot/../folder_b has returned to the folder_b directory under the f: drive letter, and this directory does not exist.
But the execution result puzzled me. It was output correctly.
Maybe you will say: In the relative path, index.php first contains file_a_a.php and then executes it. Contains the code of file_b_a.php, so the directory cannot be found. After using the absolute path, before index.php includes file_a_a.php, file_a_a.php has already executed the code containing file_b_a.php, so the output is correct. But please note :The PHP documentation and many documentation tutorials explain this: Including a file actually means copying the code in the included file to the place containing the command. Even if this view is wrong, it is also a require_once command, no Maybe the relative path is included first and then executed, while the absolute path is executed first and then included.
I don’t understand, please give me some advice. (Thank you very much for reading this. The question is very long because I've been depressed for a longer time, so please don't Ctrl+C Ctrl+V, and don't say: look at the API or find your own qualifications, etc.) Thank you.
Don't think too complicated...
Use absolute path:
Your c:\a.php
canreference
d:\b.php
The thing you misunderstood is that FILE always points to the current file, whether it is the main executable file or the included file
That is to say: if you use a relative path../folder_b/file_b_a.php Then it will be copied first and then pointed to the address. If you use FILE, it will be pointed to first and then included. Is that so?
If you use a virtual directory, then this directory will be based on your current running directory. The file is the benchmark
which is
folder_a/folder_a_a.php文件的内容这样写:
<?php require_once("folder_b/file_b_a.php"); $x = new X(); $x.printInfo(); ?>
你看下会不会错
在跟目录里建个文件,把要引用的文件全部按绝对路经引进来;其他目录引这个文件就好了.
The above is the detailed content of Detailed explanation of PHP's require_once path problem. For more information, please follow other related articles on the PHP Chinese website!

PHP remains important in modern web development, especially in content management and e-commerce platforms. 1) PHP has a rich ecosystem and strong framework support, such as Laravel and Symfony. 2) Performance optimization can be achieved through OPcache and Nginx. 3) PHP8.0 introduces JIT compiler to improve performance. 4) Cloud-native applications are deployed through Docker and Kubernetes to improve flexibility and scalability.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python each have their own advantages and are suitable for different scenarios. 1.PHP is suitable for web development and provides built-in web servers and rich function libraries. 2. Python is suitable for data science and machine learning, with concise syntax and a powerful standard library. When choosing, it should be decided based on project requirements.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

The reasons why PHP is the preferred technology stack for many websites include its ease of use, strong community support, and widespread use. 1) Easy to learn and use, suitable for beginners. 2) Have a huge developer community and rich resources. 3) Widely used in WordPress, Drupal and other platforms. 4) Integrate tightly with web servers to simplify development deployment.

PHP remains a powerful and widely used tool in modern programming, especially in the field of web development. 1) PHP is easy to use and seamlessly integrated with databases, and is the first choice for many developers. 2) It supports dynamic content generation and object-oriented programming, suitable for quickly creating and maintaining websites. 3) PHP's performance can be improved by caching and optimizing database queries, and its extensive community and rich ecosystem make it still important in today's technology stack.

In PHP, weak references are implemented through the WeakReference class and will not prevent the garbage collector from reclaiming objects. Weak references are suitable for scenarios such as caching systems and event listeners. It should be noted that it cannot guarantee the survival of objects and that garbage collection may be delayed.

The \_\_invoke method allows objects to be called like functions. 1. Define the \_\_invoke method so that the object can be called. 2. When using the $obj(...) syntax, PHP will execute the \_\_invoke method. 3. Suitable for scenarios such as logging and calculator, improving code flexibility and readability.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

Zend Studio 13.0.1
Powerful PHP integrated development environment

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Dreamweaver CS6
Visual web development tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.