2.2PHP数据类型

2.2.4. 布尔类型

Boolean型要注意的,以下值都是false:

  • 关键字 false

  • 整数 0

  • 浮点数 0.0

  • 空字符串 ("" ) 和字符串 "0"

  • 0个元素的数组

  • 没有值的和函数的对象

  • NULL

可以使用is_bool( )来判断一个值是不是布尔类型

if (is_bool($x)) {
        // $x is a Boolean
    }

 

2.3.1. 变量的作用域

 

1.本地的

    function update_counter (  ) {
      $counter++;
    }
    $counter = 10;
    update_counter(  );
    echo $counter;
    10

 2. 全局的

 用global声明

    function update_counter (  ) {
        global $counter;
        $counter++;
    }
    $counter = 10;
    update_counter(  );
    echo $counter;
    11

一种更复杂的更新全局变量的方式,使用PHP的$GLOBALS数组:

    function update_counter (  ) {
        $GLOBALS[counter]++;
    }
    $counter = 10;
    update_counter(  );
    echo $counter;
    11

 3. static

function update_counter (  ) {
      static $counter = 0;
      $counter++;
      echo "Static counter is now $counter\n";
    }
    $counter = 10;
    update_counter(  );
    update_counter(  );
    echo "Global counter is $counter\n";
    Static counter is now 1
    Static counter is now 2
    Global counter is 10

 4. 函数参数

function greet ($name) {
      echo "Hello, $name\n";
    }
    greet("Janet");
    Hello, Janet
 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值