-3

I want to write html image tag inside php but the below code is not working, Please help.

<?php 
....
....
echo '<img src="../admin/upload/'<?=$display_img?>" width="120px" height="120px"/>
.....
?>
0

2 Answers 2

4

You don't need the php tags you just need to break out of the string:

$width = 120;
echo '<img src="../admin/upload/' . $display_img . '" width="' . $width . 'px" height="120px"/>';
Sign up to request clarification or add additional context in comments.

9 Comments

thanks for your answer but it is not working there is syntax error some where in this line.....
Fixed up the typo in the answer.
Fixed the other typo :P
@CarlMarkham LMAO. Cheers for that. I swear my head was tilted for a second looking at it. +1
Yeh, quotes can be a pain when there is no syntax highlighting
|
0
  1. you are inserting a php code block <? ?> when you are already in another.
  2. <?=$display_img?> the equal sign doesn't belong here and the code is attached to the php tags, so the compiler may not be able to read it. It was better this way <?php echo( $display_img ) ?>, of course if you weren't already in php.
  3. Echoing is a bit messy for me, I would've exited php first like this.

    <?php
    ....
    ....
    ?>
    
    <img src="../admin/upload/<?php echo( $display_img ) ?>" width="120px" height="120px"/>
    
    <?php
    .....
    ?>
    

or here is your original code corrected:

<?php 
....
....
echo '<img src="../admin/upload/' . $display_img . '" width="120px" height="120px" />';
// OR 
echo '<img src="../admin/upload/{$display_img}" width="120px" height="120px" />';
.....
?>

4 Comments

Yes, he did not close the php block with ?> before using another block inside it.
echo is a statement not a function. No need for brackets.
True, I encourage using brackets for a cleaner look and reading;
Prior to PHP 5.4.0, this short syntax only works with the short_open_tag configuration setting enabled for using <?=

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.