variables in img src
this is going to an easy one i think but for the life of me i cant get it to work.
i have a variables $id i want to put it in a img src that i have echo (eg. "uploaded/$id.jpg")
i have tried lots of way and looked all over the net and cant get it to work
echo "
<td>
开发者_如何学Python <img src="uploaded'.$id'.jpg">
</td>
";
this is what the echo looks like if anyone can tell me why it is not work would be a big help
It's because you're mixing your double quotes with single quotes and you forgot the .
concat operator after $id
. Try this:
echo '
<td>
<img src="uploaded/'.$id.'.jpg">
</td>
';
No one stated the obvious:
Separate HTML and PHP, don't echo HTML:
<td>
<img src="uploaded/<?php echo $id; ?>.jpg">
</td>
You are mixing single and double quotes. Try this:
echo "<td>
<img src='uploaded/{$id}.jpg' />
</td>";
You're using double quotes inside a double quoted string.
echo "<td>
<img src='uploaded{$id}.jpg'>
</td>
";
Your mixing of quote styles is the problem. This should work.
or
echo "<td>
<img src=\"uploaded{$id}.jpg\">
</td>
";
if you're worried about valid html... 100 ways to skin a cat.
Try this:
echo "<td>
<img src=\"uploaded/{$id}.jpg\">
</td>";
EDIT: I think you were missing a / in the image path. Is "uploaded" a directory?
You were mixing single and double quotes. Double quotes allow you to embed variable in them like this... "Hello, $name" but if you tried "name$firstBlue", PHP would assume you were trying to insert the value of $firstBlue. You'd have to use "name{$first}Blue" for that to work. Always placing variables in {curly braces} is a good habit, because it prevents silly mistakes.
why it is not working.
echo "//double quotes begins here
<td>
<img src="/*it will end here later portion will not be printed*/ uploaded'.$id'.jpg">
</td>
";
you can use
echo '<td>
<img src="uploaded/'.$id.'.jpg">
</td>
';
精彩评论