如何使用&;检查php中的空变量;什么价值?
我有一个字符串:如何使用&;检查php中的空变量;什么价值?,php,Php,我有一个字符串: $string 如果我打印了它,没有任何内容返回给我: echo $string; // result in browser: blank! in html source code:  我试着用这段代码来理解$string是空的 if ($string!= ' ' && $string!= ' ' && $string!= '' && $string!= null && $st
$string
如果我打印了它,没有任何内容返回给我:
echo $string; // result in browser: blank! in html source code: 
我试着用这段代码来理解$string是空的
if ($string!= ' ' && $string!= ' ' && $string!= '' && $string!= null && $string!= ' ' && !empty($string))
echo 'true';
else
echo 'false';
但它总是给我“真实”的回答,为什么//我认为在$string中有很多空格或类似的内容一个更健壮的方法可能会将此作为起点
//correct entities
$plain=html_entity_decode($string);
if (preg_match('/[^\s]/', $plain)) {
//string contains some non-whitespace
}
我检查过了,应该可以:
$strings = ['', ' ', ' ', 's', 's ', ' s'];
foreach ($strings as $string)
{
$string = trim(str_replace(' ', ' ', $string));
if (strlen($string) != 0)
echo $string . ' is not empty';
else
echo $string . ' is empty';
echo '<br>';
}
您为
$string
分配了什么?您的逻辑错误。考虑一下,如果$string是“nbsp”-它不是''所以它评估为true!不管$string是什么,它都匹配其中一个测试<代码>$string不能同时为空。我想您是想使用|
或操作符而不是&
和
操作符。只使用纯文本…不包括html标记您还可以通过if(!(isset($string)| trim($question)==''| string!=''简化if语句
是的,我的Perl头就在那里。不管怎样,从那时开始编辑!修剪不会起作用:它会在PeeHaa评论的代码中起作用。如果你是对的,那么strlen()
而不是empty()
?
is empty
is empty
is empty
s is not empty
s is not empty
s is not empty