入门学习PHP之变量_1

 

1、函数里只能访问局部变量,不能访问全局变量,如果函数里需要访问全局变量则需要在变量前加global作用域,如下实例:

<?php
$x=5;
$y=10;
function myTest()
{
global $x,$y;
$y=$x+$y;
}
myTest();
echo $y; // 输出 15
?>

 

<?php
function myTest()
{
static $x=0;
echo $x;
$x++;
}
myTest();//0
myTest();//1
myTest();//2
?>

 

posted @ 2016-06-07 19:15  hllive  阅读(120)  评论(0编辑  收藏  举报