The Difference Between Double and Single Quotes in PHP
PHP, Uncategorized, Web Design
In PHP, the difference between using double or single quotes is quite large unlike other languages like JavaScript. The main difference, that I believe is most important, is you can use variables inside of double quotes in wherein single quote statements you cannot. For example, if I have a variable set like so:
$age = 22;
And I wanted to use it in an echo statement I could do this with single quotes:
echo ‘I am ‘ . $age . ‘ years old’;
That would work fine, it would print out to the page “I am 22 years old” just like expected. There is nothing wrong with that except we have to use concatenation (adding strings together) by adding the periods. The useful part about double quotes in PHP is you don’t have to use concatenation, you just insert the variable inside the string like so:
echo “I am $age years old”;
This would print out to the page “I am 22 years old” and it was a lot easier to type out and read without using the concatenation periods. If you’d like to learn more, be sure to check out our PHP Training. Have an amazing day!