I am dynamically grabbing image links from a database and inserting them into image tags. This php code checks if there is a link or not. If there is a link, that link is inserted into the img code. If there is no link, a generic “image coming soon” picture is used in its place.
echo '<tr>
<td width="80" height="46">
<a href="displayitem.php?item_id='.$top_product_id.'">';
if (!is_null($top_link) || $top_link == "")
{
echo'<img src = "'.$top_link.'" alt="" width="64" height="63" />';
//echo $top_link;
}
else
{
echo'<img src = "imagen/imagecomingsoon.png" alt="" width="30%" height="40%" />';}
echo'</td><td width="108"><a href="displayitem.php?item_id='.$top_product_id.'">'.$final_name.'</a>';
echo'</td>
</tr>';
I have a strange bug that in the event there is no image to link, instead of using the generic image, I get a blank. When I inspect the blank box, I see that the img tag is not generating properly. There are no quotes, or even an equals sign after the src designation. It looks like this.
<img src alt="" width="64" height="63">
Whey would the entire equals sign dissapear along with the quotes and link in the event I want to use a generic image?
You should replace
!is_null($top_link) || $top_link == ""with!is_null($top_link) && $top_link != "". But i would replace the whole condition:This would let you not care about different types of emptiness in php.