Wikis & learning

Decide when a string needs escaping

Single and double quotes give string literals different rules. To display punctuation in a code-generated label, distinguish the source-code character from the character that the visitor should see.

Single and double quotes give string literals different rules. To display punctuation in a code-generated label, distinguish the source-code character from the character that the visitor should see.

Walkthrough

Single-quoted text leaves a dollar sign alone. Double-quoted text can expand a variable and turn a newline escape into a line break. The two output statements deliberately contain similar-looking source.

PHP example

<?php
$label = 'April';
echo 'Literal: $label', PHP_EOL;
echo "Value: $label\n";

Expected output

Literal: $label
Value: April

Change and check

Replace the second pair of double quotes with single quotes, then predict which characters will become visible.