Home  >  Article  >  Backend Development  >  Android programmers learn PHP development (7)-Strengthen variable type judgment-PhpStorm

Android programmers learn PHP development (7)-Strengthen variable type judgment-PhpStorm

黄舟
黄舟Original
2017-03-02 10:00:331257browse

Forced conversion and variable type judgment

<?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(); // 判断是否为有效的函数名


The above is how Android programmers learn PHP development (7)-Forced variable type judgment-PhpStorm For more related content, please pay attention to the PHP Chinese website (www.php.cn)!



Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn