After two php include operations, the variables in the first include operation are invalid. directory structure + & nbsp; root & nbsp; index. php & nbsp; config. php & nbsp; after two php include operations, the variables in the first include operation are invalid.
Directory structure
+ Root
Index. php
Config. php
+ C
Index. php
Contents in root/config. php
$ Shell ['jquery '] = "jquery 1.4 ";
?>
Contents in root/index. php
Include "config. php ";
Print $ shell ['jquery '];
?>
Contents in root/c/index. php
Include "../index. php ";
?>
Access root/index. php and return
Jquery 1.4
Access root/c/index. php and return
Notice: Undefined variable: shell in H: \ software \ dev \ php \ xampp \ htdocs \ phptest \ index. php on line 3
Why does the variable become invalid after two include operations? How should we do it?
Thank you!
Share with php:
------ Solution --------------------
First, the resolution order is as follows:
1. is the file path an absolute path? Yes, including. resolution is complete. No, go to the next step.
2. the file path is a relative path (like your "../index. php ")? Yes. skip include_path and parse the relative path. No. next step.
PS: the base point of the relative path is always the "current working directory". that is, you have executed the script in this directory, not necessarily the directory where the script file exists.
For example, if you run 'php/var/www/index. php' in/root, the "current working directory" is/root instead of/var/www/, and/var/www is
Current_script_dir.
3. export de_path in php. ini, for example, ".: somepath: current_script_dir ". Split the list to be processed according to the DEFAULT_DIR_SEPARATOR constant (":" here ).
"." (Current working directory), "somepath (your custom directory)", "current_script_dir"
Append the included file names to these paths and try them one by one.
So your problem becomes
# Access root/c/index. php
Include "../index. php"; # execute step 2, include in/root/index. php
Include "config. php ";
# Run the third worker. The current working directory is/root/c, and current_script_dir is also/root/c,
# It is estimated that you have not customized include_path, so try to include/root/c/config. php without this file.
Print $ shell ['jquery '];
# Undefined variable
?>
This is my understanding. thank you for your comments and comments.