tags:

views:

182

answers:

5

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

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

Only double quoted strings will parse PHP variables.

Rocket
It's more accurately known as variable interpolation.
BoltClock
-1, mostly because that is the ugliest bit of code I've seen. Why not simply concat the strings, following the pattern of the other `$row['username']` he has?
Josh K
thank, this is not giving me an error, it still deosnt parse the php vairbales, so it wnt display the pic, and i have made sure that the variable has the value
getaway
@Josh, I personally think it looks nicer this way.
Rocket
@getaway, I just tested it, and it does correctly parse the variables.
Rocket
@Rocket: Editing to remove `"` from the echoed html to avoid escaping them doesn't make it look cleaner.
Josh K
@Josh Because concatenation is slow and ugly.
meagar
@Josh It's personal preference. I think not concating looks neater.
Rocket
@meagar: You have to define slow. If your application is so incredibly crippled by using string concatenation then you are doing very wrong.
Josh K
@Josh I think it looks ugly too, but I have upvoted it, because it's syntactically correct.
Col. Shrapnel
@meagar don't ashame yourself with word "slow". It's inapplicable here. There is not a single performance issue in string operations. Only a fool would mention it.
Col. Shrapnel
@Col "Only a fool would mention it" is quite the debating tactic. You are certainly passionate about concatenation, I will leave it at that.
meagar
+4  A: 

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
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
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
@Bolt: Simple, swap the `.`'s with `,`'s.
Josh K
this code deosnt have an error, but it deos not diplay the image, it deosnt parse the php variables
getaway
@Getaway: Then you are doing something wrong. Is it within `<?php ... ?>` 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
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
@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
@getaway: Have you also checked to make sure that the image actually exists in that path?
Josh K
sorry about that i mispelled the row name, thank you very much!!!
getaway
@Josh what's wrong with putting variables in double quoted strings? Personally, I think that looks neater than concating strings and variables.
Rocket
Congrats on winning the thread! :)
BoltClock
@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
@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?
meagar
@meagar: The above example is a perfect one.
Josh K
@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
meagar
@meagar: And it got him quite a few answers thinking that is what was going on. See?
Josh K
@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.
Rocket
+3  A: 
?>
<tr><td><img src="images/<?=$row['picture']?>" alt="<?=$row['username']?>" /></td>
<?
Col. Shrapnel
-1 for using short tags.
Josh K
what's wrong with short tags?
Michael Haren
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
+1 for using short tags
meagar
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
Thanks for the link @Josh - I haven't used php for a long, long time so I appreciate the info
Michael Haren
+1 because you got nitpicked over a simple code snippet
GrandmasterB
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
Mixing HTML with short tags for output purposes is not only acceptable, it's what makes PHP a viable templating language.
meagar
@BoltClock computer chips? :p
aularon
@aularon: unicorns don't fancy such things.
BoltClock
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?
Col. Shrapnel
@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
@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.
Col. Shrapnel
+1 for using short tags
stereofrog
+1  A: 

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
+2  A: 

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 ?>
meagar
Would do a +10 if I could for the use of , instead of . (avoid concat when possible!)
AlexV
@AlexV someone had deceived you. There is nothing wrong in concatenation. Don't be scared by usual and natural things.
Col. Shrapnel
@Col. Concatenation is to be avoided when a faster less memory-intensive option exists which involves changing a period to a comma
meagar
@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.
Col. Shrapnel
@Col In most cases the difference in performance won't be noticeable, but it costs you *nothing* to do it the correct way.
meagar
LOL. there is no "incorrect" way. concat is correct in every way. And not in "most cases" bit in any real world application.
Col. Shrapnel
@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
@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.
Col. Shrapnel
@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
@Alex You don't think. You imagine. Feel the difference.
Col. Shrapnel
@Col Troll harder
meagar
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.
Col. Shrapnel
@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.
meagar
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.
Col. Shrapnel