未提交ajax时$_POST[';val';]=0


$_POST['val'] = 0 when ajax is not submitted

我不知道为什么,但在PHP脚本中,当帖子没有提交到页面时,变量值为0。。。

$use = isset( $_POST['use'] ) ? (int) $_POST['use'] : '';

现在没有Ajax,但以下if总是正确的:

// $use must be '' and '' != 0 or 1 right?
if ($use == 0 || $use == 1)

这不是正确的做法吗?为什么代码在那之间执行?

if ($use === 0 || $use === 1)

将检查变量的类型以及

http://php.net/manual/en/language.operators.comparison.php

在这种情况下,通过松散比较,0可能为假,''也可能被视为假,因此执行if('' == 0)是真