Simplify your coding - string handling in HTML & PHP

Beverley Portlock
CERTIFIED EXPERT
Published:
Updated:
It should be a truism that simple, clearly written code is easier to maintain than complex and impenetratable code. Yet a common problem in PHP is that many programmers (especially those new to PHP) find some of the program code hard to read. They sometimes compound this error when mixing HTML with PHP and the chief culprit is the "double quote". It is not unusual to see code that looks like this
 
echo "<input name=\"$someName\" type=\"text\" value=\"$somevalue\">\n";

Open in new window




It is quite easy to lose a double quote somewhere and spend quite a bit of time looking for it or matching pairs. The obvious alternative is to encase the PHP string in single quotes but this affects the PHP variable substitution mechanism which then requires further workarounds. Many people believe that (X)HTML or XML requires double quotes, but the W3C standards are quite clear - either " or ' can be used.

The solution is therefore to enclose strings in double quotes and place HTML attributes in single quotes. This allows the PHP substitution mechanism to function whilst allowing HTML to have its attributes enclosed as required by the standards. Our awkward statement shown above now looks like this
 
echo "<input name='$someName' type='input' value='$someValue'>\n";

Open in new window




This clearly eliminates a number of sources of confusion by removing the need to escape double quotes, removes the need to use concatenation characters and makes it much easier to match HTML (single) quotes up and it allows variable substitution to function as well. Of course, a problem occurs if you are using javascript in the HTML and the javascript requires parameters to be passed. In this situation the escaped double quotes are back, but their effect is minimal compared to the chaos that can ensue if everything was double quoted. So to extend our example a little further
echo "<input name='$someName' onmouseup='javascript:aFunc(\"prm1\",\"prm2\");' type='input' value='$someValue'>\n";

Open in new window




It is worth trying to keep your code as simple and clear as possible. Remember - if it goes wrong it will probably be you that has to edit it, so make your life as easy as possible.
2
3,881 Views
Beverley Portlock
CERTIFIED EXPERT

Comments (4)

Richard QuadlingSenior Software Developer

Commented:
There are 2 additional mechanisms that new users should be aware of; Heredoc and nodoc.

They work in very similar ways.

By far the easiest way to show you is by example.

<?php
// Heredoc
echo <<< END_SOME_LABEL
<input name="{$someName}" type="input" value="{$someValue}" />

END_SOME_LABEL;
?>

Assuming that the variables have some values then this will output ...

<input name="richard" type="input" value="quadling" />


<?php
// 'Nowdoc'
echo <<< 'END_SOME_LABEL'
<input name="{$someName}" type="input" value="{$someValue}" />

'END_SOME_LABEL';
?>

Again, assuming that the variables have some values then this will output ...

<input name="{$someName}" type="input" value="{$someValue}" />



Yep. Nowdoc outputs the text as is.

You can also use a doublequoted label for heredoc ...


<?php
// "Heredoc"
echo <<< "END_SOME_LABEL"
<input name="{$someName}" type="input" value="{$someValue}" />

"END_SOME_LABEL";
?>


More can be read at http://docs.php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc and http://docs.php.net/manual/en/language.types.string.php#language.types.string.syntax.nowdoc

Commented:
I personally prefer printf an sprintf . I think they are more compact than here/now docs and less confusing as concatenating strings. The one disadvantage is that when the strings require many variables it can get a bit confusing, but for the most part they aren't.

$format = "<input name='%s'  type='input'  value='%s' />\n";
printf($format, $name, $value);

Open in new window

gr8gonzoConsultant
CERTIFIED EXPERT

Commented:
I would also suggest enclosing variables in curly braces like RQuadling suggested. Not only does it help PHP parse out variables (tiny performance boost), but more importantly, it makes those variables stand out a bit more when reading code.

Using heredoc (again, as RQuadling suggested) can simplify complex mixtures of HTML, PHP, and Javascript.
CERTIFIED EXPERT

Author

Commented:
Sorry for all the multiple edits - it took me a while to get to grips with this new editor.

gr8gonzo - I feel that RQuadling's points stand on their own merit and the examples he has given demonstrate his point well so rather than duplicate them back in the original article.

Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.

Get access with a 7-day free trial.
You Belong in the World's Smartest IT Community