Most people are still not building PHP web applications for performance but rather building web applications to get it working, but you must consider all areas in programming, especially performance because you want speedy applications too.
If you don't know the difference between:
echo 'Hello, World!';
and
echo "Hello, World!"; // as pointed out by vujsa
then you really need to pay attention to this, it's vital you understand interpolation.
What is Interpolation:
Interpolation is a way of inserting/modifying additional text. In programming terms it's easily explained like these BBCodes we have on this forum, anything between the BBCode tags are interpolated, meaning they are modified to be displayed however the BBCode tags have been defined to do. Other interpolated objects are HTML tags like strong, em, h1-h6, etc, anything like formatting tags are ways of interpolating text.
This meaning is the same when used with double and single quoted strings, objects, etc in PHP. However, HTML tags used within any quotes will be considered HTML by the browser which will perform it's own interpolation on the objects.
The difference between the single quoted text of Hello, World is that it's displayed as is, the PHP interpreter does not need to convert anything in this text, so it just displays the string by outputting each character it comes across.
The double quoted text of Hello, World makes the interpretter work harder, because while it reads each character it comes across, it is also looking for anything that can be interpolated. Since there's nothing to be interpolated, why do some developers use double quotes?
What I am wanting you to learn out of this, is when to use single quotes and double quotes.
So single quotes does no interpolation, double quotes does.
Here's an example that should hopefully show you what I mean.
<?php
$string1 = 'This string will not be interpolated\n<br />';
$string2 = 'Part of this string will be interpolated ' . "\n" . '<br />';
$string3 = "$string2";
$string4 = '$string1<br />';
$string5 = "This will be interpolated\n<br />";
echo $string1 . $string2 . $string3 . $string4 . $string5;
?>
You should do this example and display it, so you can see for yourself what's happening, also view the source code, so you can see whether newlines were created or not.
Now what happens in $string1 is very important. You're using the escaped character \n which means newline, however, newlines need to be interpolated and since single quotes do not provide interpolation, you will get the exact string as it is except for the <br /> since the web browser handles HTML tags it will not show on the page, but it will show in the source code.
So the output of string one will look like this in your browser:
This string will not be interpolated\n
$string2 uses a mixed method, single and double quoted strings that get concatenated (joined) together. This is quite acceptable, that way only interpolation will work inside the double quoted text, which is the newline character.
$string3 stores the exact same as $string2, we didn't need to use double quotes for this we could have just done $string3 = $string2 which is the preferred method but since this is about interpolation, I needed to use double quotes to show you. The double quotes interpolated $string2 the variable and came up with what $string2 stored.
$string4 is not interpolated it will just display:
$string1
I also had to add the <br /> tag on it, because it would not have had this tag.
$string5 uses double quotes for the whole string and requires interpolation only on a small part in this string, but still uses interpolation for the newline.
Now you can choose what ways you want to do, but non interpolation should use single quotes and interpolation should use double quotes. I prefer $string2's method with concatenating the strings but sometimes this can get quite messy that using double quotes should be ok, but only if it has a lot of things to be interpolated.
e.g.
echo 'Hello, ' . "\n\t" . 'World!' . "\t" . 'This can get quite messy' . "\n\t" . 'especially if you need to interpolate quite a few things at once.' . "\n";
echo "Hello, \n\tWolrd!\tThis is still kinda messy\n\tbut it's also a lot easier\n";
We're using the newline and tab escaped characters in these strings, and after a while, it just becomes messy. Concatenating strings to this extent can also be a performance issue, so we must choose wisely.
The main thing I wanted to point out is if it doesn't do interpolation, don't use double quotes for it, if it does, then it's ok. If you know when a string needs interpolation then this is better, e.g. you don't need interpolation in $_SERVER['host']; but you do if it's $_SERVER["$some_var"];
So hopefully from this you will understand what interpolation is, and when to use it and when not to use it. I may write a benchmarking PHP application to show you how fast it was to process an interpolated string and a non interpolated string, this speed test is however an estimation, but to my understanding, any non interpolated string should be processed faster than an interpolated string.
Cheers,
MC


