unemployment Posted December 3, 2011 Share Posted December 3, 2011 Please help me sort out the syntax for this <?php echo '<a href="/u/${user_info["username"]}"><img src="', getUserAvatar($user_info['username']), "\" class=\"avatar f_left small\" title=\"${user_info['display_name']}\" alt=\"${user_info['display_name']}\" /></a>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/252396-formatting-issue/ Share on other sites More sharing options...
xyph Posted December 3, 2011 Share Posted December 3, 2011 You have single quotes in your first echo argument. The variable within won't get parsed. Quote Link to comment https://forums.phpfreaks.com/topic/252396-formatting-issue/#findComment-1293997 Share on other sites More sharing options...
unemployment Posted December 3, 2011 Author Share Posted December 3, 2011 You have single quotes in your first echo argument. The variable within won't get parsed. Please explain in more detail. I feel like I have single quotes in the second argument. Quote Link to comment https://forums.phpfreaks.com/topic/252396-formatting-issue/#findComment-1293998 Share on other sites More sharing options...
btellez Posted December 3, 2011 Share Posted December 3, 2011 echo '<a href="/u/'. $user_info["username"] .'"><img src="'. getUserAvatar($user_info['username']) .'" class="avatar f_left small" title="'. $user_info['display_name'] .'" alt="'.$user_info['display_name'].'" /></a>'; // OR echo '<a href="/u/', $user_info["username"], '"><img src="', getUserAvatar($user_info['username']), '" class="avatar f_left small" title="', $user_info['display_name'], '" alt="', $user_info['display_name'], '" /></a>'; // OR wrapped in double quotes... echo "<a href=\"/u/{$user_info["username"]}\"><img src=\"", getUserAvatar($user_info['username']), "\" class=\"avatar f_left small\" title=\"{$user_info['display_name']}\" alt=\"{$user_info['display_name']}\" /></a>"; Note: that the comas and the periods are not doing the same thing, even though the output may be the same. Although you can use single and double quotes, try to stick to one as your "outside" wrappers to minimize confusion. Quote Link to comment https://forums.phpfreaks.com/topic/252396-formatting-issue/#findComment-1293999 Share on other sites More sharing options...
dweeber Posted December 3, 2011 Share Posted December 3, 2011 I tend to use single quotes for html output... This allows me to use the double quotes for html elements without having to think about it much and place variables outside of the single quotes to get them to work properly like: <?php ... echo '<a href="/u/' . $user_info['username'] . '"> <img src="' . getUserAvatar($user_info['username']) . '" class="avatar f_left small" title="' . $user_info['display_name'] . '" alt="' . $user_info['display_name'] . '" /></a>'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/252396-formatting-issue/#findComment-1294000 Share on other sites More sharing options...
xyph Posted December 3, 2011 Share Posted December 3, 2011 Singe and double quoted strings are different in PHP Here's some reading http://php.net/manual/en/language.types.string.php Quote Link to comment https://forums.phpfreaks.com/topic/252396-formatting-issue/#findComment-1294006 Share on other sites More sharing options...
scootstah Posted December 3, 2011 Share Posted December 3, 2011 Single quotes output what is literally between them. $foo = 'bar'; echo 'foo $foo'; So this would echo "foo $foo". Double quotes will parse variables and functions within the string. $foo = 'bar'; echo "foo $foo"; So this would echo "foo bar". If you want to use variables inside single quotes you have to concatenate. $foo = 'bar'; echo 'foo ' . $foo; This would echo "foo bar". Quote Link to comment https://forums.phpfreaks.com/topic/252396-formatting-issue/#findComment-1294019 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.