Re: Preferred Syntax
| From: | Robert Cummings | Date: | Wed, 14 Dec 2011 18:54:36 +0000 |
| Subject: | Re: Preferred Syntax | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-316014@lists.php.net to get a copy of this message | ||
On 11-12-14 01:10 PM, David Harkness wrote:
On Wed, Dec 14, 2011 at 4:59 AM, Rick Dwyer<rpdwyer@earthlink.net> wrote:+1 on the use of single quotes instead of double quotes. With a bytecode cache it's not really going to make a lick of difference, but using double quotes for HTML seems far more palatable to me. I too like to pull the variable out into code space. I also like to format my attributes for easy reading and commenting: echo '<a'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>";On Wed, Dec 14, 2011 at 9:09 AM, Peter Ford<pete@justcroft.com> wrote:Horses for courses. I use whatever I feel like at the time, and mix various styles, as long as it's readable!I agree with Peter here. I would bet that the string with embedded variables is parsed once when the file is loaded and turned into the same bytecode as the second form. If you are going to use the second style above, I would at least switch the quotes around so you can use the double-quotes in the HTML as that to me reads nicer and avoids the minor cost of scanning the string for embedded code. I also put spaces around the dots to make the variable concatenation easier to spot when skimming it.echo '<a style="text-align:left;size:**14;font-weight:bold"href="/mypage.php/' . $page_id . '">' . $page_name .'</a><br>';
.' style="text-align:left;size:**14;font-weight:bold"'
.' href="/mypage.php/'.$page_id.'"'
.'>'
.$page_name
.'</a>'
.'<br />';
Although, I usually only do the above for tags that have lots of attributes. Otherwise the following is more likely:
echo '<div class="some-class">'
.'Something something'
.'</div>';
As I said before though, a bytecode cache with any degree of optimization "should" make any particular style have zero impact on runtime (beyond original parse) by optimizing the strings into a minimal set of operations. For instance 'foo'.'fee' would be coverted to 'foofee' by the bytecode engine.
Cheers,
Rob.
--
E-Mail Disclaimer: Information contained in this message and any
attached documents is considered confidential and legally protected.
This message is intended solely for the addressee(s). Disclosure,
copying, and distribution are prohibited unless authorized.