Difference between and in php

Possible Duplicate:
PHP: different quotes?

Simple question:

What is the difference between ' and " in php? When should I use either?

asked Sep 9, 2009 at 22:54

johnnietheblackjohnnietheblack

12.8k27 gold badges91 silver badges132 bronze badges

5

Basically, single-quoted strings are plain text with virtually no special case whereas double-quoted strings have variable interpolation (e.g. echo "Hello $username";) as well as escaped sequences such as "\n" (newline.)

You can learn more about strings in PHP's manual.

answered Sep 9, 2009 at 22:57

There are 3 syntax used to declare strings, in PHP <= 5.2 :

  • single quoted
  • double quoted
  • heredoc

With single quotes :

variables and escape sequences for special characters will not be expanded

For instance :

echo 'Variables do not $expand $either';

Will output :

Variables do not $expand $either


With double-quotes :

The most important feature of double-quoted strings is the fact that variable names will be expanded.

For instance :

$a = 10;
echo "a is $a";

Will output :

a is 10


And, with heredoc :

Heredoc text behaves just like a double-quoted string, without the double quotes. This means that quotes in a heredoc do not need to be escaped,

For instance :

$a = 10;
$b = 'hello';

$str = <<

Will get you :

a is 10
and "b" is hello.

answered Sep 9, 2009 at 22:57

Pascal MARTINPascal MARTIN

388k77 gold badges647 silver badges656 bronze badges

Any variables inside a " quoted string will be parsed. Any variables in a ' quoted string will not be parsed, and will be shown literally as the variable name. For this reason, ' quoted strings are very slightly faster for PHP to process.

$test = 'hello';
echo "this is a $test"; // returns this is a hello
echo 'this is a $test'; // returns this is a $test

I'd say use ' quotes unless you want variables inside your strings.

answered Sep 9, 2009 at 22:57

The difference is, strings between double quotes (") are parsed for variable and escape sequence substitution. Strings in single quotes (') aren't.

So, using double quotes (") you can do:

$count = 3;
echo "The count is:\t$count";

which will produce

The count is:3

The same in single quotes returns the literal string.

Also, the characters that need to be escaped. If you have a string like:

'John said, "Hello"'

you would probably use single quotes, to avoid having to escape the quotes in the string and vice-versa.

answered Sep 9, 2009 at 22:57

Brenton AlkerBrenton Alker

8,8273 gold badges34 silver badges37 bronze badges

" interprets escape characters and variables. ' doesn't do either.

answered Sep 9, 2009 at 22:56

igustinigustin

1,0928 silver badges8 bronze badges

In one word: when you would like to all your special chars (like \n) and varables (like $number) be noticed and process.

answered Sep 9, 2009 at 22:57

IProblemFactoryIProblemFactory

9,3018 gold badges48 silver badges65 bronze badges

What is the difference between != and !== In PHP?

Operator != returns true, if its two operands have different values. Operator !== returns true, if its two operands have different values or they are of different types.

What is the difference between

Conclusion. The two operators, => and -> may look similar but are totally different in their usage. => is referred to as double arrow operator. It is an assignment operator used in associative arrays to assign values to the key-value pairs when creating arrays.

What is difference between and in PHP?

The difference is, strings between double quotes (") are parsed for variable and escape sequence substitution. Strings in single quotes (') aren't. The same in single quotes returns the literal string. you would probably use single quotes, to avoid having to escape the quotes in the string and vice-versa.

What is the difference between & and && in PHP?

Introduction to the PHP AND operator The && and and operators return the same result. The only difference between the && and and operators are their precedences. The and operator has higher precedence than the && operator.