PHP为空:仅将一个0作为字符串?

时间:2013-09-28 18:00:33

标签: php string

我想将一个0作为字符串仅用于数据库注入,而不是000000作为字符串。

$post["content"] = '00';

if (empty($post["content"]) && $post["content"] !== '0') 
{
     echo "this field must not be empty!";
}

它不会将其作为错误返回。

我该怎么办?

2 个答案:

答案 0 :(得分:1)

改为使用!trim()

<?php
$post["content"] = '00';


if ($post["content"] !== '0' && !trim($post["content"], '0')) 
{
    echo "this field must not be empty!";
}
?>

答案 1 :(得分:0)

你的测试是错误的。

$single ="0";
$double = "00";

var_dump(empty($single)); // true
var_dump(empty($double)); // false

双零不为空。

请勿使用empty()进行验证。你是否从头脑中知道哪些值被认为是“空的”?有一些惊喜,这足以让我避免这种功能。