对不在数组中的多个变量使用 if(!empty)
我正在尝试使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(15)
您可以创建一个新的包装函数,它接受多个参数并通过empty() 传递每个参数。它的工作方式与 isset() 类似,仅当所有参数都为空时才返回 true,而当到达第一个非空参数时返回 false。这是我想出的,并且它在我的测试中有效。
旁注:“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.
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.
您需要编写一个条件链。使用
&&
测试多个变量,每个变量都有自己的empty()
测试:但是您可能希望将其分成三个 if,因此您可以应用单独的额外文本:
You need to write a condition chain. Use
&&
to test multiple variables, with each its ownempty()
test:But you probably want to split it up into three ifs, so you can apply the extra text individually:
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.使用布尔/逻辑运算符:
此外,您可能希望使用
or
而不是and
将它们连接起来。正如您所看到的,这可以为您提供相当大的灵活性。use boolean/logical operators:
Also, you might want to join these with
or
instead ofand
. As you can see, this can give you quite a bit of flexibility.我使用此函数作为 isset 的替代函数。它比 @imkingdavid 的简单一点,如果所有参数(变量)都不为空,则返回 true,或者在第一个空值后返回 false。
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.
只需添加 imkingdavid 的帖子;
如果任何参数为空,要返回
TRUE
,请查看以下内容。这更接近于
isset()
的工作方式,并且是我期望empty()
在支持可变长度参数时的工作方式。这也将与empty()
的工作方式保持一致,因为如果一切都不是,它将返回FALSE
空的。PHP:isset - 手册
Just adding to the post by imkingdavid;
To return
TRUE
if any of the parameters are empty, check out the below instead.This is closer to how
isset()
works, and is how i would expectempty()
to work if supporting a variable length of parameters. This will also keep in line with howempty()
works in that it will returnFALSE
if everything is not empty.PHP: isset - Manual
节省一些打字时间并将其放入循环中......
Save yourself some typing and put it into a loop...
正如其他人指出的,
empty()
仅接受一个参数,因此您必须使用but 之类的东西
if (!empty($v))
is与if($v)
相同,因此您也可以使用:As others noted,
empty()
takes only one argument so you have to use something likebut
if (!empty($v))
is the same thing asif($v)
so you may also use:为什么不只是循环遍历 $item 数组
您还可以验证键索引值
对于 empty() 与 isset()
输出:
Why not just loop through the $item array
You could also validate of the key index value as well
For the empty() versus isset()
Output:
我不是 100% 确定我明白你想要做什么,但这里有几个可能的答案:
仅当每个变量不为空时才会返回它:
或者如果没有,这个将添加所有三个变量其中是空的:
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:
Or this one will add all three of them if none of them are empty:
伙计们,非常感谢你们的所有回复,我根据所有可能的答案创建了一个脚本,这实际上帮助我学习 PHP 的语法,这是我大多数错误背后的原因:)所以这就是
我只需要现在就弄清楚如何使其中一些变得不同。那么我可以通过以下方式访问每个吗?
谢谢你们!
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
I just need to figure out how to make some of these different now. So could I access each in the following way?
Thanks guys!
为什么不只是这样:
Why not just like this:
只是为了记录: array_reduce 也可以。
Just for the record: array_reduce works as well.
如果仅使用字符串,您可以尝试以下操作:
注意,empty(0) 将返回 true,而empty('0'.'0') 和empty('0') 将返回true。 t。
If working only with strings, you may try the following:
Note, that empty(0) would return true, while empty('0'.'0') and empty('0') won't.
您可以尝试 php
&&
/and
运算符。他们都是一样的。You can try php
&&
/and
operator. They both are same.