在 PHP 中是否有更好的解决方案来检查许多变量以查看其中一个变量是否为 null?

发布于 2024-08-11 10:18:15 字数 159 浏览 11 评论 0原文

在 php 文件中,许多变量由 $_REQUEST[]$_POST[] 接收,我必须检查它们以防值为 null< /code> 配合函数isset(),相当麻烦。有更好的解决方案吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

罗罗贝儿 2024-08-18 10:18:15

如何结合使用 in_arrayarray_map,例如:

// 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);

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);
苹果你个爱泡泡 2024-08-18 10:18:15

如果您的变量位于数组中(不仅仅使用 request 或 post 数组),您可以调用 isset() 函数循环遍历它们。根据您当前的代码,这可能会“更好”。

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'.

小鸟爱天空丶 2024-08-18 10:18:15

您可以尝试将数组包装在对象中。

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

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;
}
落花浅忆 2024-08-18 10:18:15

用户输入检查很麻烦,但它是不可避免的祸害。

就我个人而言,我不喜欢使用 $_GET$_POST,而是将所需内容复制到我自己的变量中进行处理。

在我的 .php 文件的顶部,我保留了一个数组,其中包含我希望从 $_GET$_POST 复制的值的名称,

这总计为

// 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] = "";

:在那里添加一个“is_null”检查,并处理以防某些内容不应该为空。或者您可以先让循环完成,然后循环 $input

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文