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.
[pastacode lang=”php” manual=”echo%20’This%20is%20%5C’test%5C’%20string’%3B%0A%2F%2FOutput%3A%20This%20is%20’test’%20string%0A” message=”Php Code” highlight=”” provider=”manual”/]

Example:

[pastacode lang=”php” manual=”%2F%2F%20Outputs%3A%20It%20is%20a%20simple%20string.%0Aecho%20’It%20is%20a%20simple%20string.’%3B%0A%20%0A%2F%2F%20Outputs%3A%20It’ll%20be%20a%20simple%20string.%0Aecho%20’It%5C’ll%20be%20a%20simple%20string.’%3B%0A%20%0A%24string%20%3D%20’string’%3B%0Aecho%20’This%20is%20a%20simple%20%24string.’%3B%0A%2F%2FOutput%3A%20This%20is%20a%20simple%20%24string.%0A%20%0A%2F%2F%20Outputs%3A%20This%20is%20a%20simple%20%5Cn%20string.%0Aecho%20’This%20is%20a%20simple%20%5Cn%20string.’%3B%0A” message=”Php Code” highlight=”” provider=”manual”/]
  • 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:

[pastacode lang=”php” manual=”%2F%2F%20Outputs%3A%20It%20is%20a%20simple%20string.%0Aecho%20%22It%20is%20a%20simple%20string.%22%3B%0A%20%0A%2F%2F%20Outputs%3A%20It’ll%20be%20a%20simple%20string.%0Aecho%20%22It’ll%20be%20a%20simple%20string.%22%3B%0A%20%0A%24string%20%3D%20’string’%3B%0Aecho%20%22This%20is%20a%20simple%20string.%22%3B%0A%2F%2FOutput%3A%20This%20is%20a%20simple%20%24string.%0A%20%0A%2F%2F%20Outputs%3A%20This%20is%20a%20simple%20string.%0Aecho%20%22This%20is%20a%20simple%20%5Cn%20string.%22%3B%0A” message=”Php Code” highlight=”” provider=”manual”/] [pastacode lang=”php” manual=”%24count%20%3D%201%3B%0Aecho%20%22The%20count%20is%20%24count%22%3B%0A%2F%2FOutput%3A%20The%20count%20is%201%0A” message=”Php Code” highlight=”” provider=”manual”/]
  • 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.

[pastacode lang=”php” manual=”%24car%20%3D%20%22car%22%3B%0A%20%0Aecho%20%22I%20have%20a%20%24car.%22%3B%0A%20%0A%2F%2F%20Invalid.%20%22s%22%20is%20a%20valid%20character%20for%20a%20variable%20name%2C%20but%20the%20variable%20is%20%24car.%0Aecho%20%22I%20have%20many%20%24cars.%22%3B%0A%20%0A%24colors%20%3D%20%5B’red’%2C’black’.’white’%5D%3B%0Aecho%20%22I%20have%20a%20%24colors%5B0%5D%20car.%22%3B%0A%20%0Aclass%20Car%20%7B%0A%20%20%20%20public%20%24name%20%3D%20%22car%22%3B%0A%7D%0A%24car%20%3D%20new%20Car()%3B%0Aecho%20%22I%20have%20a%20%24car-%3Ename.%22%3B%0A” message=”Php Code” highlight=”” provider=”manual”/]

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.

[pastacode lang=”php” manual=”%24car%20%3D%20%22car%22%3B%0A%20%0Aecho%20%22I%20have%20a%20%7B%24car%7D.%22%3B%0A%20%0A%2F%2F%20Invalid.%20%22s%22%20is%20a%20valid%20character%20for%20a%20variable%20name%2C%20but%20the%20variable%20is%20%24car.%0Aecho%20%22I%20have%20many%20%24cars.%22%3B%0A%20%0A%24colors%20%3D%20%5B’red’%2C’black’.’white’%5D%3B%0Aecho%20%22I%20have%20a%20%7B%24colors%5B0%5D%7D%20car.%22%3B%0A%20%0Aclass%20Car%20%7B%0A%20%20%20%20public%20%24name%20%3D%20%22car%22%3B%0A%7D%0A%24car%20%3D%20new%20Car()%3B%0Aecho%20%22I%20have%20a%20%7B%24car-%3Ename%7D.%22%3B%0A” message=”Php Code” highlight=”” provider=”manual”/] [ad type=”banner”]

Categorized in: