Home > Backend Development > PHP Tutorial > Deep understanding of the use of php global variables and classes_PHP tutorial

Deep understanding of the use of php global variables and classes_PHP tutorial

WBOY
Release: 2016-07-21 15:08:23
Original
1210 people have browsed it

Case 1:
father.php is defined as follows:

Copy code The code is as follows:

$jack = 1000;
?>
children.php is defined as follows:
require("father.php");
$ jack=123;
echo $jack."/n";
?>

php children.php
The running output is 123.
If $jack =123 is commented out, and the running result is 1000. If $jack=123 is placed before require("father.php");, the running result is 1000.
It is easier to understand: PHP explains and executes, where it is explained, and where it is executed where. . Global variables like $jack are global variables. For example, in the first case, when it is initially used, it is 1000. It is obtained when running require
, and the result is changed to 123, so the running result output is 123.
Case 2:
The children.php code is changed to the following:
Copy the code The code is as follows:

require("father.php");
function testJack(){
if(!isset($jack)){
echo '$jack is null '."/n";
}
}//testJack
testJack();
?>

php children.php
The running result is : $jack is null. That is to say, $jack referenced in testJack() is a local variable.
If you use the global keyword, declare that $jack is a global variable, and the code is changed to the following:
Copy the code The code is as follows:

require("father.php");
function testJack(){
global $jack;
if(!isset($jack)){
echo '$jack is null'."/n";
}else{
echo '$jack is not null'."/n";
}
}//testJack
testJack();
?>

The running result is $jack is not null!
Case 3:
children.php code As follows:
Copy code The code is as follows:

require("father.php");
class JackTest{
public function testJack(){
if(!isset($jack)){
echo '$jack is null'."/n";
}else{
echo '$jack is not null'."/n";
}
}//testJack
}
$jackTest = new JackTest();
$jackTest-> ;testJack();
?>

Running result output: $jack is null
This is because $jack of this function in class is a local variable.
If you add global $jack; at the beginning of function testJack, then $jack is not null will be output.
Easier to understand.
Case 4:
Dynamic loading of the file name as a parameter, the code is as follows:
Copy the code The code is as follows:

$casefile = $_SERVER['argv'][1];
echo $casefile."/n";
require($casefile);
echo $jack."/n";
?>

Run php children.php father.php
The result is as follows:
father.php
1000
That is to say, our dynamic loader has been successfully run. .
Case 5:
To combine dynamic loading with class definition:
The directory relationship is like this:
|- c.php
|- Bfold - b.php
|- Afold - a.class.php (the functions inside refer to ../Bfold/b.php)
That is to say, class a.class is new in c.php, and a A function in .class.php requires b.php in the Bfold folder. This require (../Bfold/b.php) reports an error, Warning: ...
Because you asked the server to currently execute c. php file, so when php parses, the path is relative to c.php. You can try changing (../Bfold/b.php) to (Bfold/b.php). It should not work. An error was reported.
The following is a program example illustrating the use of require_once (A.php) inside a function.
Understanding of require_once:
Assume require_once(A.php) is referenced in B.php ; this statement. .
Then it is actually equivalent to calling the anonymous lambda function A.php to execute. As shown below:
C.php requires B.php in a function call------》
B.php requires A.php in a normal statement--------》
A.php
Now we call php B.php; because B.php uses require in a normal statement to call A.php, then A.php will treat it as a global variable relative to A. Variables are registered in the environment of B.php. Because B.php is the root calling file, its running environment is the global environment. So the variables in the A.php file can be used normally in B.php.

Now we call php C.php; then C calls B.php using require in the function, and then B calls A. It feels that in the process of this call, relative to the root operating environment of B and A It is the environment of C's calling function, but if C's calling function wants to use the variables in B and A, there is no way.

If you use global $a, to reference, then because $a does not belong to the global variable in this case, it cannot be referenced.
If you use $a to reference, then $a will be treated as a local variable and cannot be referenced.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/327466.htmlTechArticleCase 1: father.php is defined as follows: Copy the code as follows: ?php $jack = 1000; ? children. php is defined as follows: ?php require("father.php"); $jack=123; echo $jack."/n"; ? php childr...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template