If you want to write clean, efficient, optimised code, you need to pay attention to how you declare
and work with strings.
QUOTES:
If the string doesn't contain any variables, then just use single quotes:
$greeting = 'Hello world';
If the string includes variables, use double quotes:
$name = 'justin';
$greeting = "Hello $name";
Why? If PHP sees single quotes, it doesn't parse the string for variables, so the execution is a
fraction faster.
VARIABLES INSIDE STRINGS
Most people just write the variable directly into the string:
$greeting = "Hello $name";
Or join the strings and vars with periods:
$greeting = "Hello " . $name;
I prefer to wrap the variable in "curly brackets", which helps PHP readily tell where the variable
starts/ends, which means faster, non-greedy substitution:
$greeting = "Hello {$name}";
In addition to finding it clearer for myself to read, it also enables more complex substitutions
with arrays:
$greeting = "Welcome back {$_COOKIE['name']}";
Or when two variables sit side-by-side:
$dir = "something/foo/bah/";
$file = "xxx.txt";
$path = "{$dir}{$file}";
matthew waygood wrote :948
Probably the first thing you learn to do when starting PHP. So why tell everyone, because in order to be at this site you would have already done some coding and already know this information. Just like telling someone learning html about the BODY tag.
Justin French wrote :949
I don`t agree -- the main point of my example was enclosing variables inside {braces}, which can allow more complex combinations of strings and variables, something that I very rarely see being used in any PHP related tutorial or article.
The second point of the example was to point out that "double quotes" should only be used when needed (ie, when including variables inside strings). Based on the plethora of double quotes I see in tutorials, sample code, articles and even open source projects, it would appear that not everyone knows when to use them, or don`t care.
I may have started off a little too "basic", but there`d be a lot more clean code out there if people knew when to use what, and how much easier {$vars} inside curly braces can make things.
The fact that you already knew doesn`t mean it`s a bad example -- it just means you`re smarter than the average bear.
Sarah King wrote :950
Thanks Justin.
It never hurts to be reminded of how to write efficient code. Personally I hadn`t known that about the {} and I`ve been coding php for a couple of years now (part time). I`ve seen it in some examples but not found documentation (easily) to explain why.
The other performance option with Strings is whether it`s also better to have
if ( isset($name))
{
?>
Hello <? echo $name; ?>, Welcome back...
<?
}
Personally I find it a pain in the butt to work with and my scripts aren`t so big that I need the performance to be quite so efficient. But these are all things that are useful to some.
mick 28 wrote :951
Thanks for the heads up :-)
I`ve been using PHP for some time and knew about the quotes but only recently discovered the curly braces after looking at an example from another site that used them, your right they are under used and rarely mentioned yet they will save me some time in future especially when using complex variables.
Thanks once again for sharing.
kenji wrote :953
Yep, I`ve got to agree. You`ve pointed out something I never knew and will start using. BTW, this isn`t that basic so has a place here.
PMJ wrote :954
Thanks for showing us (newbies) the small stuff. I want to do things in the proper manner. Back off Matthew.
Robin North wrote :955
Excellent piece of info for optimisation - thanks