简体   繁体   English

PHP中的单引号和双引号?

[英]single quotes and double quotes in php?

Double quotes--->"$a" interpretes variables. 双引号--->“ $ a”解释变量。

single quotes--->'$a' does not.Alright till now 单引号->'$ a'到现在为止

MyQuestion: 我的问题:

  • what if I use "'$a'" ? 如果我使用"'$a'"怎么办?

Note :want to know behind the scene details. 注意 :想了解幕后细节。

Detailed Explanation : 详细说明

I faced a major problem because of this when I used it in a foreach loop: 因此,当我在foreach循环中使用它时,我面临一个主要问题:

The following example gave me incorrect option value. 以下示例为我提供了错误的选项值。 For example, value it renders is UX if original is UX developer 例如,如果原始用户是UX开发人员,则它呈现的值为UX

echo "<option value=".$item['m_designation'].">".$item['m_designation']."</option>";

this example gave me correct option value. 这个例子给了我正确的期权价值。 For example,value it renders is UX developer if original is UX developer 例如,如果原始版本是UX开发人员,则它呈现的值是UX开发人员

 echo '<option value="'.$item['m_designation'].'"> '.$item['m_designation'].' </option>';

Hope I am clear. 希望我清楚。

Update: 更新:

I got the desired result but I don't know the logic behind it. 我得到了理想的结果,但我不知道其背后的逻辑。 I have tried it and done it successfully, but I wanted to know what's happening behind the scene. 我已经尝试过并成功完成了,但是我想知道幕后发生的事情。 I think people have misread my question. 我认为人们误解了我的问题。

The use of ' and " for strings only changes when they are the outer container of the entire string - a different quote inside that is just treated as a plain character with no special meaning, therefore "'$var'" uses the rules of " (parsing variables), whereas '"$var"' would literally output "$var" as it uses the rules of ' (no parsing of variables). 使用'"字符串,只有当他们是整个字符串的外容器的变化-内部的不同的报价只是视为没有特殊含义的普通字符,因此"'$var'"使用规则" (解析变量),而'"$var"'实际上会使用' (不解析变量)规则输出"$var"

Summary: When you do "''" or "\\"\\"" the quotes inside the string are not parsed by PHP and treated as literal characters, the contents of such quotes will have the same behaviour in or out of those quotes. 简介:当您执行"''""\\"\\"" ,PHP不会解析字符串中的引号并将其视为文字字符,此类引号的内容在这些引号之内或之外均具有相同的行为。

You'll have a string that uses double quotes as delimiters and has single quotes as data. 您将拥有一个使用双引号作为定界符并将单引号作为数据的字符串。 The delimiters are the ones that matter for determining how a string will be handled. 分隔符是决定如何处理字符串的重要分隔符。

It seems your question is more directed toward the output PHP produces for HTML formatting. 看来您的问题更针对PHP为HTML格式生成的输出。 Simply, single quotes in PHP represent the literal value: 简单来说,PHP中的单引号代表字面值:

$a = 1;
$b = '$a';
echo $b;
//$a

$a = 1;
$b = "$a";
echo $b;
//1

$a = 1;
$b = "'$a'";
echo $b;
//'1'

If you want to output HTML, you can use heredoc syntax . 如果要输出HTML,可以使用heredoc语法 This is useful when outputting more than one line containing variables: 当输出多行包含变量的行时,这很有用:

$name = "Bob";
$age = 59;
echo <<<EOT
My name is "$name".
I am $age years old.
EOT;
//My name is "Bob"
//I am 59 years old.
$a = 'test';

echo '$a';// prints $a 
echo "$a";// prints test
echo "'$a'"//prints 'test'

double quotes checks the php variable and echo the string with php variable value 双引号检查php变量并用php变量值回显字符串

example echo "wow $a 123"; //prints wow test 123 例如echo "wow $a 123"; //prints wow test 123 echo "wow $a 123"; //prints wow test 123

single quotes print whatever in the single quotes 单引号打印单引号中的任何内容

example echo 'foo $a 123';//prints foo $a 123 示例echo 'foo $a 123';//prints foo $a 123

Your 'faulty' (first) string was missing the single quotes ' : 您的“错误”(第一个)字符串缺少单引号 '

echo "<option value='".$item['m_designation']."'>".$item['m_designation']."</option>";
                    ^                          ^

Your problem is that you confuse the quotes in your HTML with the quotes in the PHP. 您的问题是您将HTML中的引号与PHP中的引号相混淆。

$a = 1;
$b = '"$a"';
echo $b;
# => "$a"

$a = 1;
$b = "\"$a\"";
echo $b;
# => "1"

I'd advise you to simply never use string literals, as (especially in PHP) there are a lot of unexpected and weird edge-cases to them. 我建议您不要使用字符串文字,因为(尤其是在PHP中)它们有很多意想不到的怪异的边缘情况。 Best is to force an interpreter (which also only works with double quotes: 最好是强制使用解释器(该解释器也只能使用双引号:

$a = 1;
$b = "\"{$a}\"";
$c = "{$a+2}";
echo $b;
# => "1"
echo $c;
# => 3

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM