Home > Backend Development > PHP Tutorial > php基础-范例:global和static的使用和区别

php基础-范例:global和static的使用和区别

WBOY
Release: 2016-06-13 10:49:35
Original
994 people have browsed it

php基础-实例:global和static的使用和区别

?? ? function testGloabl(){
??? ??? global $testGloabl;
??? ??? $testGloabl = 2;
??? ??? $testGloabl ++;
???? }
???? testGloabl();
???? echo $testGloabl."||".$GLOBALS['testGloabl'];
????
???? function testStatic(){
??? ??? static $testStatic = 2;
??? ??? $testStatic ++;
???? }

??? testStatic();
??? echo $testStatic;//使用static关键字的变量,在函数外部访问将会提示 $testStatic未定义;

?

说明:

1.使用global定义的关键字,当这个关键字所在函数被调用后,它将在当前脚本的任何位置可以被访问到

2.使用static定位的关键字,当这个关键字所在函数内被访问,该函数外其他地方无法访问到。

共同点:global? static 都可以使定位的变量指向同一块内存区域。

Related labels:
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