Blogger Information
Blog 34
fans 2
comment 2
visits 22716
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
变量的作用域 20180413-15:34
AsNwds_MIS王的博客
Original
682 people have browsed it

实例

<meta charset="UTF-8">


<?php
//变量的作用域
$x = 1;  //全局变量

function myTest(){
	$y = 2;		//局部变量
	echo "变量y是$y";
	echo '<hr color="red">';
	global $x;
	echo "变量x是$x";
	echo '<hr color="black">';
	static $z =3;  //静态变量
	echo $z;
	echo '<hr color="red">';	
	$z++;

}
myTest();
myTest();
myTest();
echo '<hr color="blue">';
echo "$x";
echo '<hr color="yellow">';
echo "$y";
echo '<hr color="red">';
?>

运行实例 »

点击 "运行实例" 按钮查看在线实例

PHP 变量作用域

在 PHP 中,可以在脚本的任意位置对变量进行声明。

变量的作用域指的是变量能够被引用/使用的那部分脚本。

PHP 有三种不同的变量作用域:

local(局部)

global(全局)

static(静态)


Correction status:Uncorrected

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post