Question

I'm using a loop to display SQL data. All the rows in my db contain some URL for an image src, however there is not actually an image stored at the location (yet) for some the URLs.

How would I default those 'bad links' to a particular image path. I was trying the 'is_null' function but I'm realizing it will not work the way as I am using it because there is in fact a URL coming from the dp - its just a bad link. Is there an alternative method for what I'm trying to do? A way to verify there is actually in image for the URL specified. Here's my dysfunctional code:

 if(is_null($smaller_img)){$smaller_img = "http://default image";}
Was it helpful?

Solution

If is_null is not working for you that means you are getting some other kind of "empty" value (perhaps an empty string?). You might want to find out why and fix that -- if the data is coming from SQL then it should definitely be null to signify the absence of an image.

That said, in this case you can simply use the ?: operator:

echo $smaller_img ?: "http://default image";
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top