我有三个变量,它们可能包含数据,也可能不包含数据。
让我们叫他们$name、$address和$phone。
假设我想把它们组合成一个var,$contact,每个都在一行上。
$contact = $name.'<br />'.$address.'<br />'.$telephone;
然后CCD_ 2被放入一个数组中。该数组最终通过foreach循环,不处理任何等于''
的变量。如何判断除了两个<br />
标记之外的字符串是否为空?我尝试过将$contact复制到$test_contact并运行str_replace('<br />', '', $test_contact;
,但即使所有变量都设置为''
,$test_contact == ''
的计算结果仍然为false。在str_replace()
之后,我也尝试过trim()
,但这也不起作用。我也尝试过使用===
,但再次失败。
我错过了什么?有更好的方法吗?
标记
走str_replace
路线肯定有效:
if(trim(str_replace('<br />', '', $test_contact)) == '') {
echo 'empty';
}
但是,为什么不在将其插入数组之前进行检查呢?
if(strlen($name) + strlen($address) + strlen($telephone) == 0) {
// don't insert
}
if(strlen(str_replace("<br />", "", $test_contact)) === 0) {
//empty
}
插入前检查:
if ($name.$address.$telephone == '')
则为空