使用函数在php中输出html标签的效率

Trying to come up with a basic way to display code in a way that would be nicely maintainable, I thought of doing something like this:

echo htmlfunction('a',array('href'=>'http://google.com'),'google');

to generate:

<a href="http://google.com">google</a>

this would use as a global scale function to output all html tags. that way, if I ever wanted to change the way my html is displayed, I could easily do it with one tweak.

does using this kind of outputting mean a grave loss of agility in performance?

thanks.

No, but you would be reinventing the wheel and in a wrong way.

Instead of writing echo title("bar"); to echo <title>bar</title>, use a templating engine to output your dynamic data inside a static HTML template, that looks like <title>$title</title> where your code has $template->assign('title','bar');.

That way you will be keeping markup as markup and code as code and in separate files, which is a worthy goal in at least the maintainability and flexibility departments.

Look, for example, at Smarty

No, it would not cause a grave loss of agility -- it would likely be hardly noticeable. Many PHP content management systems (such as Drupal and Joomla) do the same sort of thing for all their links.

I would avoid writing functions like that in most cases. Notice how it takes more code to write than you actually have output. That's also additional processing the server is going to need to do each and every time. It probably won't be a huge performance problem at first, but if used excessively, it could be bad.

If you want to abstract presentation from logic, might I recommend a template engine like Smarty