Goal: test whether a key exists in a source without reading its value.
public function hasGet(string $key): bool;
public function hasPost(string $key): bool;
public function hasRaw(string $key): bool;Each is the equivalent of array_key_exists() against its source — and,
like array_key_exists(), a key whose value is null still counts as
present.
use InitPHP\Input\Inputs;
$input = new Inputs(
get: ['debug' => '1', 'note' => null],
post: ['email' => 'jane@example.com'],
raw: ['token' => 'abc'],
);
$input->hasGet('debug'); // true
$input->hasGet('note'); // true — present even though the value is null
$input->hasGet('email'); // false — that key is in post, not get
$input->hasPost('email'); // true
$input->hasRaw('token'); // true
$input->hasRaw('debug'); // falsehasGet() answers "is the key there?", not "does it hold a useful
value?". Use it to distinguish an absent field from one submitted empty:
$input = new Inputs(get: ['newsletter' => '']);
$input->hasGet('newsletter'); // true — the checkbox was submitted
$input->get('newsletter') === ''; // true — but it is empty| Call | Result |
|---|---|
hasGet('note') |
true |
hasGet('email') |
false |
hasRaw('token') |
true |
- Assuming case-insensitivity.
hasGet('Debug')isfalsewhen the key isdebug. Keys are case-sensitive. - Using presence as a truthiness test. A present key can still be an
empty string. Combine with a value read or a
requiredrule when you need a non-empty value.