Strings in PHP can be enclosed between the double quotation marks (“) or single quotation marks (‘). Where is the different in it.
There is a difference between double quotation marks and single quotation marks when used with strings.
Double quotation marks allow the parsing of variables. If you include a variable within double quotation marks the PHP engine substitutes the variable’s value, like so:
$name = “Jim”;
print “hello, $name”; // hello, Jim
If you use single quotation marks to enclose the same string, the variable is not substituted:
print ‘hello, $name’; // hello, $name
Double-quoted strings are also parsed for escape characters. Escape characters take on or lose special meaning when preceded by a backslash () character. Notable among these are n for a newline character, t for a tab, ” to print a double-quoted character within a double-quoted string, \ to print a backslash, and $ to print a dollar sign (so that it is not mistaken for the start of a variable).
As a rule of thumb, if you want a string to be output exactly as you typed it, you can use single quotation marks. This can help your code to run more quickly because the interpreter does not have to parse the string. If you want to take advantage of escape characters such as n and use variable substitution, you should use double quotation marks.
Leave a Reply
You must be logged in to post a comment.