Re: Preferred Syntax

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 11-12-14 01:10 PM, David Harkness wrote:
On Wed, Dec 14, 2011 at 4:59 AM, Rick Dwyer<rpdwyer@xxxxxxxxxxxxx>  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>";


On Wed, Dec 14, 2011 at 9:09 AM, Peter Ford<pete@xxxxxxxxxxxxx>  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>';

+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'
    .' 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.

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux