Using the PHP Escape Slash

Using the escape slash

$speaker = 'Adam';
$withEscape = "$speaker says, \"PHP is useful!\"";

Output: Adam says, "PHP is useful!"

Not using the escape slash

$speaker = 'Adam';
$withoutEscape = "$speaker says, PHP is useful!";

Output: Adam says, PHP is useful!

Explanation

In the first double-quoted string, each backslash tells PHP to treat the following quotation mark as part of the text rather than the end of the string. The second string does not contain quotation marks inside it, so it needs no escape slash. In both examples, PHP replaces the $speaker variable with its value.

Back to Activity 3