HTML/PHP: Single and double quotes

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

echo '' . 
       $linktext . ''; 




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!


Leave a Reply

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