Home > Backend Development > PHP Tutorial > The principle and solution of $GLOBALS failure in PHP

The principle and solution of $GLOBALS failure in PHP

墨辰丷
Release: 2023-03-31 20:56:02
Original
2755 people have browsed it

This article mainly introduces the solution to the global variable global failure after multiple includes in PHP. The example analyzes the principle and solution of $GLOBALS failure, which has certain reference value. Friends in need can refer to it

The example in this article describes the solution to the failure of the global variable global after multiple includes in PHP.

The specific analysis is as follows:

In multiple files, the files are included one after another, but the function in the last file cannot reference global variables after using global. For example:
a.php file:

<?php
$aa = 1;
?>
Copy after login

b.php file:

<?php
include a.php
function show(){
global $aa;
var_dump($aa);
}
?>
Copy after login

Display: null;

This failure is caused by a variety of reasons. An effective approach is to use the $GLOBALS array if you decide to use a variable as a global variable for multiple files. For example, a.php in the above example:

<?php
$GLOBALS[&#39;aa&#39;] = 1;
?>
Copy after login

Then you can reference this variable in functions and methods in multiple files.

Summary: The above is the entire content of this article, I hope it will be helpful to everyone's study.

Related recommendations:

php operates strings and arrays to implement similarity algorithm

php regular expression to implement filtering UBB Code class

php method to operate ffmpeg to add subtitles to video

##

The above is the detailed content of The principle and solution of $GLOBALS failure in PHP. For more information, please follow other related articles on the PHP Chinese website!

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