library.php
My own HTML tag encoder that will print HTML codes according to the input.
<?php
function tag($tagname, $content = NULL, array $properties = NULL)
{
$html = "<$tagname";
if (!($properties === NULL))
foreach ($properties as $name => $value) {
$html .= " $name=\"$value\"";
}
$html .= ($content === NULL || $content == "") ? " />" : ">$content</$tagname>";
return $html;
}
?>
index.php
The test index.php
file that will run the HTML encoder from the library.
<?php
require 'library.php';
echo tag("head",
tag("meta","",array("name"=>"title","content"=>"Test Print"))
);
echo tag("body",
tag("div",tag("p","test print".tag("br")."test print"),array())
);
?>
Output:
<head><meta name="title" content="Test Print" /></head><body><div><p>test print<br />test print</p></div></body>
Question:
Using this library will make my code more readable when adding more PHP code. Instead of this <div><?php $variable ?></div>
, I can use this echo tag("div",$variable);
, but the latter will definitely be longer compared to just typing HTML code.
Should I not create such function and stay with coding HTML?