Home php教程 php手册 概论为什么PHP全局变量不能生效

概论为什么PHP全局变量不能生效

Jun 13, 2016 am 11:10 AM
php cannot Why overall situation variable exist newbie hour Take effect

对于一个新手在实际操作

但在实际操作时,会接连碰到几次全局变量无效的郁闷的事,下面就重点看一下PHP全局变量不能生效的原因及解决办法。

1.PHP全局变量不能生效错误重现

问题出现在用我的简易框架时,在view中使用原来第三方定义好的函数时(此函数比较独立),下面模拟一下:

t1.php

  1.  ?  
  2. run(); //执行  
  3. function run(){  
  4. include 'func.php';  
  5. showGlobal();  
  6. }  
  7. ?> 

func.php1

<ol class="dp-xml">
<li class="alt"><span><span class="tag"><span> ?  </span></span></span></li>
<li>
<span>$</span><span class="attribute">vars</span><span> = </span><span class="attribute-value">'I am global!'</span><span>;  </span>
</li>
<li class="alt"><span>function showGlobal(){  </span></li>
<li><span>global $vars;  </span></li>
<li class="alt"><span>print('我使用全局变量:'.$vars);  </span></li>
<li><span>}  </span></li>
<li class="alt">
<span class="tag">?></span><span> </span>
</li>
</ol>
Copy after login

很简单的两个文件(第一次发现问题时,远比这复杂,在一层层排错后,得出最少化的问题重现环境),func.php是定义好的第三方函数,该函数使用了一些全局变量,如果这时把这两个文件放在一起,执行一下t1.php,会发现showGlobal里的$vars是显示不出来的,global失效了?

2.PHP全局变量不能生效错误原因

搜索后发现,php.net上很早就有人提出过,也有人给出了解释:
原来,在t1.php的run函数中include func.php时,func.php中的变量$vars的作用域只是在run之内,而在showGlobal中使用global声明的$vars是要求属于t1.php的不是run函数的,所以为空)

3.PHP全局变量不能生效解决方法

知道原因后,解决就很简单,可以把include 从run中移出来,这样,func.php中的$vars就属于t1.php了;也可以在run里用global声明一下$vars,这样也可以把原来属于run的$vars声明为全局(属于t1.php);

虽然问题可以简决,但用起来还是很不爽, 因为在我的phpec框架中,include的情况比较普遍,不可能把view中按需include的移到外层去,使用global的话,我在使用第三方函数时,又不会也不想去了解它用了什么全局变量,而且,层次结构一多,就….

PHP全局变量不能生效的原因总结:

1)尽量减少多级和函数中include文件.

2)尽量不用全局变量


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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Explain the match expression (PHP 8 ) and how it differs from switch. Explain the match expression (PHP 8 ) and how it differs from switch. Apr 06, 2025 am 12:03 AM

In PHP8, match expressions are a new control structure that returns different results based on the value of the expression. 1) It is similar to a switch statement, but returns a value instead of an execution statement block. 2) The match expression is strictly compared (===), which improves security. 3) It avoids possible break omissions in switch statements and enhances the simplicity and readability of the code.

Does H5 page production require continuous maintenance? Does H5 page production require continuous maintenance? Apr 05, 2025 pm 11:27 PM

The H5 page needs to be maintained continuously, because of factors such as code vulnerabilities, browser compatibility, performance optimization, security updates and user experience improvements. Effective maintenance methods include establishing a complete testing system, using version control tools, regularly monitoring page performance, collecting user feedback and formulating maintenance plans.

Can JS run without H5? Can JS run without H5? Apr 06, 2025 am 09:06 AM

Is JavaScript available to run without HTML5? The JavaScript engine itself can run independently. Running JavaScript in a browser environment depends on HTML5 because it provides the standardized environment required to load and execute code. The APIs and features provided by HTML5 are crucial to modern JavaScript frameworks and libraries. Without HTML5 environments, many JavaScript features are difficult to implement or cannot be implemented.

What are the advantages of H5 page production What are the advantages of H5 page production Apr 05, 2025 pm 11:48 PM

The advantages of H5 page production include: lightweight experience, fast loading speed, and improving user retention. Cross-platform compatibility, no need to adapt to different platforms, improving development efficiency. Flexibility and dynamic updates, no audit required, making it easier to modify and update content. Cost-effective, lower development costs than native apps.

What is Cross-Site Request Forgery (CSRF) and how do you implement CSRF protection in PHP? What is Cross-Site Request Forgery (CSRF) and how do you implement CSRF protection in PHP? Apr 07, 2025 am 12:02 AM

In PHP, you can effectively prevent CSRF attacks by using unpredictable tokens. Specific methods include: 1. Generate and embed CSRF tokens in the form; 2. Verify the validity of the token when processing the request.

Describe the purpose and usage of the ... (splat) operator in PHP function arguments and array unpacking. Describe the purpose and usage of the ... (splat) operator in PHP function arguments and array unpacking. Apr 06, 2025 am 12:07 AM

The... (splat) operator in PHP is used to unpack function parameters and arrays, improving code simplicity and efficiency. 1) Function parameter unpacking: Pass the array element as a parameter to the function. 2) Array unpacking: Unpack an array into another array or as a function parameter.

How can you prevent a class from being extended or a method from being overridden in PHP? (final keyword) How can you prevent a class from being extended or a method from being overridden in PHP? (final keyword) Apr 08, 2025 am 12:03 AM

In PHP, the final keyword is used to prevent classes from being inherited and methods being overwritten. 1) When marking the class as final, the class cannot be inherited. 2) When marking the method as final, the method cannot be rewritten by the subclass. Using final keywords ensures the stability and security of your code.

Why does a specific div element in the Edge browser not display? How to solve this problem? Why does a specific div element in the Edge browser not display? How to solve this problem? Apr 05, 2025 pm 08:21 PM

How to solve the display problem caused by user agent style sheets? When using the Edge browser, a div element in the project cannot be displayed. After checking, I posted...

See all articles