Escaping double and single quotes

Hi,

Here’s what I have.

foreach ($author_query as $author) {
    echo $author['name'].", ";
}

I am trying to add a href link to it, but I’m having issues with quotes. What’s the right way to do this? The code below doesn’t work because I already opened single quotes with a href, then tried to use it with person_id.

foreach ($author_query as $author) {
    echo "<a href='/person/$author[\'person_id\']'>".$author['name'].", ". "</a>";
}

When outputting array values in a string, I generally break out of the string to access the array value, or enclose the array access in braces:

foreach ($author_query as $author) {
    echo "<a href='/person/" . $author['person_id'] . "'>" . $author['name'] . "</a>";
}

or

foreach ($author_query as $author) {
    echo "<a href='/person/{$author['person_id']}'>{$author['name']}</a>";
}

Both methods should give you what you’re looking for :slight_smile:

2 Likes

What about something like this?

echo '<a href="/person/' . $author['person_id'] . '">' . $author['name'] . ',</a>';

EDIT: pipped by a better answer. :slight_smile:

2 Likes

Thank you guys!

I really appreciate it.

echo '<a href="/person/' .$author['person_id'] .'">'
     	  .$author['name'] .', '
     .'</a>';

I prefer the above method:

1 .Never ever use \ because it breaks the flow and is confusing
2. always start and end with a single quote
3. add double quotes within the string (ie. width=“123” height=“456”)
4. use .$variable to add to string (throws error if not defined)
5. single quotes within array elements or variable
6. indent wherever possible

edit:
Just checked and noticed @ralphm’s recommendation is the same without indentation.

1 Like

Remember that the only escapes allowed inside single quotes are to escape single quotes and that everything is considered to be text.

With double quotes the content gets parsed for variable names and additional escapes are allowed - eg. “\n” is the only way to specify a new line character.

Added items 7 & 8

  1. Never ever use \ because it breaks the flow and is confusing
  2. always start and end with a single quote
  3. add double quotes within the string (ie. width=“123” height=“456”)
  4. use .$variable to add to string (throws error if not defined)
  5. single quotes within array elements or variable
  6. indent wherever possible
  7. only escape single quote within string ( ’ alt=“JB's Link” ’ )
  8. only double-quote linefeeds, carriage returns, tabs, ( .“\n\t” )
echo '<a href="/person/' .$author['person_id'] .'" 
         title="' .$author['name'] .'\'s Link" >'
     	  .$author['name'] .', '
     .'</a>'
     . "\n\r\t\t"
     ;

Have I missed any other anomalies?

1 Like

if you need apostrophes in names (like “author’s”) consider using the typographical apostrophe. It looks better and doesn’t need to be escaped.

something else to keep in mind is the printf() family of functions.

printf('<a href="/person/%1$s" title="%2$s’s Link">%2$s</a>'."\n\r\t\t", $author['person_id'], $author['name']);
1 Like

To save anyone trying to find the apostrophe character:

&#39;    // straight quote mark/apostrophe 	

&#146;   // right curly quote mark/apostrophe

&rsquo;  // right curly quote mark/apostrophe

http://www.tedmontgomery.com/tutorial/HTMLchrc.html

1 Like

Not true. PHP is one of the only languages that allows this.

$var = 'This is a
multiple line
text string';

Of course, having the line break actually do a line break is rarely desireable.

And while we are talking multiple lines of text in PHP, let’s not forget HEREDOC and NOWDOC.

Scott

2 Likes

Another question, is it okay to store names in the database like below? That way I don’t have to escape but I don’t know whether it’s a good practice or not.

O&#39;Reilly

Don’t do that - you should only convert to use HTML escape codes immediately before outputting to HTML. Suppose you decide to output to a PDF ow a word processor instead and now you have garbage in the content.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.