您的位置:首页 > 编程语言 > PHP开发

php定义变量

2012-10-21 22:23 323 查看
1.引用全局变量

法1

1 <?php

2  $var=0;

3  function test($index)

4  {

5      global $var;

6      $var=$var+1;

7      echo "The ".$index." number is ".$var."<br>";

8  }

9   test(1);

10  test(2)

11 ?>

法2

1 <?php

2  $var=0;

3  function test($index)

4  {

5     

6      $GLOBALS["var"]=$GLOBALS["var"]+1;

7      echo "The ".$index." number is ".$GLOBALS["var"]."<br>";

8  }

9  test(1);

10  test(2)

11 ?>

2.定义常量

bool define ( string name, mixed value [, bool case_insensitive] )

name 为常量名,value为常量的值。case_insensitive]为大小写敏感。默认为敏感。例如:

1 <?php

2 define("CONSTANT", "Hello world.");

3 echo CONSTANT; // outputs "Hello world."

4 echo Constant; // outputs "Constant" and issues a notice.

原文:
http://www.cnblogs.com/confach/articles/470492.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: