On 12/14/2011 11:50 PM, Ross McKay wrote: > On Wed, 14 Dec 2011 07:59:46 -0500, Rick Dwyer wrote: > >> Can someone tell me which of the following is preferred and why? >> >> echo "<a style='text-align:left;size:14;font-weight:bold' href='/ >> mypage.php/$page_id'>$page_name</a><br>"; >> >> echo "<a style='text-align:left;size:14;font-weight:bold' href='/ >> mypage.php/".$page_id."'>".$page_name."</a><br>"; >> [...] > > Just to throw in yet another possibility: > > echo <<<HTML > <a style="text-align:left;size:14;font-weight:bold" > href="/mypage.php/$page_id">$page_name</a><br> > HTML; > > I love HEREDOC for slabs of HTML, sometimes SQL, email bodies, etc. > because they allow you to drop your variables into the output text > without crufting up the formatting with string concatenation, AND they > allow you to use double quotes which can be important for HTML > attributes that may contain single quotes. > > So whilst either above option is fine for the specific context, I prefer > HEREDOC when there's attributes like href. > > But what is "preferred" is rather dependent on the "preferrer". I second this example, with one minor change, I would add '{' and '}' around variables. echo <<<HTML <a style="text-align:left;size:14;font-weight:bold" href="/mypage.php/{$page_id}">{$page_name}</a><br> HTML; This works for $variables, $objects, and variable functions calls. But doesn't work if you try to call functions directly (bummer). $ cat variable_usage.php <?php $v1 = 'Variable 1'; $o1 = new stdClass(); $o1->v1 = 'Object 1'; function something($str) { return "...{$str}..."; } $f1 = "something"; echo <<<_ {$v1} {$o1->v1} {$f1('Function 1')} {something('F1')} _; ?> Results in this $ php -f variable_usage.php Variable 1 Object 1 ...Function 1... {something('F1')} This is why I like heredoc syntax over pretty much everything else. -- Jim Lucas http://www.cmsws.com/ http://www.cmsws.com/examples/ http://www.bendsource.com/ C - (541) 408-5189 O - (541) 323-9113 H - (541) 323-4219 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php