note 128771 added to function.isset

From: Date: Tue, 01 Aug 2023 02:41:44 +0000
Subject: note 128771 added to function.isset
Groups: php.notes 
Request: Send a blank email to php-notes+get-220731@lists.php.net to get a copy of this message
If you are annoyed by the behavior of isset() concerning null values, here is a handy function for you. its similar to array_key_exists but, its a lot more flexible and can check for multiple array keys across multiple arrays. Not recursive! Tested on php 8.1.6, linux <?php /** * is_set * @author DJ Eckoson * @link @eckosongh Facebook Page * checks whether variable names are set within the global space or they exists as an key and return if they are set (even if their values are null) * @param string $var_name * name of the first variable to check * @param array|null|string * optional array to check for key (if null, checks from $GLOBALS) OR * other variable names to check OR * other variable names and their associated arrays to their right (use null for global variables, optional if its the last parameter) * check examples below */ function is_set(string $var_name, array|null|string ... $args): bool { $vars[$var_name] = null; if (array_key_exists(0, $args)) { if (is_array($args[0])) { $vars[$var_name] = $args[0]; } elseif (is_string($args[0])) { goto main; } unset($args[0]); } main: if ($args) { $args = array_reverse($args); $cur_array = null; array_walk($args, function ($value) use (&$cur_array, &$vars): void { if (!is_string($value)) { $cur_array = $value; } else { $vars[$value] = $cur_array; } }); } foreach ($vars as $name => $array) { if (!array_key_exists($name, $array??$GLOBALS)) return false; } return true; } // Examples $arr1 = range(0, 5); $arr2 = [ 'a' => 1, 'b' => 2, 'c' => 'hELLO wORLD!' ]; $gender = 'male'; $age = 12; var_dump(is_set('age')); // true var_dump(is_set('age', null)); // true var_dump(is_set('age', $arr1)); // false var_dump(is_set('age', array())); // false var_dump(is_set('age', array('age' => 48))); // true var_dump(is_set('age', 'arr1', null, 'b', $arr2, 0, 3, 4, $arr1, 'gender')); // true var_dump(is_set('age', 'arr1', null, 'b', $arr2, 0, 3, 4, $arr1, 'gender', null)); // true $c=$d=$e=$a=2; $arr = [1,4]; var_dump(is_set('a', 'c', null, 0, 1, $arr)); // true var_dump(is_set('a', 'c', null, 0, 4, $arr)); // false ?> Note: it won't work for variables declared locally inside a function; however you can use it for checking if array keys exists inside a function ---- Server IP: 45.112.84.4 Probable Submitter: 45.91.157.134 (proxied: 154.160.27.185) ---- Manual Page -- https://php.net/manual/en/function.isset.php Edit -- https://main.php.net/note/edit/128771 Del: integrated -- https://main.php.net/note/delete/128771/integrated Del: useless -- https://main.php.net/note/delete/128771/useless Del: bad code -- https://main.php.net/note/delete/128771/bad+code Del: spam -- https://main.php.net/note/delete/128771/spam Del: non-english -- https://main.php.net/note/delete/128771/non-english Del: in docs -- https://main.php.net/note/delete/128771/in+docs Del: other reasons-- https://main.php.net/note/delete/128771 Reject -- https://main.php.net/note/reject/128771 Search -- https://main.php.net/manage/user-notes.php

« previous php.notes (#220731) next »