PHP Textbox Display Function

I find this entry-level function useful when creating large forms with dozens of textboxes. There are several different variations possible, depending on how much control you need over the final textbox. You can add variables for classes, maxlength, value, even JavaScript.

function make_textbox($name) {
     print '<input type="text" name="' . $name . ' /">';

The code produced by the above with “mytextbox” passed as the variable looks like this:

<input type="text" name="mytextbox" />

The textbox itself looks like this: 

Something that short might look like a waste of space and time—it’s not that much shorter than actually typing the code out, right? But what if your textbox code looks like this?

<input type="text" name="mycomplextextbox" class="textboxclass"
value="My Pre-Filled Value" maxlength="30" onfocus="if
(this.value==this.defaultValue) this.value='';" onblur="if
(this.value=='') this.value=this.defaultValue;" />

Suddenly, that function is looking a lot more attractive! Instead of typing out all that code, you could instead just send make_textbox("mycomplextextbox", "textboxclass", "My Pre-Filled Value", 30) to the following function:

function make_textbox($name, $class, $value, $maxlength) {
     echo "<input type='text' name='$name' class='$class' value='$value'
          maxlength='$maxlength' onfocus=\"if (this.value==this.defaultValue)
          this.value='';\" onblur=\"" if (this.value=='') this.value=
          this.defaultValue;\" />";

Doing this cleans up your code and makes it easier to read—in the case above, you’re replacing three lines of text with one. Have 10 textboxes? You just saved 20 lines of code and improved legibility a zillion percent! Imagine the effort you’d save if you combined this with a few other simple functions, like one to generate a state select box?