PHP用于创建表单元素的函数.好的做法?

ntg*_*ner 1 html php

我不是一个使用插件或任何东西,所以我现在试图避免像Zend这样的东西,但我想看看用PHP函数创建"模板"是否是一个好习惯?我现在忘记了很多PHP,一旦得到他的回答就会重新回到它,但我想的是这样的:

PHP

function input($type, $label, $id, $class) {
    echo "<div>";
    echo "<input type=" .$type. " class=" .$class. " id=" .$id. />";
    echo "<label for=" .$id. ">".$label."</label>";
    echo "</div>";
}
Run Code Online (Sandbox Code Playgroud)

HTML

<div class="wrapper">
    <?PHP input("text", "Username", "username", "input"); ?>
</div>
Run Code Online (Sandbox Code Playgroud)

当然这个代码需要根据我的需要进行调整,但我想知道它是否会被视为良好的做法?或者我应该偏离做这样的事情?

Jan*_*roň 7

我看到了评论,我希望你不要把我钉在十字架上,说大多数人会同意可重用性:返回价值而不是回应它

function input($type, $label, $id, $class) {
  // create the div here ...
  return $divString;
}
Run Code Online (Sandbox Code Playgroud)

然后按照您的意愿处理结果......

<div class="wrapper">
    <?PHP echo input("text", "Username", "username", "input"); ?>
</div>
Run Code Online (Sandbox Code Playgroud)

或者像这样使用它

$formElements = input("text", "Username", "username", "input") . input("password", "Password", "password", "input");