PHP 的 strcmp()...

出自「PHP的strcmp函数引发的安全问题」。

拿 PHP 5.5 來測,這兩個都沒什麼問題:

php > var_dump(strcmp('a', 'a'));
int(0)
php > var_dump(strcmp('a', 'b'));
int(-1)

接下來測這個:

php > var_dump(strcmp('a', array()));
PHP Warning:  strcmp() expects parameter 2 to be string, array given in php shell code on line 1
NULL

沃槽...

這代表這個寫法是有問題的:

if (!strcmp($v, $v_input)) {
    // If same...
}

如果先不管 warning,還是要用 strcmp 的話,可能得寫成:

if (0 === strcmp($v, $v_input)) {
    // If same...
}

不過用原生的操作元應該是比較好的解法?

if ($v === $v_input) {
    // If same...
}

這真是太迷人了... -_-