generate_input_element()
function generate_input_element(string $type, string $name, ?string $value = null, bool|string|null $checked = false, array $attributes = []): string
Description
Generates a raw HTML input element with full control over type, name, value, checked state, and additional attributes. This is the underlying function used by other form helpers and provides the most flexibility for custom input field generation.
Parameters
| Parameter | Type | Description | Default |
|---|---|---|---|
| $type | string | The type attribute for the input element (e.g., 'text', 'email', 'checkbox'). | N/A |
| $name | string | The name attribute for the input element. | N/A |
| $value | ?string | The value attribute for the input element. | null |
| $checked | bool|string|null | Whether the input element should be checked (for radio/checkbox types). Accepts true, false, 'checked', or null. | false |
| $attributes | array | An associative array of additional HTML attributes. | [] |
Return Value
| Type | Description |
|---|---|
| string | The generated HTML input element as a string. |
Example Usage
PHP
// Text input
echo generate_input_element('text', 'username', 'John');
// Checkbox
echo generate_input_element('checkbox', 'agree', 'yes', true, ['class' => 'form-checkbox']);
// Custom input with multiple attributes
echo generate_input_element('number', 'quantity', 1, false, ['min' => 0, 'max' => 100, 'step' => 1]);