简体   繁体   中英

single quotes and double quotes in php?

Double quotes--->"$a" interpretes variables.

single quotes--->'$a' does not.Alright till now

MyQuestion:

  • what if I use "'$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:

The following example gave me incorrect option value. For example, value it renders is UX if original is UX developer

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

 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).

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.

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. Simply, single quotes in PHP represent the literal value:

$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 . 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

example 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

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.

$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. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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