Php echo if two conditions are true

The actual code is as follows:

if (file_exists($filename)) {echo $player; } else { echo 'something'; 

but it displays the player even if the identifier 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 empty with

 if(empty($id)) echo "text"; 

but I don’t know how to combine them

Can someone help me?

Thanks for all your code examples, but I still have a problem:

How can I check if $ id is not empty and then repeat the rest of the code

+6
source share
6 answers
 if (!empty($id) && file_exists($filename)) 
+9
source

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

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

This is fundamental PHP. Reading material:

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

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

+4
source

You need a logical AND operator :

 if (file_exists($filename) AND !empty($id)) { echo $player; } 
+4
source
 if (file_exists($filename) && !empty($id)){ echo $player; }else{ echo 'other text'; } 
+2
source

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

 if (file_exists($filename) && $id != '') { echo $player; } else { echo 'something'; } 
+1
source

Using the ternary operator:

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

Using the if-else clause:

 if ( (!empty($id)) && file_exists($filename) ) { echo 'OK'; } else { echo 'not OK'; } 
+1
source

Source: https://habr.com/ru/post/887557/


All Articles