Single Quoted Strings:

  • Single quoted strings are the easiest way to specify string. Single quote is used to specify string when we want the string to be exactly as it is written.
  • To specify a literal single quote or backslash, escape it with a backslash (\).
  • All other escape sequences like \r or \n, will be output literally as specified rather than having any special meaning.
Php Code
echo 'This is \'test\' string';
//Output: This is 'test' string

Example:

Php Code
// Outputs: It is a simple string.
echo 'It is a simple string.';

// Outputs: It'll be a simple string.
echo 'It\'ll be a simple string.';

$string = 'string';
echo 'This is a simple $string.';
//Output: This is a simple $string.

// Outputs: This is a simple \n string.
echo 'This is a simple \n string.';
  • When string is specified in single quotes , then PHP will not evaluate it or interpret escape characters except single quote with backslash (‘) and backslash(\) which has to be escaped.
  • If you want a variable to be used with a single quoted string, the you need to use the period to separate code in string because PHP will not evaluate the variable if used directly within the single quoted string.
[ad type=”banner”]

Double Quoted Strings:

If we use double quoted string, then following things can be done:

  • Double quotes force PHP to evaluate the string.
  • Escape sequences are interpreted.
  • Any variable will be replaced by their value.

Example:

Php Code
// Outputs: It is a simple string.
echo "It is a simple string.";

// Outputs: It'll be a simple string.
echo "It'll be a simple string.";

$string = 'string';
echo "This is a simple string.";
//Output: This is a simple $string.

// Outputs: This is a simple string.
echo "This is a simple \n string.";
Php Code
$count = 1;
echo "The count is $count";
//Output: The count is 1
  • In Double quote strings will be printed after interpretation.
  • So it is better to use single quotes unless you need to interpret the variable value.

Advantage:

Using double quotes is you don’t need to use concatenation(.) operator for strings (Like ‘My blog is’.$blog).

Difference between single-quoted and double-quoted strings:

               Single Quoted Strings                Double Quoted Strings
A single-quoted string does not have variables within it interpreted. A double-quoted string does.
A single-quoted string can contain unescaped quotation marks. A double-quoted string can contain apostrophes without backslashes
Single-quoted strings are faster at runtime because they do not need to be parsed. double-quoted string are than single-quoted strings.
[ad type=”banner”]

Which option should be used?

Since, double quotes force PHP to evaluate the string  (even though it might not be needed) while single quotes not. So, using single quotes is faster than using double quotes.

So, use single quotes (‘ ‘) for string unless we need the double quotes (” “).  Also, parsing variables between strings takes more memory than concatenation.

Variable parsing in double quoted strings:

  • When a string is specified in double quotes, variables are parsed within it.
  • There are two types of syntax of using variables in double quoted strings:
  • simple one (directly as $string)
    complex one (with curly braces as {$string})
  • It provides a way to embed a variable, an array value, or an object property in a string with a minimum of effort.

Simple syntax:

If a dollar sign ($) is encountered, the parser will greedily take as many tokens as possible to form a valid variable name.

Php Code
$car = "car";

echo "I have a $car.";

// Invalid. "s" is a valid character for a variable name, but the variable is $car.
echo "I have many $cars.";

$colors = ['red','black'.'white'];
echo "I have a $colors[0] car.";

class Car {
public $name = "car";
}
$car = new Car();
echo "I have a $car->name.";

Complex syntax:

The complex syntax can be recognized by the curly braces surrounding the variable name. Enclose the variable name in curly braces to explicitly specify the end of the name.

Php Code
$car = "car";

echo "I have a {$car}.";

// Invalid. "s" is a valid character for a variable name, but the variable is $car.
echo "I have many $cars.";

$colors = ['red','black'.'white'];
echo "I have a {$colors[0]} car.";

class Car {
public $name = "car";
}
$car = new Car();
echo "I have a {$car->name}.";
[ad type=”banner”]

Categorized in: