This functions solves the problem of always writing the same code to check if a get or post request is set or not.
I created this because I was tired of the extra code needed just to see if a request variable has the array key set or not. I don't want to use error suppression because those errors come in handy so I created this. This does not use request super global only get or post. Gone are the days of writing a bunch of if is set pairs in your php code. ternary if statement for variable assignment.
Sample usage( add get variable to your url like so http://localhost/test.php?userid=23)
The variable now has the value of 23 and you can sanitize that data and do type checking too.
Background
I created this because I was tired of the extra code needed just to see if a request variable has the array key set or not. I don't want to use error suppression because those errors come in handy so I created this. This does not use request super global only get or post. Gone are the days of writing a bunch of if is set pairs in your php code. ternary if statement for variable assignment.
The Code
Code: Php
<?php
function isRequest($name,$type,$val = NULL)
{
strtoupper($type); // convert type value to all uppercase for switch statement
$returnVal; // create variable that is not initiated yet but holds boolean values casted as numerics
switch($type) // check if $type is either get or post request types
{
case 'GET': // is get perform actions below
if(isset($_GET[$name])) // if get super global has been give the array key of $name
{
if(!is_null($val)) // get super global does have the array key of $name check if the value of $val is not empty; its not empty
{
if($_GET[$name] == $val) // $val is not empty check if the value for the get super global matches $val; matches return 1 for true
{
$returnVal = 1;
}
else // doesn't match return 0 for false
{
$returnVal = 0;
}
} // value for $val is empty but get super global has an array key of $name; return 1 for true
else
{
$returnVal = 1;
}
} // get super global doesn't have an array key of
else
{
$returnVal = 0;
}
break;
case 'POST': // check if $type is post and perform actions below
if(isset($_POST[$name])) // check if post super global has array key of $name; it does return 1 for true
{
$returnVal = 1;
}
else // it doesn't return 0 for false
{
$returnVal = 0;
}
break;
default: // $type value does not match our type list set $returnVal to 0 or false
$returnVal = 0;
break;
}
return $returnVal; // give response of boolean back from operation
}
?>
Sample usage( add get variable to your url like so http://localhost/test.php?userid=23)
PHP Code:
$uID = isRequest('userid','GET') ? $uID = $_GET['userid'] : 0;
echo $uID;
