3

sorry i know this is a basic question, but i have been trying for hours and i cnt seem to think what is wrong with this!

 echo '<tr><td><img src="images/$row['picture']" alt="' . $row['username'] . '" /></td>';

is thier a more cleaner way to do this and and error free.

thanks

getaway
  • 8,792
  • 22
  • 64
  • 94

5 Answers5

4

You are missing a few ''s in there.

I would do it as follows:

 echo '<tr><td><img src="images/' . $row['picture'] . '" alt="' . $row['username'] . '" /></td>';

There are a few other ways of doing this, but I wouldn't recommend either using short tags or inserting variables into strings ever. Doesn't matter if it is double quoted, terminate the string and concatenate. It is much simpler on the eyes and makes for cleaner code.

You could even avoid string concatenation completely by using ,'s to separate echo arguments.

 echo '<tr><td><img src="images/', $row['picture'], 
      '" alt="', $row['username'], '" /></td>';
Josh K
  • 28,364
  • 20
  • 86
  • 132
  • Also could be "you have too many single-quotes", because of the single-quotes in `$row['picture']`, which should not be inside a string that's surrounded by single-quote characters. @Josh, you beat me to this answer. =) – anonymous coward Sep 08 '10 at 19:37
  • It'll be even simpler on the eyes if the string is long enough to warrant using line breaks to separate the various parts. – BoltClock Sep 08 '10 at 19:44
  • @Bolt: Simple, swap the `.`'s with `,`'s. – Josh K Sep 08 '10 at 19:46
  • this code deosnt have an error, but it deos not diplay the image, it deosnt parse the php variables – getaway Sep 08 '10 at 19:47
  • @Getaway: Then you are doing something wrong. Is it within `` tags? Is the file named `.php`? A little more information other then `doesn't parse the php variables` would go a long way in helping. – Josh K Sep 08 '10 at 19:49
  • the file is in a php file, and within php tags. and the variable has value because i have checked by echoing the file name!! even when i check the source on mozilla, theres no file name between the img src!! thanks – getaway Sep 08 '10 at 19:51
  • @getaway: I don't understand, how can you check if the variable has a value? Put a `print_r($row);` above this line and see what you get. – Josh K Sep 08 '10 at 19:53
  • @getaway: Have you also checked to make sure that the image actually exists in that path? – Josh K Sep 08 '10 at 19:55
  • 1
    @Josh what's wrong with putting variables in double quoted strings? Personally, I think that looks neater than concating strings and variables. – gen_Eric Sep 08 '10 at 19:58
  • 1
    @Rocket: You may personally feel it is better, but I can tell you that in professional code you will rarely, if ever, see people write that. It is not only confusing when reading but it can and will become an issue when it breaks something. – Josh K Sep 08 '10 at 20:04
  • 1
    @Josh I would contend that variable interpolation is neither confusing nor prone to breakage, and also sees wide usage. What can it possibly break that string concatenation couldn't also break? – user229044 Sep 08 '10 at 20:25
  • 1
    @meagar: The above example is a perfect one. – Josh K Sep 08 '10 at 20:35
  • @Josh Are you talking about the question? I don't believe there was any attempt at variable interpolation there; he simply doesn't understand strings and concatenation – user229044 Sep 08 '10 at 20:49
  • @meagar: And it got him quite a few answers thinking that is what was going on. See? – Josh K Sep 09 '10 at 01:06
  • @Josh: I don't see what you're saying. It didn't generate answers thinking that he was doing variable interpolation. It generated answers _suggesting_ that as a solution. – gen_Eric Sep 10 '10 at 13:27
3
 echo "<tr><td><img src='images/{$row['picture']}' alt='{$row['username']}' /></td>";

Only double quoted strings will parse PHP variables.

gen_Eric
  • 223,194
  • 41
  • 299
  • 337
3
?>
<tr><td><img src="images/<?=$row['picture']?>" alt="<?=$row['username']?>" /></td>
<?
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
  • 1
    See [Are PHP short tags acceptable](http://stackoverflow.com/questions/200640/are-php-short-tags-acceptable-to-use). In short they cause far more issues then they solve. – Josh K Sep 08 '10 at 19:38
  • 2
    Oh yay, another short tag fight. I'm out of votes so I can't do anything but grab popcorn. *Edit:* wouldn't you know it, I'm out of popcorn too, but these chips will do. – BoltClock Sep 08 '10 at 19:39
  • 1
    Thanks for the link @Josh - I haven't used php for a long, long time so I appreciate the info – Michael Haren Sep 08 '10 at 19:42
  • 5
    Omgosh who cares! If your server supports short tags, then use them. If you think you're going to change servers eventually, then don't use them. Stop voting this up/down based on short tags, that's stupid. Vote it up/down based on 'Does this answer the question well?' – animuson Sep 08 '10 at 19:44
  • 2
    Mixing HTML with short tags for output purposes is not only acceptable, it's what makes PHP a viable templating language. – user229044 Sep 08 '10 at 19:44
  • @BoltClock computer chips? :p – aularon Sep 08 '10 at 19:45
  • wow, what a discussion already! Personally I don't understand why it makes so much brawl. After all it's just a configuration option, one of many. Why noone fights against memory_limit or display_errors usage? – Your Common Sense Sep 08 '10 at 19:59
  • @Col: Probably because setting those differently doesn't break the vast amounts of code short tags would break. Does anyone feel safe recommending `magic_quotes`? – Josh K Sep 08 '10 at 20:06
  • @Josh well magic quotes are misconception, that's another matter. And memory_limit, when unable to be changed, may spoil the same amounts of code, making it not work. it's empty argue. – Your Common Sense Sep 08 '10 at 20:14
2

The first ' opens a string. You're accidentally closing that string at $row[', causing an error. You need to either close your string earlier and echo $row['picture'] separately (see below), or use double quotes (") which allow for variable interpolation.

Also, a word of advice: Don't use concatenation (the . operator) when echoing in PHP. Echo accepts multiple comma-separated arguments which incurs none of the overhead of string concatenation:

echo '<tr><td><img src="images/', $row['picture'], '" alt="',
     $row['username'], '" /></td>';

As a side note, the same applies to <?=, which is identical to <?php echo:

<?= $value, ' + 1 = ', $value + 1 ?>
user229044
  • 232,980
  • 40
  • 330
  • 338
  • 1
    Would do a +10 if I could for the use of , instead of . (avoid concat when possible!) – AlexV Sep 08 '10 at 19:44
  • 1
    @AlexV someone had deceived you. There is nothing wrong in concatenation. Don't be scared by usual and natural things. – Your Common Sense Sep 08 '10 at 20:08
  • @Col. Concatenation is to be avoided when a faster less memory-intensive option exists which involves changing a period to a comma – user229044 Sep 08 '10 at 20:17
  • @meagar oh please. All these issues are imaginable and not real. Please, do not listen to fools, grow up and have your own opinion. Learn to profile and to distinguish this crap from really important matters. It's interpreted language, dude. Your super-micro-excellent-perfect code being parsed at EACH damn user request! And it takes to use MEGABYTES of memory. So, don't whine for few bytes! Take my advice, learn to profile. I've seen many ppl who claimed such a nonsense. Everyone of them had terrible performance issues in their apps, 1000 times worst. Every friggin one of them. – Your Common Sense Sep 08 '10 at 20:28
  • @Col In most cases the difference in performance won't be noticeable, but it costs you *nothing* to do it the correct way. – user229044 Sep 08 '10 at 22:00
  • LOL. there is no "incorrect" way. concat is correct in every way. And not in "most cases" bit in any real world application. – Your Common Sense Sep 08 '10 at 22:13
  • @Col. Shrapnel Yeah I know it's real marginal and won't change much but it's still nice to know that you can save some bytes by just changing a dot for a comma. And I understand it's sometimes impossible to do so and I can live with it :) – AlexV Sep 09 '10 at 01:39
  • @Alex it not nice. It makes you think wrong way. Instead of thinking of profiling, improving algorithms, code reuse, coding standards and other and real things, it makes you think of saving bytes. As I have told to meagar, every time I inspect code of such a byte-saver, I find real performance-wise flaws, that makes things thousands times worst. this byte-saving is indeed terrible thing. – Your Common Sense Sep 09 '10 at 04:41
  • @Col. Shrapnel I don't think it makes me think the wrong way. I understand it's not much of an improvement but it's a nice thing to know. And I understand that you should look somewhere else to save CPU cycles/memory in you PHP app, but still it's a nice feature to know I think. But still I come from a C++/Delphi background... – AlexV Sep 09 '10 at 11:35
  • @Alex You don't think. You imagine. Feel the difference. – Your Common Sense Sep 09 '10 at 12:01
  • You do not understand what trolling is. PHP is hated because of billions of lamers using it and telling each other scary tales. You can hardly find real knowledge, but there are tons of bullshit like this "memory-saving tips". It makes me sick, not fun. – Your Common Sense Sep 09 '10 at 13:55
  • @Col I understand that you're intentionally abrasive and antagonistic with seemingly no other goal than to provoke negative responses. Pretty much textbook trolling brah. – user229044 Sep 09 '10 at 14:38
  • You are wrong. My last comment to Alex is not too informative but it is not to provoke anything. Actually I don't need any responses. The only thing I really wish is to make people to think at last. But you are right, it's useless. – Your Common Sense Sep 09 '10 at 14:49
1

You need to escape $row['picture'] as well

echo '<tr><td><img src="images/' . $row['picture'] . '" alt="' . $row['username'] . '" /></td>';

If you want a cleaner way consider the following:

echo "<tr><td><img src='images/{$row['picture']}' alt='{$row['username']}'/></td>";

If you have double quoted strings you can include array values inline as long as they're enclosed in { }. Ofcourse you have to change the double quotes in the html elements to single, or escape them.

Fanis Hatzidakis
  • 5,282
  • 1
  • 33
  • 36