Access array value safely

3 weeks ago

ihsanberahim

this way could be used when accessing input form $_GET, $_POST, $_REQUEST.

Code

$_GET = [
    'hi' => 'wan',
];

if ($v = pos(array_column([$_GET], 'hi'))) {
    print_r(["got hi", $v]);
}

if (!($v = pos(array_column([$_GET], 'hi2')))) {
    print_r(["hi2 not there", $v]);
}

Output

Array
(
    [0] => got hi
    [1] => wan
)
Array
(
    [0] => hi2 not there
    [1] => 
)
=> true
HELO: Local email testing for your desktop!