-
Notifications
You must be signed in to change notification settings - Fork 1
Presence Checks
Muhammet Şafak edited this page Jun 10, 2026
·
1 revision
Use the has* methods to 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 tell an absent field apart from one submitted
empty — the classic unchecked vs. empty checkbox problem:
$input = new Inputs(get: ['newsletter' => '']);
$input->hasGet('newsletter'); // true — the field was submitted
$input->get('newsletter') === ''; // true — but it is emptyWhen you need a non-empty value, combine a presence check with a value read
or, better, validate with a required rule:
$name = $input->post('name', null, ['required']); // null when missing OR empty$input = new Inputs(get: ['Token' => 'abc']);
$input->hasGet('Token'); // true
$input->hasGet('token'); // false — different key-
Assuming case-insensitivity.
hasGet('Debug')isfalsewhen the stored key isdebug. (1.x folded keys to lower-case; 2.0 does not — see Migration from 1.x.) - Using presence as a truthiness test. A present key can still be an empty string.
initphp/input · MIT License · part of the InitPHP family
Source · Issues · Discussions · Packagist · Contributing · Security Policy
Getting Started
Usage
Reference
Other