Trying to use in line styling in the middle of a PHP echo

136 views Asked by At

Is there anyway I can get in-line styling within a PHP echo statement. I want to set the height and the width of an image, as when I try to apply the height and `width externally it makes no difference as the image loads before the style sets in.

I have tried this but doesn't seem to be making any difference what so ever...

<p><b>Profile Picture: </b>
<?php 
    $picture = $row['imagePath'];
        if (empty($picture)){
            echo "<img src='profiles/no-image.png' 'width=500' 'height=600' >";
        } else {
        echo "<img src='".$row['imagePath']."' 'width=500' 'height=600' >";    
        };
?></p>
2

There are 2 answers

0
rx2347 On BEST ANSWER

That doesn't work because you are not setting the quotes right.

This should do the trick:

echo "<img src='profiles/no-image.png' width='500' height='600' >";

You can apply styles the same way:

echo "<img src='profiles/no-image.png' style='width:500px;height:600px;'>";
0
ryantxr On

Here is an alternative. Use PHP like a template engine. This approach does not use echo statements to output HTML. Instead, dynamic elements are introduced as needed.

<p><b>Profile Picture: </b>
<?php 
$picture = $row['imagePath'];
if (empty($picture)) :
?>
    <img src="profiles/no-image.png" width="500" height="600" >
<?php else : ?>
    <img src="<?= $row['imagePath'] ?>" width="500" height="600" >
<?php endif ?>
</p>