1

Possible Duplicate:
Difference between single quote and double quote string in php

I am new to PHP and in programming i have seen use of both "" and ' '.

What is the difference between "" and ' '?

And in declaring a link i have used the following ,but it does not seem to work there must be something wrong in quotation:

$abc_output .='<a href="abc.com">' Back to Main Menu'</a>';

echo $abc_output;  

What might be the error here?

Community
  • 1
  • 1
Dumb_Shock
  • 1,050
  • 1
  • 13
  • 23

3 Answers3

4

You want to keep the text inside of your string:

$abc_output .='<a href="abc.com">Back to Main Menu</a>';

The difference between ' and " is that you can embed variables inside of a double quoted string.

For example:

$name = 'John';
$sentence = "$name just left"; // John just left

If you were to use single quotes, then you'd have to concatenate:

$name = 'John';
$sentence = $name.' just left'; // John just left

PS: Don't forget that you always have to escape your quotes. The following 2 are the same:

$double = "I'm going out"; // I'm going out
$single = 'I\'m going out'; // I'm going out

Same applies the other way around:

$single = 'I said "Get out!!"'; // I said "Get out!!"
$double = "I said \"Get out!!\""; // I said "Get out!!"
Joseph Silber
  • 214,931
  • 59
  • 362
  • 292
1

Double quotes allow additional expressions, such as "$variable \n", which single quotes don't. Compare:

$variable = 42;
echo "double: $variable,\n 43\n";
echo 'single: $variable,\n 43';

This outputs:

double: 42,
 43
single: $variable,\n 43

For more information, refer to the official documentation.

phihag
  • 278,196
  • 72
  • 453
  • 469
1

Text in double quotes are parsed.

For example:

$test = 'something';

print('This is $test');
print("This is $something");

would result in:

This is $test
This is something

If you don't need the string to be parsed you should use single quotes since it's better performance wise.

In your case you need to do:

$abc_output .='<a href="abc.com">Back to Main Menu</a>';
echo $abc_output;  

Or you will get an error.

The Back to Main Menu wasn't in the string.

PeeHaa
  • 71,436
  • 58
  • 190
  • 262
  • `use single quotes since it's better performance wise`. I always use single quotes, since I find them more convenient, but I never knew that it's better for performance. Do you have a source for that? And, does that also apply to Javascript? – Joseph Silber Aug 25 '11 at 22:17
  • @Joseph Silber: I'm sure if you use your Google skills :) you'll find out that I'm right. Basically PHP doesn't have to search the string for stuff to parse so it will be faster. I dunno bout JS and parsing strings. – PeeHaa Aug 25 '11 at 22:19
  • maybe my google skills just suck, but I *did* search, found this: http://stackoverflow.com/questions/482202/is-there-a-performance-benefit-single-quote-vs-double-quote-in-php , read the comments to the answer, and wasn't impressed. – Joseph Silber Aug 25 '11 at 22:23
  • @Joseph Silber: Not impressed by the 30 times upvoted answer? Uff... tough crowd. :) – PeeHaa Aug 25 '11 at 22:53
  • http://www.phpbench.com/ . Scroll down to `Quote types`. – Joseph Silber Aug 25 '11 at 22:56