php 对不在数组中的多个变量使用 if(!empty)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4993104/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 16:28:48  来源:igfitidea点击:

Using if(!empty) with multiple variables not in an array

phpis-empty

提问by HGB

I am trying to polish some code with the if(!empty)function in PHP but I don't know how to apply this to multiple variables when they are not an array (as I had to do previously) so if I have:

我正在尝试使用if(!empty)PHP 中的函数来完善一些代码,但是当它们不是数组时(因为我以前必须这样做),我不知道如何将其应用于多个变量,所以如果我有:

$vFoo       = $item[1]; 
$vSomeValue = $item[2]; 
$vAnother   = $item[3];

Then I want to print the result only if there is a value. This works for one variable so you have:

然后我想只有在有值时才打印结果。这适用于一个变量,所以你有:

 if (!empty($vFoo)) {
     $result .= "<li>$vFoo</li>";
 }

I tried something along the lines of

我尝试了一些类似的东西

if(!empty($vFoo,$vSomeValue,$vAnother) {
    $result .= "<li>$vFoo</li>"
    $result .= "<li>$vSomeValue</li>"
    $result .= "<li>$vAnother</li>"
}

But of course, it doesn't work.

但当然,它不起作用。

回答by imkingdavid

You could make a new wrapper function that accepts multiple arguments and passes each through empty(). It would work similar to isset(), returning true only if all arguments are empty, and returning false when it reaches the first non-empty argument. Here's what I came up with, and it worked in my tests.

您可以创建一个新的包装函数,它接受多个参数并通过 empty() 传递每个参数。它的工作方式类似于isset(),只有当所有参数都为空时才返回true,并在遇到第一个非空参数时返回false。这是我想出的,它在我的测试中起作用。

function mempty()
{
    foreach(func_get_args() as $arg)
        if(empty($arg))
            continue;
        else
            return false;
    return true;
}

Side note: The leading "m" in "mempty" stands for "multiple". You can call it whatever you want, but that seemed like the shortest/easiest way to name it. Besides... it's fun to say. :)

旁注:“mempty”中的前导“m”代表“multiple”。您可以随心所欲地称呼它,但这似乎是命名它的最短/最简单的方法。此外……说起来很有趣。:)

Update 10/10/13:I should probably add that unlike empty() or isset(), this mempty() function will cry bloody murder if you pass it an undefined variable or a non-existent array index.

2013 年 10 月 10 日更新:我可能应该补充一点,与 empty() 或 isset() 不同,如果将 mempty() 函数传递给它一个未定义的变量或一个不存在的数组索引,它会哭得很惨。

回答by mario

You need to write a condition chain. Use &&to test multiple variables, with each its own empty()test:

你需要写一个条件链。使用&&测试多个变量,每个自己的empty()测试:

if (!empty($vFoo) && !empty($vSomeValue) && !empty($vAnother)) {

But you probably want to split it up into three ifs, so you can apply the extra text individually:

但您可能希望将其拆分为三个 if,以便您可以单独应用额外的文本:

if (!empty($vFoo)) {
   $result .= "<li>$vFoo</li>";
}
if (!empty($vSomeValue)) {
   $result .= "<li>$vSomeValue</li>";
}
if (!empty($vAnother)) {

回答by Andrew

empty()can only accept one argument. isset(), on the other hand, can accept multiple; it will return true if and only if all of the arguments are set. However, that only checks if they're set, not if they're empty, so if you need to specifically rule out blank strings then you'll have to do what kelloti suggests.

empty()只能接受一个论点。isset(),另一方面,可以接受多个;当且仅当设置了所有参数时,它才会返回 true。但是,这只检查它们是否已设置,而不检查它们是否为空,因此如果您需要专门排除空白字符串,那么您必须按照 kelloti 的建议进行操作。

回答by kelloti

use boolean/logical operators:

使用布尔/逻辑运算符:

if (!empty($vFoo) && !empty($vSomeValue) && !empty($vAnother)) {
    $result .= "<li>$vFoo</li>"
    ...
}

Also, you might want to join these with orinstead of and. As you can see, this can give you quite a bit of flexibility.

此外,您可能希望使用or而不是and. 如您所见,这可以为您提供相当多的灵活性。

回答by awm

Save yourself some typing and put it into a loop...

为自己节省一些输入并将其放入循环中......

foreach (array('vFoo','vSomeValue','vAnother') as $varname) {
  if (!empty($$varname)) $result .= '<li>'.$$varname.'</li>';
}

回答by Dylan

Just adding to the post by imkingdavid;

只是通过imkingdavid添加到帖子中;

To return TRUEif any of the parameters are empty, check out the below instead.

TRUE在任何参数为空时返回,请查看以下内容。

function mempty(...$arguments)
{
    foreach($arguments as $argument) {
        if(empty($argument)) {
            return true;
        }
    }
    return false;
}

This is closer to how isset()works, and is how i would expect empty()to work if supporting a variable length of parameters. This will also keep in line with how empty()works in that it will return FALSEif everything is notempty.

这更接近于如何isset()工作,empty()如果支持可变长度的参数,这也是我期望的工作方式。这也将与empty()工作方式保持一致,因为FALSE如果一切都不为空,它将返回。



If multiple parameters are supplied then isset() will return TRUE only if all of the parameters are set. Evaluation goes from left to right and stops as soon as an unset variable is encountered.

如果提供了多个参数,那么仅当设置了所有参数时,isset() 才会返回 TRUE。评估从左到右进行,并在遇到未设置的变量时立即停止。

PHP: isset - Manual

PHP: isset - 手册

回答by hovado

I use this function as an alternative to isset. It's a little simpler than @imkingdavid's one and return true, if all arguments (variables) are not empty, or return false after first empty value.

我使用这个函数来替代isset。它比@imkingdavid 的简单一点,如果所有参数(变量)不为空,则返回 true,或者在第一个空值后返回 false。

function isNotEmpty() {
    foreach (func_get_args() as $val) {
        if (empty($val)) {
            return false;
        }
    }

    return true;
}

回答by Eelvex

As others noted, empty()takes only one argument so you have to use something like

正如其他人指出的那样,empty()只需要一个参数,所以你必须使用类似的东西

if(!empty($v1) && !(empty($v2) ...)

butif (!empty($v))is the same thing as if($v)so you may also use:

if (!empty($v))if($v)所以你也可以使用相同的东西:

if ($v1 && $v2 ...)

回答by Phill Pafford

Why not just loop through the $item array

为什么不循环遍历 $item 数组

foreach($item as $v) {
    if(!empty($v))) {
        $result .= "<li>$v</li>";
    }
}

You could also validate of the key index value as well

您还可以验证键索引值

foreach($item as $key => $value) {
    if($key == 'vFoo') {
        if(!empty($value))) {
            $result .= "<li>$value</li>";
        }
    }
    // would add other keys to check here
}

For the empty() versus isset()

对于 empty() 与isset()

$zero = array(0, "0");

foreach($zero as $z) {
    echo "\nempty(): $z ";
    var_dump($z);
    var_dump(empty($z)); // Will return true as in it's empty
    echo "isset(): $z ";
    var_dump(isset($z)); // will return true as it has value
}

Output:

输出:

empty(): 0 int(0)
bool(true)
isset(): 0 bool(true)

empty(): 0 string(1) "0"
bool(true)
isset(): 0 bool(true)

回答by HGB

Guys, many thanks for all your responses, I kind of created a script based on all of our possible answers which is actually helping me with learning PHP's syntax which is the cause behind most of my errors :) So here it is

伙计们,非常感谢您的所有回复,我根据我们所有可能的答案创建了一个脚本,这实际上帮助我学习了 PHP 的语法,这是我大多数错误背后的原因:) 所以这里是

$vNArray ['Brandon']  = $item[3]; 
$vNArray['Smith']= $item[4]; 
$vNArray ['Johnson']= $item[5];
$vNArray ['Murphy']= $item[6];
$vNArray ['Lepsky']= $item[7];

foreach ($vNArray as $key => $value){

    if(!empty($value)){
        $result  .= "\t\t\t\t<li><strong>$key</strong>"  .$value.   "</li>\n";
}

I just need to figure out how to make some of these different now. So could I access each in the following way?

我现在只需要弄清楚如何使其中的一些不同。那么我可以通过以下方式访问每个吗?

$result  .= "\t\t\t\t<li><strong>$key[0]</strong>"  .$value. "</li>\n";
$result  .= "\t\t\t\t<li><a href="thislink.com">$key[3]</a>"  .$value. "</li>\n";

Thanks guys!

谢谢你们!