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

发布于 2024-10-17 11:14:45 字数 590 浏览 10 评论 0原文

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

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

那么我只想在有值的情况下打印结果。这适用于一个变量,因此您可以:

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

我尝试了类似的方法

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

但是当然,它不起作用。

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:

$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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(15

分分钟 2024-10-24 11:14:45

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

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

旁注:“mempty”中的前导“m”代表“多个”。你可以随意称呼它,但这似乎是最短/最简单的命名方式。而且……说起来也很有趣。 :)

更新 10/10/13: 我可能应该补充一点,与empty() 或isset() 不同,如果你向mempty() 函数传递一个未定义的变量或一个非- 函数,它将会大喊血腥谋杀。现有的数组索引。

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.

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. :)

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.

妞丶爷亲个 2024-10-24 11:14:45

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

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

但是您可能希望将其分成三个 if,因此您可以应用单独的额外文本:

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

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

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 (!empty($vFoo)) {
   $result .= "<li>$vFoo</li>";
}
if (!empty($vSomeValue)) {
   $result .= "<li>$vSomeValue</li>";
}
if (!empty($vAnother)) {
傲影 2024-10-24 11:14:45

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

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.

陈年往事 2024-10-24 11:14:45

使用布尔/逻辑运算符:

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

此外,您可能希望使用 or 而不是 and 将它们连接起来。正如您所看到的,这可以为您提供相当大的灵活性。

use boolean/logical operators:

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

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

残疾 2024-10-24 11:14:45

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

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

    return true;
}

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.

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

    return true;
}
阳光下慵懒的猫 2024-10-24 11:14:45

只需添加 imkingdavid 的帖子;

如果任何参数为空,要返回 TRUE,请查看以下内容。

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

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


如果提供了多个参数,则仅当所有参数均已设置时 isset() 才会返回 TRUE。计算从左到右进行,一旦遇到未设置的变量就停止。

PHP:isset - 手册

Just adding to the post by imkingdavid;

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

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 FALSE if everything is not empty.


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.

PHP: isset - Manual

念﹏祤嫣 2024-10-24 11:14:45

节省一些打字时间并将其放入循环中......

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

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

foreach (array('vFoo','vSomeValue','vAnother') as $varname) {
  if (!empty($varname)) $result .= '<li>'.$varname.'</li>';
}
不爱素颜 2024-10-24 11:14:45

正如其他人指出的, empty() 仅接受一个参数,因此您必须使用

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

but 之类的东西 if (!empty($v)) is与 if($v) 相同,因此您也可以使用:

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

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

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

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

if ($v1 && $v2 ...)
云淡风轻 2024-10-24 11:14:45

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

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

您还可以验证键索引值

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

对于 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
}

输出:

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

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

Why not just loop through the $item array

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()

$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)
来世叙缘 2024-10-24 11:14:45

我不是 100% 确定我明白你想要做什么,但这里有几个可能的答案:

仅当每个变量不为空时才会返回它:

function printIfNotEmpty($var) {
  if (!empty($var)) {
    return '<li>' . $var . '</var>';
  }
  return '';
}
$result .= printIfNotEmpty($vFoo);
$result .= printIfNotEmpty($vSomeValue);
$result .= printIfNotEmpty($vAnother);

或者如果没有,这个将添加所有三个变量其中是空的:

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

I'm not 100% sure I understand what you're trying to do, but here are a couple of possible answers:

This will return each variable only if it isn't empty:

function printIfNotEmpty($var) {
  if (!empty($var)) {
    return '<li>' . $var . '</var>';
  }
  return '';
}
$result .= printIfNotEmpty($vFoo);
$result .= printIfNotEmpty($vSomeValue);
$result .= printIfNotEmpty($vAnother);

Or this one will add all three of them if none of them are empty:

if (!empty($vFoo) && !empty($vSomeValue) && !empty($vAnother)) {
  $result .= '<li>' . $vFoo . '</li>';
  $result .= '<li>' . $vSomeValue . '</li>';
  $result .= '<li>' . $vAnother . '</li>';
}
吃兔兔 2024-10-24 11:14:45

伙计们,非常感谢你们的所有回复,我根据所有可能的答案创建了一个脚本,这实际上帮助我学习 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";
}

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

$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";

谢谢你们!

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

$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!

吲‖鸣 2024-10-24 11:14:45

为什么不只是这样:

foreach(array_filter($input_array) as $key => $value) {
    // do something with $key and $value
}

Why not just like this:

foreach(array_filter($input_array) as $key => $value) {
    // do something with $key and $value
}
战皆罪 2024-10-24 11:14:45

只是为了记录: array_reduce 也可以。

if (!array_reduce($item, function($prev_result,$next) {
    return $prev_result || empty($next); // $prev_result will be initial null=false
})) {
    // do your stuff
}

Just for the record: array_reduce works as well.

if (!array_reduce($item, function($prev_result,$next) {
    return $prev_result || empty($next); // $prev_result will be initial null=false
})) {
    // do your stuff
}
浮华 2024-10-24 11:14:45

如果仅使用字符串,您可以尝试以下操作:

if(empty($str1.$str2.$str3)){ /* ... */ }

注意,empty(0) 将返回 true,而empty('0'.'0') 和empty('0') 将返回true。 t。

If working only with strings, you may try the following:

if(empty($str1.$str2.$str3)){ /* ... */ }

Note, that empty(0) would return true, while empty('0'.'0') and empty('0') won't.

野心澎湃 2024-10-24 11:14:45

您可以尝试 php &&/ and 运算符。他们都是一样的。

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

You can try php &&/ and operator. They both are same.

if(!empty($vFoo) && !empty($vSomeValue) && !empty($vAnother) {
   $result .= "<li>$vFoo</li>"
   $result .= "<li>$vSomeValue</li>"
   $result .= "<li>$vAnother</li>"
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文