PHP陷阱,一些注意事项

判断的一些注意事项

count(false)  > 0 // true
count(0) > 0 // true
"随便一个字符串" == 0 // true
"不是0的一个字符串" == true // true

$a = "2级";
$b = "14级";

$b > $a // false 不要做这样的比较

isset("") === true // 

0 === false // false 


iconv("UTF-8", "GB2312", $text)  // 容易遇到特殊的字符,会截断
// 建议使用
iconv("UTF-8", "GB2312//TRANSLIT", $text)

exec($a, $b);  // $b 是负责记录输出,会不断累加

strlen("宝宝树") == 9
mb_strlen("宝宝树") == 3 //强烈建议使用 

mb_strimwidth("宝宝树babytree", 0, 7, "", "UTF-8") == "宝宝树b"  // 推荐在web端截字

function abcTest () {}
function abctest () {} 
//上面两个函数是一个,php会忽略大小写


//静态变量的问题
class A {
    static $x = 1;
    public static function fun() {
        echo self::$x;
    }
}

class B extends A {
    static $x = 2;
}

B::fun();   //输出是1,而不是2



// 判断是否是数字,用is_numeric,不要用is_int,因为在浏览器传过来的,是一个字符串,不是整型
is_numeric("1234")  // true
is_int("1234")   // false


$a = $a . $b; //不建议使用,效率低,和下面的效率相比,是一个数量级的差别

$a .= $b;

// memcache,tt的key不能有中文,空格 允许的字符集有 "a-Z", "0-9", "_", "-"

//feof死循环
// if file can not be read or doesn't exist fopen function returns FALSE
$file = @fopen("no_such_file", "r");
// FALSE from fopen will issue warning and result in infinite loop here
while (!feof($file)) {
}
原文地址:https://www.cnblogs.com/wangkongming/p/5163479.html