Skip to content

Instantly share code, notes, and snippets.

@megclaypool
Last active July 16, 2021 16:13
Show Gist options
  • Save megclaypool/e2440e33e6de8357b317d0faa0e67800 to your computer and use it in GitHub Desktop.
Save megclaypool/e2440e33e6de8357b317d0faa0e67800 to your computer and use it in GitHub Desktop.
Q&A with Jason on PHP quotes

Why do both of these work? Why does one need single quotes around the array key and the other doesn't?

echo '<li><a href="' . $item['slug'] . '">' . $item['title'] . '</a></li>';

echo "<li><a href=\"$item[slug]\">$item[title]</a></li>";

On the other hand, this doesn't work:

echo '<li><a href="$item[slug]">$item[title]</a></li>';

(it displays as $item[title] )


Items in single quotes get output verbatim, unprocessed Items in double quotes get processed

If you use curly braces, your editor will make it clear that the thing is a variable

echo "<li><a href=\"{$item['slug']}\">{$item['title']}</a></li>";

Jason likes to use: echo "<li><a href='{$item['slug']}'>{$item['title']}</a></li>";

If not including variables, definitely use single quotes all the time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment