Home  >  Article  >  What are the changes in the global variable mechanism under PHP5 and PHP7? (Code actual test)

What are the changes in the global variable mechanism under PHP5 and PHP7? (Code actual test)

藏色散人
藏色散人Original
2018-08-09 12:14:436944browse

For PHP programmers, as different versions of PHP update and improve some function variable mechanisms, it often causes confusion when using the same variable in different versions produces different results. So this article mainly explains to you in detail why different results occur when using the same global variable under PHP5 and PHP7 versions? Let me give you a simple and easy-to-understand example.

For example, the existing exl.php page operation code is as follows:

What are the changes in the global variable mechanism under PHP5 and PHP7? (Code actual test)

#The result we ran in the local php5 version environment is 5, as shown in the screenshot below :

What are the changes in the global variable mechanism under PHP5 and PHP7? (Code actual test)

Then the result of running it in the php7 version environment is 3, as shown in the screenshot below:

What are the changes in the global variable mechanism under PHP5 and PHP7? (Code actual test)

The reason why Different operation results will appear. This is actually because in the latest php7 version, the PHP global variable processing mechanism has been modified. In this version, the global keyword can now only refer to simple variables. So when we operate in the local php5 version environment, the output value of $y is $x+$y=5. At this time, global is equivalent to the use of global variables. In the php7 environment, the output value of $y is $y=3. At this time, the global variable can only directly reference the simple variable $y.

So through the above sample code explanation, I hope that this article about the problem of global variables in PHP producing different results when operating in different versions will be helpful to everyone when encountering related problems. I also hope that everyone can pass this article. This article provides an understanding of the usage of php global under different versions.

[Recommended related articles]

Why do global variables become invalid or wrong?

Some different understandings of global and $GLOBAL['']

Global keyword in PHP source code analysis

global and $GLOBALS in php


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