What is the difference between single
I'm not an expert in PHP programming, but I'm a little confused why I see some code in PHP with string placed in single quotes and sometimes in double quotes.
I just know in .NET, or the C language, if it is in single quote, that means it is a character, not a string.
PHP strings can be specified not just in two ways, but in four ways.
'
, and to display a back slash, you can escape it with another backslash
( So yes, even single quoted strings are parsed ). $type
and you what to echo "The $types are"
That will look for the variable $types
. To get around this use echo "The {$type}s are"
You can put the left brace before or after the dollar sign. Take a look at string parsing to see how to use array variables and such. <<<
. After this operator, an identifier is provided, then a newline. The string itself follows, and then the same identifier again to close the quotation. You don't need to escape quotes in this syntax. <<<
sequence used for heredocs, but the identifier which follows is enclosed in single quotes, eg <<<'EOT'
. No parsing is done in nowdoc. Speed:
I would not put too much weight on single quotes being faster than double quotes. They probably are faster in certain situations. Here's an article explaining one manner in which single and double quotes are essentially equally fast since PHP 4.3 ( Useless Optimizations
toward the bottom, section C
). Also, this benchmarks page has a single vs double quote comparison. Most of the comparisons are the same. There is one comparison where double quotes are slower than single quotes.
事情得到双引号评估,但不是单一的:
$s = "dollars";
echo 'This costs a lot of $s.'; // This costs a lot of $s.
echo "This costs a lot of $s."; // This costs a lot of dollars.
Single quoted
The simplest way to specify a string is to enclose it in single quotes. Single quote is generally faster, and everything quoted inside treated as plain string.
Example:
echo 'Start with a simple string';
echo 'String's apostrophe';
echo 'String with a php variable'.$name;
Double quoted
Use double quotes in PHP to avoid having to use the period to separate code (Note: Use curly braces {}
to include variables if you do not want to use concatenation ( .
) operator) in string.
Example:
echo "Start with a simple string";
echo "String's apostrophe";
echo "String with a php variable {$name}";
Is there a performance benefit single quote vs double quote 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 is any variables in there.
链接地址: http://www.djcxy.com/p/9846.html上一篇: 按数值删除PHP数组(不是键)
下一篇: 单身之间有什么区别