This is a discussion on Optimization in coding style within the PHP Language forums, part of the PHP Programming Forums category; Hi there, I was wondering is there a point in optimization in code style... I have used to put parenthesis ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
Hi there,
I was wondering is there a point in optimization in code style... I have used to put parenthesis around some php keywords, but they are not needed. For example, I always use: echo( "String..." ); but I could use echo "String..."; the same thing is with return: return( $tempHTML ); as opposed to return $tempHTML; Also is there a point to variate between string delimiters " and ' ? ie. is it better to use single quotes instead of double quotes if I don't won't any variable in a string? For example: 'This is a string...' as opposed to "This is a string...". Double quotes are needed, of course in other cases: "Frankly my dear, your name is ${nameOfTheUser}!" I know it sounds stupid, even hard to read, but what are your opinions? - Evanescent Lurker - |
|
|||
|
evanescent.lurker@gmail.com says...
> I was wondering is there a point in optimization in code style... I > have used to put parenthesis around some php keywords, but they are not > needed. > > For example, I always use: > echo( "String..." ); > but I could use > echo "String..."; Not needed, so don't use them. > the same thing is with return: > return( $tempHTML ); > as opposed to > return $tempHTML; Ditto. > Also is there a point to variate between string delimiters " and ' ? > ie. is it better to use single quotes instead of double quotes if I > don't won't any variable in a string? For example: > 'This is a string...' > as opposed to > "This is a string...". My preference is to use single quotes unless you are certain that the string will surely have an apostrophe, and then decide whether it will be better to escape the apostrophe or switch to double quotes. > Double quotes are needed, of course in other cases: > "Frankly my dear, your name is ${nameOfTheUser}!" No they're not, and my strongly preferred style is to clearly string escape and concatenate variables. For starters in my editor it is far superior for syntax highlighting and debugging, even if it is more typing: $string = 'My "nickname" is '.$myNickName.', what\'s yours?'; > I know it sounds stupid, even hard to read, but what are your opinions? IIRC there is also a (very slight) additional overhead is parsing double quoted as opposed to single quoted strings. Geoff M |
|
|||
|
One quick glance of an experienced eye allowed to understand the blurred
and almost unreadable Geoff Muldoon's handwriting: > My preference is to use single quotes unless you are certain that the > string will surely have an apostrophe, and then decide whether it will > be better to escape the apostrophe or switch to double quotes. Agreed. >> Double quotes are needed, of course in other cases: >> "Frankly my dear, your name is ${nameOfTheUser}!" > > No they're not, and my strongly preferred style is to clearly string > escape and concatenate variables. For starters in my editor it is far > superior for syntax highlighting and debugging, even if it is more > typing: > > $string = 'My "nickname" is '.$myNickName.', what\'s yours?'; > Hmmm... I'd say that it's a definetely less readable than: $string="My \"nickname\" is $myNickName, what's yours?"; and if you can use single quotes to surround "nickname": $string="My 'nickname' is $myNickName, what's yours?"; I really like to write readable code. Saves time and hassle. Besides, don't know about yours, but my editor highlights the variables surrounded by double-quotes as viariables. Cheers Mike |
|
|||
|
Michal Wozniak wrote:
<snip> > > $string = 'My "nickname" is '.$myNickName.', what\'s yours?'; > > > Hmmm... I'd say that it's a definetely less readable than: > $string="My \"nickname\" is $myNickName, what's yours?"; > > and if you can use single quotes to surround "nickname": > $string="My 'nickname' is $myNickName, what's yours?"; > > I really like to write readable code. Saves time and hassle. His version is much better than yours. Nowadays, I'm annoyed when someone uses double quotes:( -- <?php echo 'Just another PHP saint'; ?> Email: rrjanbiah-at-Y!com Blog: http://rajeshanbiah.blogspot.com/ |
|
|||
|
One quick glance of an experienced eye allowed to understand the blurred
and almost unreadable R. Rajesh Jeba Anbiah's handwriting: > His version is much better than yours. Nowadays, I'm annoyed when > someone uses double quotes:( Why? Maybe I need to re-think my position? Cheers Mike |
|
|||
|
<evanescent.lurker@gmail.com> wrote in message
news:1113433748.805122.306810@o13g2000cwo.googlegr oups.com... > Hi there, > > I was wondering is there a point in optimization in code style... I > have used to put parenthesis around some php keywords, but they are not > needed. Not really. You don't get much saving from stripping them out. And when you think about it, having the paratheses there makes it easier to replace "echo" with a function. > Also is there a point to variate between string delimiters " and ' ? > ie. is it better to use single quotes instead of double quotes if I > don't won't any variable in a string? For example: > 'This is a string...' > as opposed to > "This is a string...". Again, not really. The saving is practically nil. Personally I use double quotes for long sentences and single quotes from single letters or words. Looks a little neater, I think. |
|
|||
|
"Geoff Muldoon" <gmuldoonnospam@scu.edu.au> wrote in message
news:MPG.1cc85e50c86e830a98969f@newsgate.x-privat.org... > No they're not, and my strongly preferred style is to clearly string > escape and concatenate variables. For starters in my editor it is far > superior for syntax highlighting and debugging, even if it is more typing: > > $string = 'My "nickname" is '.$myNickName.', what\'s yours?'; The dot notation is confusing for people who jump between PHP and Javascript. |
|
|||
|
Chung Leong wrote:
<snip> > > Also is there a point to variate between string delimiters " and ' ? > > ie. is it better to use single quotes instead of double quotes if I > > don't won't any variable in a string? For example: > > 'This is a string...' > > as opposed to > > "This is a string...". > > Again, not really. The saving is practically nil. Personally I use double > quotes for long sentences and single quotes from single letters or words. > Looks a little neater, I think. It would be much better if you could benchmark the difference before advocating the wrong practice *purely* because you use it. -- <?php echo 'Just another PHP saint'; ?> Email: rrjanbiah-at-Y!com Blog: http://rajeshanbiah.blogspot.com/ |
|
|||
|
I noticed that Message-ID:
<1113477617.109936.233590@l41g2000cwc.googlegroups .com> from R. Rajesh Jeba Anbiah contained the following: >> Hmmm... I'd say that it's a definetely less readable than: >> $string="My \"nickname\" is $myNickName, what's yours?"; >> >> and if you can use single quotes to surround "nickname": >> $string="My 'nickname' is $myNickName, what's yours?"; >> >> I really like to write readable code. Saves time and hassle. > > His version is much better than yours. Nowadays, I'm annoyed when >someone uses double quotes:( better not look at any of my code then... -- Geoff Berrow (put thecat out to email) It's only Usenet, no one dies. My opinions, not the committee's, mine. Simple RFDs http://www.ckdog.co.uk/rfdmaker/ |
|
|||
|
|