While reading a Linux Magazine the other day, I saw a summary of changes which are perhaps going into PHP v6. One of these was a function called ifsetor. Namely :
//Old :
$name = "";
if(isset($_POST['name'])) {
$name = $_POST['name'];
}
Is replaced with a single call of :
$name = ifsetor($_POST['name'], "");
Which seems syntatically much nicer :), so my version is :
function ifsetor(&$param, $or) {
if(isset($param)) {
return $param;
}
return $or;
}
Of course, while writing this I did a quick Google and found this .
I also thought that perhaps the function could/should go one step further and also apply a regexp on the variable, which could be used to sanitise input, so perhaps :
function ifsetorre(&$param, $or, $re = false) {
if(isset($param)) {
if($re === false) {
return $param;
}
if(preg_match($re, $param)) {
return $param;
}
}
return $or;
}
// fail if '$bar' contains lower case letters.
$foo = ifsetorre($bar, "default", "/^[^a-z]+$/");
Is a better approach; where it performs a regexp match on the input as well - this would at least provide a simple (one line) validation check as well.
My thoughts are partly prompted by the Zend Framework InputFilter which I think is a good idea (and I definately like it's unsetting of $_POST) but perhaps not sufficient - no length checking, or matching for weird characters.
Are regexp too hard for most people to use?
Comments
Proper ifsetor
This is how ifsetor is supposed to behave (with added regular expression match). Anyone is free to use it.
function ifsetor(&$var, $or, $regex = false) {if (empty($var) || ($regex && preg_match($regex, $var))) {
$var = $or;
}
return $var;
}
Sorry about the de-tabification.
Why not use this extendable one.
function ifsetor()
{
$numargs = func_num_args();
$i = 0;
while($i < $numargs)
{
$current_arg = func_get_arg($i);
if(is_bool($current_arg) && $current_arg == true)
{
return $current_arg;
}
else if(!empty($current_arg))
{
return $current_arg;
}
$i++;
}
return false;
}
$X = 5;
$Y = false;
$Z = 2;
$B = 4;
$temp = @ifsetor($X, $Y, $Z, $B);
print $temp;
Try commenting out the $X, $Y, $Z, $B declarations. You should get the next one in the sequence.
Default $or parameter
How about a default $or parameter?
function ifsetor(&$var, $or = null) { return isset($var) ? $var : $or; }
erratum
your code :
$name = ifsetor($_POST['name', "");
is missing a ]
$name = ifsetor($_POST['name]', "");
well spotted; thanks!
I've updated the original article.
Do we really need to pass
Do we really need to pass the variable by reference?
it stops PHP complaining about an undefined index
It will stop PHP complaining if e.g. $_POST['name'] doesn't exist. Presumably this is because it doesn't try to copy the value when passing it into the function.
Perhaps you could also do :
function ifsetor(@$_POST['name'], 'whatever') { ... }
instead?
I am using PHP 5 with error
I am using PHP 5 with error reporting turned on and PHP does not complain that the variable is not set so is this just a PHP 4 thing?
Post new comment