Home >Backend Development >PHP Tutorial >PHP variable scope testing

PHP variable scope testing

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-08-08 09:31:451271browse
<?php
	$a = 1;
	function test(){
		echo $a;
	}
	test();//$a undefined
?>在外部声明的变量,函数内不会自动调用
<?php
	/*$a = 1;
	function test(){
		$a = 2;
		echo $a;//2
	}
	test();
	echo $a;//1
?>
<?php
	function test(){
		$a = 1;
	}
	test();
	echo $a;//$a undefined
?>
<?php
	/function test(){
		Global $a;
		$a = 1;
	}
	test();
	echo $a;//1
?>在函数中的变量声明为global时在外部可调用
<?php
	Global $a;
	$a = 1;
	function test(){
		echo $a;//undefined
	}
	test();
?>
<?php
	$a = 2;
	function test(){
		Global $a;
		echo $a;//2
		$a++;
	}
	test();
	echo $a;//3
?>
<?php
	define(&#39;A&#39;, 1);
	function test(){
		echo A;//1
	}
	test();
?>使用define后,编译时自动替代

The above has introduced PHP variable scope testing, including aspects of it. I hope it will be helpful to friends who are interested in PHP tutorials.

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