如何比较字符串或int类型


How to compare string or int type?

if($_GET['choice'] == (int))

if($_GET['choice'] == (string))

我得到一个错误。

所有GET参数都是字符串。如果你想确定它是字符串中的整数,那么你应该对它进行消毒

要检查字符串$_GET['choice']是否可以表示为整数,请使用ctype_digit(),例如

if (ctype_digit($_GET['choice'])) {
    // integer
}

你做错了。您的示例显示了CASTING:

$var = (int)"15";  // casts the string 15 as an integer

如果你想比较某物是否为INTEGER,你可以使用PHP中的is_int()函数。还有其他操作符可以对字符串、数组等执行此操作;

http://us2.php.net/manual/en/function.is-int.php