Maison  >  Article  >  développement back-end  >  Android程序员学PHP开发(7)-强转变量类型判断-PhpStorm

Android程序员学PHP开发(7)-强转变量类型判断-PhpStorm

黄舟
黄舟original
2017-03-02 10:00:331212parcourir

强制转换 和 变量类型判断

<?php
    /**
     * 强制转换 和 变量类型判断
     */
    $int = 1000;
    echo gettype($int); // 打印结果:integer
    echo "<br>";

    $str = (String)$int; // 整形转字符串
    echo gettype($int); // 打印结果:integer
    echo "<br>";
    echo gettype($str); // 打印结果:string
    echo "<br>";

    $int2 = intval($str); // 字符串转整形
    echo gettype($int2); // 打印结果:string
    echo "<br>";

    $int3 = (int)($str); // 字符串转整形
    echo gettype($int3); // 打印结果:string
    echo "<br>";

    $a = "456e3abchello123 world";
    $b = 100;
    $c = $a + $b; // 相加的过程中,a别当做456e3科学计数法进行相加
    var_dump($c); // 打印结果:float(456100)
    echo "<br>";

    // 获取a的变量类型,判断是否为string
    if (gettype($a)=="string"){
        echo "gettype == string";
        echo "<br>";
    }

    // 判断a为空,是否为真;或者说判断a是否为空
    if (is_null($a)){
        echo "is_null == true";
        echo "<br>";
    }else{
        echo "is_null == false";
        echo "<br>";
    }

    // 判断a为数组,是否为真;或者说判断a是否为数组
    if (is_array($a)){
        echo "is_array == true";
        echo "<br>";
    }else{
        echo "is_array == false";
        echo "<br>";
    }

    // 判断a为字符串,是否为真;或者说判断a是否为字符串
    if (is_string($a)){
        echo "is_array == true";
        echo "<br>";
    }else{
        echo "is_array == false";
        echo "<br>";
    }

    is_bool(); // 判断是否为布尔值
    is_int(); // 判断是否为整形
    is_integer(); // 判断是否为整形
    is_long(); // 判断是否为整形
    is_float(); // 判断是否为浮点型
    is_double(); // 判断是否为浮点型
    is_real(); // 判断是否为浮点型
    is_string(); // 判断是否为字符串
    is_array(); // 判断是否为数组
    is_object(); // 判断是否为对象
    is_resource(); // 判断是否为资源类型
    is_null(); // 判断是否为空
    is_scalar(); // 判断是否为标量
    is_numeric(); // 判断变量是数字还是数字字符串.
    is_callable(); // 判断是否为有效的函数名


 以上就是Android程序员学PHP开发(7)-强转变量类型判断-PhpStorm的内容,更多相关内容请关注PHP中文网(www.php.cn)!



Déclaration:
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn