php echo if two conditions are true

2020-07-07 04:23发布

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

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

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 empty then echo the rest of code

6条回答
可以哭但决不认输i
2楼-- · 2020-07-07 05:02

You need the logical AND operator:

if (file_exists($filename) AND !empty($id)) {
    echo $player;
}
查看更多
太酷不给撩
3楼-- · 2020-07-07 05:02

Using ternary operator:

echo (!empty($id)) && file_exists($filename) ? 'OK' : 'not OK';

Using if-else clause:

if ( (!empty($id)) && file_exists($filename) ) {
    echo 'OK';
} else {
    echo 'not OK';
}
查看更多
smile是对你的礼貌
4楼-- · 2020-07-07 05:04
if (!empty($id) && file_exists($filename))
查看更多
时光不老,我们不散
5楼-- · 2020-07-07 05:06

Just use the AND or && operator to check two conditions:

if (file_exists($filename) AND ! empty($id)): // do something

It's fundamental PHP. Reading material:

http://php.net/manual/en/language.operators.logical.php

http://www.php.net/manual/en/language.operators.precedence.php

查看更多
迷人小祖宗
6楼-- · 2020-07-07 05:09
if (file_exists($filename) && !empty($id)){
   echo $player;
}else{
   echo 'other text';
}
查看更多
在下西门庆
7楼-- · 2020-07-07 05:17

you need to check $id along with file_exists($filename) as follows

if (file_exists($filename) && $id != '') {
echo $player;

} else { 
echo 'something';
}
查看更多
登录 后发表回答