Very basic, but would like to know the difference/security ramifications etc of using " vs. '.
非常基本,但想知道使用“vs.”的差异/安全后果等。
Can someone provide an example that explains when to use each one?
有人可以提供一个解释何时使用每个人的例子吗?
3 个解决方案
#1
There are a lot of subtle differences, you'll want to read the php documentation to get a lot of the details, but the important detail are:
有很多细微的差别,你需要阅读php文档来获取很多细节,但重要的细节是:
Double quotes are parsed whereas single quotes are literals.
解析双引号,而单引号是文字。
You can use variables inline with double quotes, but not with single quotes.
您可以使用带双引号的内联变量,但不能使用单引号。
There are some catches though:
虽然有一些捕获:
<?php
$beer = 'Heineken';
echo "$beer's taste is great"; // works; "'" is an invalid character for variable names
echo "He drank some $beers"; // won't work; 's' is a valid character for variable names but the variable is "$beer"
echo "He drank some ${beer}s"; // works
echo "He drank some {$beer}s"; // works
?>
Single quotes are slightly faster.
单引号稍快一些。
#2
When a string is enclosed in double quotes, then escape sequences such as \n
and variable identifiers such as $var
are interpreted.
当字符串用双引号括起来时,将解释转义序列(如\ n)和变量标识符(如$ var)。
See the PHP strings manual for specific details and examples.
有关具体细节和示例,请参阅PHP字符串手册。
#3
The biggest one is this. Inside double-quotes, you can include variables, but inside single quotes, the variable name will be literal:
最大的就是这个。在双引号内,您可以包含变量,但在单引号内,变量名称将是文字:
$var1 = "hello";
// this will echo "hello world"
echo "$var1 world";
// this will echo $var1 world
echo '$var1 world';
Using double-quotes becomes extremely useful in a number of situations, expecially when you place {} around the variable names. Here are some examples (certainly others can give you more examples):
在许多情况下使用双引号变得非常有用,特别是当您在变量名称周围放置{}时。以下是一些示例(当然其他人可以提供更多示例):
// array elements
echo "Element 5 is {$myArray[5]}";
echo "Element 2 subelement 3 is {$myArray[2][3]}";
//
// a dynamic key
$value = "thing";
$someValue = $myArray["some{$value}"]; // returnd $myArray[something]
#1
There are a lot of subtle differences, you'll want to read the php documentation to get a lot of the details, but the important detail are:
有很多细微的差别,你需要阅读php文档来获取很多细节,但重要的细节是:
Double quotes are parsed whereas single quotes are literals.
解析双引号,而单引号是文字。
You can use variables inline with double quotes, but not with single quotes.
您可以使用带双引号的内联变量,但不能使用单引号。
There are some catches though:
虽然有一些捕获:
<?php
$beer = 'Heineken';
echo "$beer's taste is great"; // works; "'" is an invalid character for variable names
echo "He drank some $beers"; // won't work; 's' is a valid character for variable names but the variable is "$beer"
echo "He drank some ${beer}s"; // works
echo "He drank some {$beer}s"; // works
?>
Single quotes are slightly faster.
单引号稍快一些。
#2
When a string is enclosed in double quotes, then escape sequences such as \n
and variable identifiers such as $var
are interpreted.
当字符串用双引号括起来时,将解释转义序列(如\ n)和变量标识符(如$ var)。
See the PHP strings manual for specific details and examples.
有关具体细节和示例,请参阅PHP字符串手册。
#3
The biggest one is this. Inside double-quotes, you can include variables, but inside single quotes, the variable name will be literal:
最大的就是这个。在双引号内,您可以包含变量,但在单引号内,变量名称将是文字:
$var1 = "hello";
// this will echo "hello world"
echo "$var1 world";
// this will echo $var1 world
echo '$var1 world';
Using double-quotes becomes extremely useful in a number of situations, expecially when you place {} around the variable names. Here are some examples (certainly others can give you more examples):
在许多情况下使用双引号变得非常有用,特别是当您在变量名称周围放置{}时。以下是一些示例(当然其他人可以提供更多示例):
// array elements
echo "Element 5 is {$myArray[5]}";
echo "Element 2 subelement 3 is {$myArray[2][3]}";
//
// a dynamic key
$value = "thing";
$someValue = $myArray["some{$value}"]; // returnd $myArray[something]