This is a discussion on Optimization in coding style within the PHP Language forums, part of the PHP Programming Forums category; R. Rajesh Jeba Anbiah wrote: > Michal Wozniak wrote: > <snip> > >>>$string = 'My "...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
R. Rajesh Jeba Anbiah wrote:
> 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:( Annoyed? Why? |
|
|||
|
On Fri, 15 Apr 2005 07:35:03 +0100, Geoff Berrow <blthecat@ckdog.co.uk> wrote:
>I noticed that Message-ID: ><1113477617.109936.233590@l41g2000cwc.googlegroup s.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... Or mine. I like to use sprintf. $string = sprintf('My "nickname" is %s, what's yours?', $myNickName); OK, so I maybe wouldn't use it if there's just one argument. But you have a few arguments and you can do a nice spaced out call; sprintf's the original templating system ;-) If you have to HTML encode everything then it saves on temporaries or mangling the original variables. $string = sprintf( '<td>something something %s</td><td>something else %s</td>', htmlspecialchars($something1), htmlspecialchars($something2) ); You could have multiple concatenations. It's a style issue - unless your script is trivial, worrying about the performance of this is micro-optimisation - only worth doing when you've optimised the more important bits first. -- Andy Hassall / <andy@andyh.co.uk> / <http://www.andyh.co.uk> <http://www.andyhsoftware.co.uk/space> Space: disk usage analysis tool |
|
|||
|
On Wed, 13 Apr 2005 16:09:08 -0700, evanescent.lurker@gmail.com wrote:
> 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. No, coding style is for clarity, remember the parser only reads through your code once everytime your code is loaded ... or not at all if using the Zend optimising thingymebob. Loading unrequired code, repeated loading of libaries, inefficient program design and logic all have a far greater impact on system performance:- $sql = "SELECT [id], [name] FROM [foo] ORDER BY id"; $result = mysql_query($sql, $conn); while($row = mysql_fetch_array($result)) { if($row['id'] == 9) { echo "Row 9 exists"; } } It doesn't matter whether you use echo "Row 9 exists"; echo ( "Row 9 exists" ); echo 'Row 9 exists'; printf ( "Row 9 exists" ); the program code still processes too many database records just to find one row. > 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}!" Double quotes are used for indicating text which may have a variable or code embedded into it, and that the php interpreter should scan the text as it outputs it for tokens representing variables and then automatically replace them with the value of those variables. If you don't need to parse variables use single quotes. > I know it sounds stupid, even hard to read, but what are your opinions? Putting literals into the body of your program is evil! define('TXT_YOUR_NAME_IS', 'Frankly my dear, your name is %s!'); .. .. .. .. echo sprintf(TXT_YOUR_NAME_IS , $nameOfTheUser); This code could be very quickly maintained in French, German, Spanish as I don't need to change the logic of the program, just the values I declare at the start of the program. switch($_COOKIE['lang']) { case 'fr': include('lang/french.php'); break; case 'gr': include('lang/german.php'); break; case 'sp': include('lang/spanish.php'); break; default: include('lang/english.php'); break; } Each language has it's own declaration file and only the relivent one will be loaded. Compare that to switch($_COOKIE['lang']) { case 'fr': echo "Tu et ${nameOfTheUser}"; break; case 'gr': echo "Das namen est ${nameOfTheUser}"; break; case 'sp': echo "No speaka da language ${nameOfTheUser}"; break; default: echo "Frankly my dear, your name is ${nameOfTheUser}!"; break; } Where all four different languages have to be included even when only one is used. (Appologies to German/French/Spanish readers!) |
|
|||
|
On Fri, 15 Apr 2005 22:52:02 +0100, Andy Hassall wrote:
> On Fri, 15 Apr 2005 21:40:44 +0100, CJ Llewellyn <cj.llewellyn@gmail.com> > wrote: > >>echo sprintf( > > Yay, it's not just me that likes sprintf then :-) Yup. :) Although htmlspecialchars processing is just one of it's many talents, the same techniques can easily be applied to SQL statement building. >>This code could be very quickly maintained in French, German, Spanish as > > Although sprintf for localisation has the main problem that word order within > sentences isn't always the same; sprintf doesn't do ordered parameters. It may have been a bad example to give, I was trying to place emphasis on code/data seperation and efficiency of code maintenance. |
|
|||
|
On Fri, 15 Apr 2005 21:40:44 +0100, CJ Llewellyn <cj.llewellyn@gmail.com>
wrote: >echo sprintf( Yay, it's not just me that likes sprintf then :-) >This code could be very quickly maintained in French, German, Spanish as Although sprintf for localisation has the main problem that word order within sentences isn't always the same; sprintf doesn't do ordered parameters. -- Andy Hassall / <andy@andyh.co.uk> / <http://www.andyh.co.uk> <http://www.andyhsoftware.co.uk/space> Space: disk usage analysis tool |
|
|||
|
On Fri, 15 Apr 2005 22:12:01 +0100, CJ Llewellyn <cj.llewellyn@gmail.com>
wrote: >On Fri, 15 Apr 2005 22:52:02 +0100, Andy Hassall wrote: >> On Fri, 15 Apr 2005 21:40:44 +0100, CJ Llewellyn <cj.llewellyn@gmail.com> >> wrote: >> >>>echo sprintf( >> >> Yay, it's not just me that likes sprintf then :-) > >Yup. :) Although htmlspecialchars processing is just one of it's many >talents, the same techniques can easily be applied to SQL statement >building. Indeed - I am a rabid advocate of the use of placeholders/bind variables in SQL, to separate SQL from variable values - since I use Oracle at work, it's got native support (and using them is mandatory unless you want to kill the database) so sprintf() is the Wrong Way To Do it there - but both when I use Oracle, and when I use MySQL for playing at home, I use ADOdb to give me a consistent interface which supports native placeholders on Oracle, and emulated ones on MySQL. If you're using the naked MySQL functions then sprintf()+mysql_escape_string() works very nicely, provided you always remember to use it... >>>This code could be very quickly maintained in French, German, Spanish as >> >> Although sprintf for localisation has the main problem that word order within >> sentences isn't always the same; sprintf doesn't do ordered parameters. > >It may have been a bad example to give, I was trying to place emphasis on >code/data seperation and efficiency of code maintenance. OK :-) -- Andy Hassall / <andy@andyh.co.uk> / <http://www.andyh.co.uk> <http://www.andyhsoftware.co.uk/space> Space: disk usage analysis tool |
|
|||
|
"Andy Hassall" <andy@andyh.co.uk> wrote in message
news:rod061dh6enchedtehntud77ls7d83g0ep@4ax.com... > On Fri, 15 Apr 2005 21:40:44 +0100, CJ Llewellyn <cj.llewellyn@gmail.com> > wrote: > > >echo sprintf( > > Yay, it's not just me that likes sprintf then :-) > > >This code could be very quickly maintained in French, German, Spanish as > > Although sprintf for localisation has the main problem that word order within > sentences isn't always the same; sprintf doesn't do ordered parameters. I thought you can reference the variables supplied to sprintf() by number in the format string. Not something I have tried tough. Of course it doesn't help much when you have languages with declensions. |