Skip to content
Advertisement

Show image only if jsonObj has a value [closed]

If <?php echo $jsonObj->data->image; ?> has a value, like 1234.jpg I want to show this:

<img src="https://www.example.com/img/<?php echo $jsonObj->data->image; ?>"  />

If <?php echo $jsonObj->data->image; ?> has no value, then <img src="https://www.example.com/img/<?php echo $jsonObj->data->image; ?>" /> should remain hidden.

Any help please?

Advertisement

Answer

Use php if-else statement for that.

<?php
   if(!empty($jsonObj->data->image)) {
?>
     <img src="https://www.example.com/img/<?php echo $jsonObj->data->image; ?>"  />
<?php
   }
?>

You can add else part and do whatever you want.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement