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

PHP variable scope testing

WBOY
Release: 2016-08-08 09:31:45
Original
1242 people have browsed it
<?php
	$a = 1;
	function test(){
		echo $a;
	}
	test();//$a undefined
?>在外部声明的变量,函数内不会自动调用
Copy after login
<?php
	/*$a = 1;
	function test(){
		$a = 2;
		echo $a;//2
	}
	test();
	echo $a;//1
?>
Copy after login
<?php
	function test(){
		$a = 1;
	}
	test();
	echo $a;//$a undefined
?>
Copy after login
<?php
	/function test(){
		Global $a;
		$a = 1;
	}
	test();
	echo $a;//1
?>在函数中的变量声明为global时在外部可调用
Copy after login
<?php
	Global $a;
	$a = 1;
	function test(){
		echo $a;//undefined
	}
	test();
?>
Copy after login
<?php
	$a = 2;
	function test(){
		Global $a;
		echo $a;//2
		$a++;
	}
	test();
	echo $a;//3
?>
Copy after login
<?php
	define(&#39;A&#39;, 1);
	function test(){
		echo A;//1
	}
	test();
?>使用define后,编译时自动替代
Copy after login

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.

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