So maybe you’ve seen people use single quotes, maybe double quotes? Personal preference? Wrong. Processing time? True. Also, you’ve seen them use this crazy “.” thing in between variables and strings, or maybe even entire functions?! Yes, that’s called concatenation. (con-cat-in-ation) yes, it’s a mouthful but it’s extremely useful.
“Quotations”
In PHP there are two types of quotations (actually 3 but I’ve never met anyone that uses the third, so, I’ll just tell you it’s the “ and unless you have some stupid infatuation with them, just use normal single or double quotes.)
The two types we are going to talk about in this article are
- Single quotations
- Double quotations
NOTE Sometimes they are referred to as, “unparsed quotations” and “parsed quotations” you’ll know why soon.
Single quotations (unparsed quotations) – ‘ ‘
In PHP single quotations can encapsule (hold) any type of data, numbers, decimal, strings, variables, it doesn’t matter. The difference here is when we feed it a variable to hold alongside a string or something, for example:
<?php
$name = ‘crainbandy’;
echo ‘what’s up, my name is $name’;
?>
Instead of actually saying “what’s up, my name is crainbandy” this will say, “what’s up, my name is $name” because single quotes turn a built-in PHP function called “variable parsing” off. Meaning, instead of looking for $variables to replace with their corresponding values, they just use the raw text which is $name in the case of the script above. Make sense? In other words… “Single quotes don’t replace variable names with their corresponding value.”
Double quotations (parsed quotations) – ” ”
This is the opposite of single quotations in the fact that, they replace variables with their corresponding value.
Example
<?php
$name = ‘crainbandy’;
echo ‘what’s up, my name is $name’;
?>
This will actually say, “what’s up, my name is crainbandy” instead of “what’s up, my name is $name“, the opposite result of single quotes.
The main thing I want you to get out of this is the fact that PHP has to turn on a lot of referencing and underlying matching if you use double quotations, whether or not there is a variable involved. You think PHP just somehow knows that a variable has a VARIABLE for a value? No. PHP has to look.
For example,
<?php
$name = “crainbandy”;
echo $name;
?>
Why in the world would you want to parse a solid string with double quotes when you could have just as easily used $name = ‘crainbandy’; instead and gotten the same result? How much processing time and performance you lose by things like this I’m not sure, but you do lose some obviously.
So now you know the difference. If I look at your code and I see variable assignments to solid values with double quotes I’m going to e-slap you.
In a nutshell, use single quotes! Unless for some reason you need to “parse” the strings, fine, but I still think you can use the next big word “concatenation” to get around that.
Concatenation (Or a period .)
This is such a cool feature, I don’t know where I would be in my scripts without it. As you can see in my Constantly use Constants in PHP article, when I’m defining constants I use a combination of single quotes with concatenation. It’s cool, check it out.
For example, usually to echo a variable value to anything you would have to use double quotes.
Example:
<?php
$name = ‘crainbandy’;
echo “what’s up my name is $name”;
?>
This would produce the desired effect right? Of course it would, but, there’s something more we can do with this simple script.
Concatenation! Concatenation simply means “Attaching a value to a variable” by means of… “concatenating” it.
Example
<?php
$name = ‘crainbandy’;
echo ‘what’s up, my name is ‘ . $name;
?>
You like that? You FREAKING like that? That’s what I thought. Yes, you’re observant. “What in the world is that backslash doing in there?” Well my friend. That is called “escaping strings” and this article is not about that. Sorry. Back to concatenation.
So there you have it, PHP actually knows that when you have a concatenated string like the one above, just continue going on replacing values/running methods and or functions until you reach that beautiful semi-colon. Why would you use this instead of the “time saver” that double quotes is? Well for several reasons
- You cannot use arrays values inside single quotes or double quotes (you can with double quotes, but you run into a problem)
- You can’t run functions or methods inside a double quoted or single quoted string. With concatenation, you can do that.
Running a function inside an echo statement with PHP
<?php
$name = ‘crainbandy’;
echo ‘what’s up, my name is ‘ . strtolower($name);
?>
You see how that works? Cool. Glad. Psyched. Riled. Sweet
Usually in your PHP scripts you will be using a mixture of single quoted, double quoted, and concatenation. Sometimes there’s just no reason to use concatenation and make yourself go through the extra typing, but, it usually is more resourceful.
One other way to concatenate strings is with the .= operator.
$name = ‘wax’;
$name .= ‘jelly’;
This creates a variable called $name that has the value “crainbandy”, pretty cool huh? Okay. Til next time…