[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More


Simplify your coding - string handling in HTML & PHP

Published on
7,896 Points
2 Endorsements
Last Modified:
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.
LVL 40

Expert Comment

by:Richard Quadling
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.

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


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

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

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


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 ...

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


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


Expert Comment

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

LVL 38

Expert Comment

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.
LVL 34

Author Comment

by:Beverley Portlock
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.

Featured Post

HTML5 and CSS3 Fundamentals

Build a website from the ground up by first learning the fundamentals of HTML5 and CSS3, the two popular programming languages used to present content online. HTML deals with fonts, colors, graphics, and hyperlinks, while CSS describes how HTML elements are to be displayed.

Join & Write a Comment

Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

Keep in touch with Experts Exchange

Tech news and trends delivered to your inbox every month