PHP:不同的引号? [英] PHP: different quotes?
问题描述
下面的引号和"有什么区别?关于`?在下面使用不同的引号将有错误吗?
What is the difference between the quotes " and ' ? What about `? Is there an error in using different quotes ' and " below?
$result = pg_query_params($dbconn,
'INSERT INTO users
(username, email, passhash_md5)
VALUES ($1, $2, $3)',
array($username, $email, $passhash_md5
)
$result = pg_query_params( $dbconn,
"SELECT user_id
FROM users
WHERE email = $1",
array( $email )
)
推荐答案
使用单引号(')时,变量替换未完成,这意味着第一个示例中的值从字面上是$ 1 $ 2等,如果它是常规字符串,并且不会传递给替换它们的函数.
Variable-substitution isn't done when using single quotes ('), meaning that the values in your first example would literally be $1 $2 etc if it was a regular string and not passed on to a function that replaces them.
如果您不需要变量替换,出于性能原因,最好使用单引号.
If you don't need variable-substitiution, it's better to stick with single quotes for performance reasons.
``调用shell引擎并将其作为实际命令调用,并返回结果,就像在perl中一样.因此,它的含义完全不同.
`` invokes the shell-engine and invokes it as an actual command, and returning the result, just like in perl. Hence, it has a completely different meaning.
示例:
$email = 'user@example.org';
$sql1 = "SELECT user_id FROM users WHERE email = $email";
$sql2 = 'SELECT user_id FROM users WHERE email = $email';
$ sql1是来自用户的SELECT user_id,其中电子邮件= user@example.org
$sql1 would be SELECT user_id FROM users WHERE email = user@example.org
$ sql2将是来自其中电子邮件= $ email的用户的SELECT user_id
$sql2 would be SELECT user_id FROM users WHERE email = $email
这篇关于PHP:不同的引号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!