PHP Display Image IF Else Statement

Viewed 158

My code displays an image if the order status change matches the image name. However I have some order status' with no image and in this case there is an error where the picture cannot be found on the server.

I want to display an If Else statement where the image only shows if it matches the status name else if displays nothing.

<img src="<?php print get_template_directory_uri().'/images/truck-'.$order->status; ?>.png" />
2 Answers

You can use the php function file_exists(). Here an example snippet:

<?php
if(file_exists(get_template_directory_uri().'/images/truck-'.$order->status.'.png')) {
//display image
}
else {
//display a placeholder image
}
?>

You can check if the file exists on the server with file_exists(string $filename) Or use a try catch statement

Related