Jump to content
xisto Community
veerumits

Difference Between Single Quote(') And Double Quote(") In Php More confussion Could you Help.

Recommended Posts

I have confusion in Single Quote and Double Quote in PHP Programming so any body help in this regard in details with explanation. I only know that both are use to represent string
for example

<?php$x=20;echo "$x"; //output 20echo '"$x"';//output "$x"echo '$x';//output $xecho "'$x'";//output '20'echo ""$x"";//output Syntax error or parser errorecho "\"$x\"";//output "20" use of escape sequence ?>
when we execute i got output as you see above

but my confusion is not clear so please explain above each and every line of the code.

thanks
Notice from truefusion:
Use code bbcode for code.

Share this post


Link to post
Share on other sites

1) This line starts the PHP scripting block.

2) We define the variable $x to be 20

3) Since we use double quotes, $x is interpreted and so the string is the value of $x, which is 20.

4) This is the string "$x". The encapsulating single quotes means that variables are not parsed.

5) Same as above. Single quotes does not parse variables like double quotes.

6) Since it's double quoted, the variable $x is parsed.

7) Basically this is a null string, 20, then another null string, with no operators in between.

8) \" allows you to use the double-quotes inside the double-quoted string, and $x is parsed.

9) End the PHP scripting block.

 

Basically, when you start a string with either of the single or double quote marks, the string goes until it finds a matching quote. Having single-quotes inside a double-quoted string or double-quotes inside a single-quoted string does not end the string or start a new string, they are simply interpreted as characters. If double-quotes are used, variables are interpreted, while single-quoted strings do not interpret variables. The backslash (\) is the escape character, so \" is an escaped double quote and is not interpreted to end the string. If you need to have " in a string, you either have it double-quoted and use \", or use single quotes and you do not need to escape it.

Share this post


Link to post
Share on other sites

Huh....you're fast Nabb..I'm writing a solution...and you've already posted the solution. So...I discarded my solution.

Single quotes and double quotes are confusing. So....As a thumb of rule...stick to double qoutes. Use single quotes only if necessary(sometimes you need to use quotes inside quotes.....single quotes are helpful in those situations).

What Nabb told is correct. Single quotes don't parse variables. Thats why you're seeing undesirable results.

 

And another good programming practice is use {} to treat them as variables inside strings....for example in your code

echo "$x";
You'll face problem if you want to use string inside it...for examle if you add anystring after $xtest..the whole string will be treated as varialbe.....so use the following instead..

echo "testing {$x}value";
In the above....the variable is seperated from the string.

 

And if you use a code editor like Komodo edit...the code will be highlighted and you can see how php engine will treat the code.....like just as a string or varialbe etc.....

Edited by xpress (see edit history)

Share this post


Link to post
Share on other sites

Single quotes are used for text literal values. Double quotes are used when the stuff inside the double quotes should be parsed by the php engine.Say you want to output the text string "$x", then use single quotes.If you want to output the value of the variable defined by $x, then enclose it with double quotes.

Share this post


Link to post
Share on other sites

But some people said that single quote is parsed faster than double quotes.Is that true?


Yes. A single quoted string is not parsed by PHP, so requires virtually no memory or CPU power. A double quoted string gets parsed by PHP for variables etc. That requires memory, CPU power and time. If you have 100 double quoted strings on a page, being accessed by 1000 people, that is an awful lot of time, CPU power and memory getting used when it doesn't have to be used.

Share this post


Link to post
Share on other sites

anything in double quotes is parsed id "$20" so it will try to evaluate "'$20'" again since the outer most is "" it will again try to evaluatein case of '' it does not evaluate '$20' will be treated as a stringavoid using "" anytime possible since it invokes the quotation parser and slows down the page

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.