English 中文(简体)
Is there a better solution to check many variables to see if one of them is null in PHP?
原标题:

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?

最佳回答

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);
问题回答

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 .

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;
}

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





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签