The single and double quotes:
Let's start with a classic PHP namely single and double quotes (quotation marks). They derive their main difference is that everything is within single quotes is not interpreted, contrary to what is inside double quotes. In practice the following code does exactly the same thing:
PHP Code:
<? Php
echo 'Here is a test string';
?>
<? Php
echo "This is a test string";
?>
Cons by the following code will give two different results:
Version 1:
PHP Code:
<? Php
$ Myvar = 'hello world';
echo "Variable Value: $ myvar ';
?>
Version 2:
PHP Code:
<? Php
$ Myvar = 'hello world';
echo "Variable Value: $ myvar";
?>
When you use the second version of this example, you see that the string $ myvar is replaced by its value, unlike what happens in the first version of this example. Now, replace its value by $ myvar requires additional time for PHP because it must not only find the exact name of the variable but then substitute its value. That is why it is faster to use version 1. Although you may say, but what if you want to display the contents of a variable? well, we will be able to use concatenation. Resume our slow code and optimize it:
Version 1:
PHP Code:
<? Php
$ Myvar = 'hello world';
echo "Variable Value: $ myvar";
?>
Version 2:
PHP Code:
<? Php
$ Myvar = 'hello world';
echo 'Variable Value'. $ myvar;
?>
Version 3:
PHP Code:
<? Php
$ Myvar = 'hello world';
print 'Variable Value'. $ myvar;
?>
We can also (as of version 3) use the print function. It has an echo difference, however: it always returns 1, so that you can use it in conditions or otherwise. In practice, it is supposed to be slower.
Bookmarks