Concat single quote in php
The difference between single and double quotes in PHP is that double quotes are "intelligent" in that they will parse for variables when being read, while single quotes are "dumb" and will not try to parse any character in the string. Show
These result in some minor differences in what characters you can use; basically, the only character you need to escape when using single quotes is a single quote itself:
While if you use double quotes you have to escape other characters:
But it also allows for some nifty things like adding a new-line to the end:
With single quotes you would have to do a concatenation:
Generally, single quotes are faster because they are "dumb". However, normally one should not really worry about these issues, that is called Premature optimization, and should be avoided. A couple of words about optimization: generally one should first write the program the way it should work, and then find the biggest bottlenecks and fix those particular ones. If string speed really is an issue for you in PHP, you might want to consider switching to another language. Regarding speed: you probably want to focus more on memory usage than on CPU time. In these cases the CPU time could be considered pretty constant. CPU time is more relevant when writing algorithms that will iterate many times. Regarding concatenations: the more you concatenate strings using the dot-operator, the more memory you will be using. Consider this: This is an article I wrote a while ago on my old blog when I first started learning PHP. I'll repost it here, in case it helps anyone :) If you want to save text as a variable or display it on the screen (with say, You can use either single quotes ( Single QuotesSingle quotes are the simplest way to make a string. They just display what they are given, no bells and whistles, no special "powers" like being able to show variable values (see below in the Double Quotes section).
Enter fullscreen mode Exit fullscreen mode Line breaks with single quotesIf you need to display text on multiple lines, you can use line breaks within the quotes to achieve this. For example:
Enter fullscreen mode Exit fullscreen mode This will be output as:
Enter fullscreen mode Exit fullscreen mode If you try to use multiple
Enter fullscreen mode Exit fullscreen mode This will be output as:
Enter fullscreen mode Exit fullscreen mode Special characters and escape characters with single quotesYou only have two options when it comes to single quotes: One: You would need to escape the
Enter fullscreen mode Exit fullscreen mode This would properly output the following without causing an error.
Two: You could use
Enter fullscreen mode Exit fullscreen mode To print
*It might depend on the compiler, but when I was testing this out, Double QuotesOne big difference about double quotes vs single quotes is that you can use double quotes to include variables directly inside the string. If you use single quotes, you would have to concatenate the pieces together. Let's see an example. Let's say you have recipes and you save the titles into a variable called
Enter fullscreen mode Exit fullscreen mode If you want to create the HTML for recipe titles so that they look like this (and you aren't embedding PHP directly into HTML files, in which you might use
Enter fullscreen mode Exit fullscreen mode Using single quotes you need to add the different parts together:
Enter fullscreen mode Exit fullscreen mode With double quotes, however, you can put the variable directly inside the quotes:
Enter fullscreen mode Exit fullscreen mode Both methods work just fine, but using double quotes can save you some hassle. Pro Tip:Use curly braces to explicitly specify the end of a variable name when parsing it into a double-quoted string. Trying to
print 2 cups on line 2 below will give you an error because the code thinks the variable name is $unit_cups instead of
Enter fullscreen mode Exit fullscreen mode To avoid errors like this, you can surround the variable name in curly brackets likes so:
Enter fullscreen mode Exit fullscreen mode You can also do some more complex operations right in the double quotes, but that's beyond the scope of this article. To learn more about parsing complex operations within double quotes, check out the examples in the PHP manual. -- By the way, like single quotes, you can add line breaks to your output by including line breaks within the string. For example,
Enter fullscreen mode Exit fullscreen mode displays:
Enter fullscreen mode Exit fullscreen mode Special characters and escape characters with double quotesDouble quotes give you many more special characters to work with than single quotes, including the line break character.
What Should I Use?In general, you use either or, but you should be consistent with what type you use and when. For example, you might choose to use single quotes by default unless you need to use variables or special characters within the string. You might think that because double quotes give you more features, that it would be better to use them all the time, but single quotes are probably better for simple strings because you don't need to escape special characters like dollar signs. Can I use single quotes in PHP?Yes. It is slightly faster to use single quotes. PHP won't use additional processing to interpret what is inside the single quote. when you use double quotes PHP has to parse to check if there are any variables within the string.
How do you add single quotes to a string?The following are our strings with single and double quote. String str1 = "This is Jack's mobile"; String str2 = "\"This is it\"!"; Above, for single quote, we have to mention it normally like. However, for double quotes, use the following and add a slash in the beginning as well as at the end.
How can I add double quotes to a variable in PHP?A double-quoted string will output \' with either a single or double backslash used with the apostrophe.. \" for a double quote.. \\ for a backslash.. \$ to render a dollar sign instead of expanding the variable.. \n for a new line.. \t for a tab.. What is the difference between single quotes and double quotes when assigning it to a string variable?Generally, double quotes are used for string representation and single quotes are used for regular expressions, dict keys or SQL.
|