HTML/PHP: Single and double quotes

If PHP programmers are creating a link from variables often you can see the following code:

echo '' . 
       $linktext . ''; 

or

?>


or

echo "$linktext";

Apart from the personal preferences of the programmer all these snippets have one thing in common: they are hard to read. "Sure!", you will say, "attributes in HTML have to be enclosed in double quotes and in PHP I have to use one of the above methods." Are you sure? Do attributes really have to be enclosed in double quotes? The simple answer is: No!
Let's take a look at the specification:

[...] By default, SGML requires that all attribute values be delimited using either double quotation marks (ASCII decimal 34) or single quotation marks (ASCII decimal 39). Single quote marks can be included within the attribute value when the value is delimited by double quote marks, and vice versa. [...]

Double or single quotation marks. That will make things much easier:

echo "$linktext";

As you see the code is much easier to read and that simply happened because you read the specs!


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

To respond on your own website, enter the URL of your response which should contain a link to this post's permalink URL. Your response will then appear (possibly after moderation) on this page. Want to update or remove your response? Update or delete your post and re-enter your post's URL again. (Find out more about Webmentions.)