如何检查php变量

时间:2017-03-14 14:19:29

标签: php

我必须检查PHP-Variables是否为空,我有一些代码,但我想知道它是否可以更快。

丹尼尔

if ($beer1 == "" || $beer2 == "" || $beer3 == "" || $beernumber1 == "" || $beernumber2 == "" || $beernumber3 == "" || $wine1 == "" || $wine2 == "" || $wine3 == "" || $winenumber1 == "" || $winenumber2 == "" || $winenumber3 == "" || $name == "" || $adress == "" || $zipcode == "" || $city == "" || $email == "" || $nieuwsbriefjaofnee == "" || ) {
    # code...
} else {
    # code...
}

1 个答案:

答案 0 :(得分:0)

您可以使用php为您提供的 empty 函数。见the manual here

您也不希望在if条件中有这么多条件。您可以使用值创建一个数组,然后循环它。例如:

foreach ($array as $item) {
    if (empty($item)) {
        do_sth();
    }
}

然后根据你的情况展开它......