note 50493 deleted from function.isset by googleguy
| From: | googleguy@php.net | Date: | Wed, 16 Jan 2013 05:40:13 +0000 |
| Subject: | note 50493 deleted from function.isset by googleguy | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-193109@lists.php.net to get a copy of this message | ||
Note Submitter: dubmeier aaattt Y! daht calm
----
Here are some handy wrappers to isset that I use when I need to do common evaluations like: this
variable is set and has a length greater than 0, or: I want the variables value, or a blank, if not
set.
/**
* isset_echo()
*
* Accomplishes the following w/o warnings:
* echo $x;
* echo $x[$y];
* echo $x[$y][$z];
*
* FIXME: make this recursive so it works for N args?
*/
function isset_echo($x, $y=Null, $z=Null)
{
if (is_array($x)) {
if (array_key_exists($y, $x)) {
if (is_array($x[$y])) {
if (array_key_exists($z, $x[$y])) { echo $x[$y][$z]; }
}
else { echo $x[$y]; }
}
}
else { echo $x; }
}
/**
* isset_value()
*
* As above, but returns value instead of echoing
*/
function isset_value(&$x, $y=Null)
{
if (is_array($x)) {
if (array_key_exists($y, $x)) { return $x[$y]; }
}
else { return $x; }
}
/**
* isset_and_equals()
*
* As above, but ...
* Returns true if variable (or array member) is set and equaL to the first parameter
*/
function isset_equals($val, $w, $x=null, $y=null, $z=null) {
if (is_array($w)) {
if (array_key_exists($x, $w)) {
if (is_array($w[$x])) {
if (array_key_exists($y, $w[$x])) {
if (is_array($w[$x][$y])) {
if(array_key_exists($z, $w[$x][$y])) {
return ($w[$x][$y][$z] == $val) ? true : false;
}
} else {
return ($w[$x][$y] == $val) ? true : false;
}
}
} else {
return ($w[$x] == $val) ? true : false;
}
}
} else {
return ($w == $val) ? true : false;
}
}
/**
* isset_gt0()
*
* As above, but returns true only if var is set and it's length is > 0
*/
function isset_gt0(&$x)
{
if (isset($x) && strlen($x) > 0) { return true; }
else { return false; }
}