note 50493 added to function.isset
| From: | dubmeieraaatttY!dahtcalm at osu1 dot php dot net | Date: | Tue, 01 Mar 2005 23:13:58 +0000 |
| Subject: | note 50493 added to function.isset | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-85755@lists.php.net to get a copy of this message | ||
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; }
}
----
Manual Page -- http://www.php.net/manual/en/function.isset.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+50493
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+50493&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+50493&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+50493&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+50493&report=yes&reason=useless
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+50493&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+50493&report=yes
Search -- http://master.php.net/manage/user-notes.php