18

What could I use in PHP in place of the normal ' and (without ' or ") symbols around something?

Example:

echo("Hello, World!")
8
  • What do you mean by ` in place of the normal ' and " symbols` ?
    – Sarfraz
    Commented Jun 4, 2012 at 19:14
  • 4
    You can add a double quote by escaping it : "\"". Commented Jun 4, 2012 at 19:14
  • ...as, while the intent seems possibly misguided, I have yet to see alternatives mentioned.
    – user166390
    Commented Jun 4, 2012 at 19:15
  • And don't forget that in HTML, you'd better use ". Commented Jun 4, 2012 at 19:17
  • 2
    Actually, heredoc and nowdoc, like I described below, are fairly unknown features of PHP.
    – Jeroen
    Commented Jun 4, 2012 at 19:30

2 Answers 2

40

There are 4 ways to encapsulate strings, single quotes ', double quotes ", heredoc and nowdoc.

Read the full php.net article here.

Heredoc

A third way to delimit strings is the heredoc syntax: <<<. After this operator, an identifier is provided, then a newline. The string itself follows, and then the same identifier again to close the quotation.

http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc

$str = <<<EOD
Example of string
spanning multiple lines
using heredoc syntax.
EOD;

Nowdoc

Nowdocs are to single-quoted strings what heredocs are to double-quoted strings. A nowdoc is specified similarly to a heredoc, but no parsing is done inside a nowdoc. The construct is ideal for embedding PHP code or other large blocks of text without the need for escaping. It shares some features in common with the SGML construct, in that it declares a block of text which is not for parsing.

A nowdoc is identified with the same <<< sequence used for heredocs, but the identifier which follows is enclosed in single quotes, e.g. <<<'EOT'. All the rules for heredoc identifiers also apply to nowdoc identifiers, especially those regarding the appearance of the closing identifier.

http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.nowdoc

$str = <<<'EOD'
Example of string
spanning multiple lines
using nowdoc syntax.
EOD;

Escaping

If you want to use literal single or double quotes within single or double quoted strings, you have to escape them:

$str = '\''; // single quote
$str = "\""; // double quote

As Herbert noted, you don't have to escape single quotes within a double quoted strings and you don't have to escape double quotes within a single quoted string.


If you have to add quotes on a large scale, use the addslashes() function:

$str = "Is your name O'reilly?";
echo addslashes($str); // Is your name O\'reilly?
6
  • 1
    +1 for the references. However, the here-doc and now-doc look awfully similar ;-) It might be worthwhile expanding or noting the interpolation differences.
    – user166390
    Commented Jun 4, 2012 at 19:19
  • Added quotes from the php.net page as an explanation ;)
    – Jeroen
    Commented Jun 4, 2012 at 19:30
  • You don't have to escape single quotes inside double quotes or vice versa. e.g. "O'reilly" or 'She said, "Me too"'
    – Herbert
    Commented Jun 4, 2012 at 19:52
  • 1
    So "EOD" is arbitrary? Or some naming convention? Or a reserved name? For "end of data"? Perhaps include something to this effect in the answer? Commented Dec 15, 2019 at 17:21
  • 1
    Using addslashes is almost always wrong. The only valid use case is when you want to generate PHP code dynamically, which is something you should not do either. Commented Apr 12, 2020 at 7:19
0

In fact in the PHP older than 8.0, you can really write the string literal without any quotes(so called bareword) when there's no other global constant defined with the same name, since the bareword will be recognized as a constant, then fall back to string literal if any constant with that name is not defined.

Example: https://3v4l.org/cT1ie

<?php
var_dump(Y);
var_dump(date(Y));
  • Output for 8.0.0 - 8.0.28, 8.1.0 - 8.1.16, 8.2.0 - 8.2.3
Fatal error: Uncaught Error: Undefined constant "Y" in /in/cT1ie:3
Stack trace:
#0 {main}
  thrown in /in/cT1ie on line 3

Process exited with code 255.
  • Output for 7.2.0 - 7.2.34, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33
Warning: Use of undefined constant Y - assumed 'Y' (this will throw an Error in a future version of PHP) in /in/cT1ie on line 3
string(1) "Y"

Warning: Use of undefined constant Y - assumed 'Y' (this will throw an Error in a future version of PHP) in /in/cT1ie on line 4
string(4) "2023"
  • Output for 4.3.0 - 4.3.11, 4.4.0 - 4.4.9, 5.0.0 - 5.0.5, 5.1.0 - 5.1.6, 5.2.0 - 5.2.17, 5.3.0 - 5.3.29, 5.4.0 - 5.4.45, 5.5.0 - 5.5.38, 5.6.0 - 5.6.40, 7.0.0 - 7.0.33, 7.1.0 - 7.1.33
Notice: Use of undefined constant Y - assumed 'Y' in /in/cT1ie on line 3
string(1) "Y"

Notice: Use of undefined constant Y - assumed 'Y' in /in/cT1ie on line 4
string(4) "2023"

From https://wiki.php.net/rfc/deprecate-bareword-strings:

When PHP encounters an unquoted token such as FROB_ACTIVE, it tries to resolve it as a built-in or user-defined constant, but if no such constant exists, it treats it as equivalent to the quoted string 'FROB_ACTIVE', and issues an E_NOTICE message. This behaviour has been around since very early versions of PHP, but is inconsistent with the rest of the language, and can lead to serious bugs. This RFC proposes three things: to raise the level of the message to E_WARNING; to officially deprecate the fallback; and to remove it in PHP 8.0.

and its discussion: https://externals.io/message/98025