php echo 如果两个条件都为真
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5927767/
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 echo if two conditions are true
提问by m3tsys
The actual code looks like this:
实际代码如下所示:
if (file_exists($filename)) {echo $player;
} else {
echo 'something';
but it displays the player even if the id is not called from the url
但即使未从 url 调用 id,它也会显示播放器
i need something like this:
我需要这样的东西:
check if $filename exists and $id it is not empty then echo $player
if else echo something else
i check if $id is not empty with
我检查 $id 是否不为空
if(empty($id)) echo "text";
but i don't know how to combine both of them
但我不知道如何将它们结合起来
Can somebody help me?
有人可以帮助我吗?
Thank you for all your code examples but i still have a problem:
感谢您提供所有代码示例,但我仍然有问题:
How i check if $id is not emptythen echo the rest of code
我如何检查 $id是否不为空然后回显其余的代码
回答by Pentium10
if (!empty($id) && file_exists($filename))
回答by Wesley Murch
Just use the AND
or &&
operator to check two conditions:
只需使用AND
or&&
运算符来检查两个条件:
if (file_exists($filename) AND ! empty($id)): // do something
It's fundamental PHP. Reading material:
它是基本的 PHP。阅读材料:
http://php.net/manual/en/language.operators.logical.php
http://php.net/manual/en/language.operators.logical.php
http://www.php.net/manual/en/language.operators.precedence.php
http://www.php.net/manual/en/language.operators.precedence.php
回答by lonesomeday
You need the logical AND
operator:
您需要逻辑AND
运算符:
if (file_exists($filename) AND !empty($id)) {
echo $player;
}
回答by Tom Claus
if (file_exists($filename) && !empty($id)){
echo $player;
}else{
echo 'other text';
}
回答by Tadeck
Using ternary operator:
使用三元运算符:
echo (!empty($id)) && file_exists($filename) ? 'OK' : 'not OK';
Using if-else clause:
使用 if-else 子句:
if ( (!empty($id)) && file_exists($filename) ) {
echo 'OK';
} else {
echo 'not OK';
}
回答by afarazit
you need to check $id
along with file_exists($filename)
as follows
你需要检查$id
沿file_exists($filename)
如下
if (file_exists($filename) && $id != '') {
echo $player;
} else {
echo 'something';
}