tags:

views:

103

answers:

4

On a php file that many variables are received by $_REQUEST[] or $_POST[], and I have to check them in case the value is null with the function isset(), it is quite troublesome. Is there a better solution?

+4  A: 

How about using a combination of in_array and array_map, e.g.:

// array of possible parameters that can be passed by the client
$keys = array('username','password');

// this will store the names of the ones that are not present
$missing = array();

foreach($keys as $key) {
    if(!in_array($key, $_POST)) {
        $missing[] = $key;
    }
}

$nullOffsets = array_map("is_null", $_POST);

echo 'Printing missing params:<br />';
print_r($missing);
echo 'Printing null existing params:<br />';
print_r($nullOffsets);
karim79
This does not check array items that are supposed to be there but aren't there in the first place, which is probably why the OP uses `isset()` instead of `empty()`
Lukman
@Lukman - Thanks for your comment, I've edited the answer to address that.
karim79
This doesn't work because you are looping over $_POST, not $keys. You are just getting a list of unexpected keys which were posted
Tom Haigh
@Tom Haigh - fixed, and thanks for the input.
karim79
Hang on, will a parameter key even appear in the $_GET, $_POST or $_REQUEST superglobals if its value is `null`?
karim79
I think from the HTTP request there would be no way for PHP to differentiate between empty string and null, so you would always get an empty string if the value is empty?
Tom Haigh
Maybe you should use array_key_exists() instead of in_array()?
Steven
What does (!in_array($key, $_POST)) mean? $key is supposed to be index(key) of the array $_POST.
Steven
A: 

If you had the variables in an array (don't just use the request or post arrays) you could loop through them calling the isset() function. Depending on what your current code is, this might be 'better'.

Blair McMillan
A: 

User input checking is troublesome, but its a necessary evil.

Personally I prefer not using $_GET or $_POST other than copying needed content to my own variables for processing.

At the top of my .php file, I keep an array with names of values that I wish to copy from $_GET or $_POST

This adds up to:

// the following array needs to be modified when you change your input specs
$inputAllowed = array("name", "title", "company");
$input = array();
foreach($inputAllowed as $key)
    if( array_key_exists( $key, $_POST ) )
        $input[$key] = $_POST[$key];
    else
        $input[$key] = "";

Its easy to add an "is_null" check in there with handling for in case something shouldn't be null. Or you can first let the loop finish and then loop over $input

nash
This will generate a Notice if any of the named parameters are not POSTed
Tom Haigh
True. Sorry, dry coded that. Fixed with an array_key_exists
nash
A: 

You can try wrapping the array in an object.

class ArrayWrapper {
    private $data;
    public function __get($var) {
    if (!isset($this->data[$var])) {
        return false;
    }
    else {
        return $this->data[$var];
    }
    }
    public function __construct($a) {
    $this->data = $a;
    }
}

$a = array('test' => 1);

$aw = new ArrayWrapper($a);

if ($aw->test != false) {
    echo "test: ".$aw->test;
}
if ($aw->foo != false) {
    echo "foo: ".$aw->foo;
}
Peter Lindqvist