Home  >  Article  >  Backend Development  >  Full analysis of php global variables and classes used together_PHP tutorial

Full analysis of php global variables and classes used together_PHP tutorial

WBOY
WBOYOriginal
2016-07-14 10:11:081088browse

Case 1:

father.php is defined 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, the running result is 1000. If $jack=123 is placed before require("father.php");, the running result is 1000.

Easier to understand: PHP explains and executes, wherever it is explained, it is executed. . Global variables like $jack are global variables. For example, in the first case, when it is initially used, it is 1000, which is in require

When running

, the result was changed to 123, so the running result output is 123.

Case 2:

The children.php code is changed to the following:

require("father.php");

function testJack(){

if(!isset($jack)){

echo '$jack is null'."/n";

}

}//testJack

testJack();

?>

php children.php

The operation result is: $jack is null. That is to say, the $jack referenced in testJack() is a local variable.

If you use the global keyword and declare $jack as a global variable, the code will be changed to the following:

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 result is $jack is not null!

Case 3:

The children.php 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();

?>

Run 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 the file name as a parameter, the code is as follows:

$casefile = $_SERVER['argv'][1];

echo $casefile."/n";

require($casefile);

echo $jack."/n";

?>

Run php children.php father.php

The results are as follows:

father.php

1000

That is to say, our dynamic loader ran successfully. .

Case 5:

To combine dynamic loading with class definition:

The directory relationship is as follows:
|- 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 function of a.class.php requires b.php under the Bfold folder. This require (../Bfold/b.php ) error, Warning: ……


Because you let the server currently execute the c.php file, when php parses it, the path is relative to c.php. Try changing (../Bfold/b.php) to (Bfold/b .php) and you should see no error.

The following is a program example illustrating the use of require_once (A.php) inside a function.

Understanding of require_once:

Assume that require_once(A.php); is referenced in B.php. .

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 register its variables that are global variables relative to A to B. php environment. 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 during this calling process, the root operating environment of B and A is C's. The environment of the calling function, but if the calling function of C 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/477354.htmlTechArticleCase 1: father.php is defined as follows: ?php $jack = 1000; ? children.php is defined as follows: ? php require(father.php); $jack=123; echo $jack./n; ? The output of php children.php is 123. If...
Statement:
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