I have a php code that does this:
if (file_get_contents($businessData['header_image'],0,null,0,1) !== false) {
$headerBackingImage = $businessData['header_image'];
}
else {
$headerBackingImage = "default_image_url.jpg";
}
The purpose is to establish if the business has a valid image file at the defined location, to then use that file URL in the webpage. The $businessData is an array of outputs from a database.
what I am finding is that with some database rows the $businessData['header_image'] value is empty, and this is causing warnings:
PHP Warning: file_get_contents(): Filename cannot be empty
So obviously, the precursor to the above statement is to check that $businessData['header_image'] is not empty. Easily done with PHP empty() function.
I could structure it as the following:
if (!empty($var)){
if (file_get_contents($var)){
///file exists!
}
else {
///file does not exist, use default.
}
}
else {
///file does not exist, use default.
}
But this would mean repeating the else clause inside and outside of the IF EMPTY check.
My question
So - How about this layout to maintain a single IF{...} statement and not need to repeat the ELSE{...} clause :
if (!empty($businessData['header_image']) && file_get_contents($businessData['header_image'],0,null,0,1) !== false){ ... }
So, Does PHP bother checking the second condition in the IF statement if the first condition returns FALSE?
Would the above solution still return a WARNING, because PHP still runs the file_get_contents function even after evaluating the first --empty()-- function as FALSE?
The stuff I've read about this from php.net and other places seems to only evaluate IF conditionals as single return blocks, as in the whole block between the brackets returns TRUE or FALSE rather than how PHP handles each sub part of the IF condition returning TRUE or FALSE individually.
Aucun commentaire:
Enregistrer un commentaire