So you can use single or double quotes for Strings. But what’s the difference? Let’s take a look.
The main difference between single and double quotes is any string in double-quotes will be processed by PHP before it’s outputted. That means, for example, any variables in strings with double quotes will become their literal value. Let’s take a look at an example here. Where on the left side of the screen, I have my code and on the right side of the screen, I have the It in the browser. I’ll type my opening PHP tag. And then I will create a variable called name. So we’ll say, the name gets Joe and I’ll put Joe in single quotes. Then we’ll say Echo? Double quote. Hello, comma space name. Remember PHP Prince exactly as we type within the quotes so we need to add any punctuation spaces and things. Like that. Then we’ll add our semicolon will save this and will refresh the page. You can see it says, hello Joe. Now, if we change this string to single quotes,
first of all, you’ll notice that my editor
has changed the highlighting to make this the maroon instead of the same blue as the variable.
When I save this and refresh the single quote prints out the literal string that we’ve written here. Because single quotes don’t go through the PHP processor first, it makes them a little bit more efficient than double quotes, but not so efficient that we should never use double quotes still. If you do want to print a variable with single quotes, will need to use something called string concatenation. So I’ve deleted the variable reference here and I’m going to write, hello comma. And then a single I’ll quote. And then I’m going to add a period that period is the concatenation character. So then I will write the name in no quotes another period and then
I’ll open the single quote, again, add my exclamation point. Closed the single quote, and add the semicolon. Now, I’ll save this and I’ll refresh notice that there’s no space after the comma.
That’s because Even though we have spaces within the concatenation,
Comments
Post a Comment
If you have any doubts, Please let me know