PHP:如果 !empty & empty
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2347061/
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
PHP: if !empty & empty
提问by Karem
So i have this form.. With 2 fields. "Youtube" and "link" I want to do if you have filled in YouTube, it should do this:
所以我有这个表格..有2个字段。“Youtube”和“link”我想做的如果你填过YouTube的话,应该是这样的:
if(!empty($youtube)) {
if ($pos === false) {
echo "Du skal indtaste youtube et URL, som starter med 'http://www.youtube.com/watch?..<br>";
echo "<br> Har du ikke din video p? YouTube, skal du ikke udfylde feltet, men kun 'Link' feltet.<br><br>";
echo "<a href='javascript:history.back();'>G? tilbage</a>";
}
}
}
This do its job, but i also want to check on the same if(), if nothing in link. So ive did this:
这完成了它的工作,但我也想检查相同的 if(),如果链接中没有。所以我这样做了:
if(!empty($youtube) && empty($link)) {
if ($pos === false) {
echo "Du skal indtaste youtube et URL, som starter med 'http://www.youtube.com/watch?..<br>";
echo "<br> Har du ikke din video p? YouTube, skal du ikke udfylde feltet, men kun 'Link' feltet.<br><br>";
echo "<a href='javascript:history.back();'>G? tilbage</a>";
}
}
But what if i want to check the opposite, if theres something in LINK and nothing in youtube? And if i want to check if theres nothing at all in those two?
但是,如果我想检查相反的内容,如果 LINK 中有内容而 youtube 中没有内容,该怎么办?如果我想检查这两个中是否没有任何内容?
回答by Felix Kling
if(!empty($youtube) && empty($link)) {
}
else if(empty($youtube) && !empty($link)) {
}
else if(empty($youtube) && empty($link)) {
}
回答by bart
Here's a compact way to do something different in all four cases:
这是在所有四种情况下做不同事情的紧凑方法:
if(empty($youtube)) {
if(empty($link)) {
# both empty
} else {
# only $youtube not empty
}
} else {
if(empty($link)) {
# only $link empty
} else {
# both not empty
}
}
If you want to use an expression instead, you can use ?:instead:
如果要改用表达式,可以?:改用:
echo empty($youtube) ? ( empty($link) ? 'both empty' : 'only $youtube not empty' )
: ( empty($link) ? 'only $link empty' : 'both not empty' );
回答by Eli
For several cases, or even just a few cases involving a lot of criteria, consider using a switch.
对于几种情况,甚至只是涉及很多标准的少数情况,请考虑使用 switch。
switch( true ){
case ( !empty($youtube) && !empty($link) ):{
// Nothing is empty...
break;
}
case ( !empty($youtube) && empty($link) ):{
// One is empty...
break;
}
case ( empty($youtube) && !empty($link) ):{
// The other is empty...
break;
}
case ( empty($youtube) && empty($link) ):{
// Everything is empty
break;
}
default:{
// Even if you don't expect ever to use it, it's a good idea to ALWAYS have a default.
// That way if you change it, or miss a case, you have some default handler.
break;
}
}
If you have multiple cases that require the same action, you can stack them and omit the break; to flowthrough. Just maybe put a comment like /*Flowing through*/ so you're explicit about doing it on purpose.
如果有多个case需要同一个action,可以堆叠起来,省略break;流过。只是可以添加像 /*Flowing through*/ 这样的评论,这样你就明确表示是故意这样做的。
Note that the { } around the cases aren't required, but they are nice for readability and code folding.
请注意,案例周围的 { } 不是必需的,但它们对于可读性和代码折叠非常有用。
More about switch: http://php.net/manual/en/control-structures.switch.php
更多关于开关:http: //php.net/manual/en/control-structures.switch.php

