Template helpers from php novice to ninja

In the excellent book PHP and MySQL novice to ninja on page 181 Kevin suggests the following

function html($text)
  return htmlspecialchars($text, ENT_QUOTES, 'UTF-8');
function htmlout($text)
  echo html($text);

to output the data

This simplified version seems to do the same job.

function html($text){
	$out = htmlspecialchars($text, ENT_QUOTES, 'UTF-8');
	echo $out;

Is there any problem with doing it like this?

Hi Matt,

Your function does indeed do the job, although you could make it simpler by skipping the temporary variable $out and just echo the return value of htmlspecialchars directly.

Personally, I’d prefer the function from the book that returns its output, as it’s more flexible/reusable. With functions that return their output, you have the option to do further processing. With your function, you are forced to echo the output.

Thank you…Makes sense

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