Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/273.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在PHP中检查所有变量是否为空_Php - Fatal编程技术网

如何在PHP中检查所有变量是否为空

如何在PHP中检查所有变量是否为空,php,Php,我必须检查所有变量是否为空 我用下面的方法 if(empty($first_name) || empty($last_name) || empty($email) || empty($password) || empty($contact_no) || empty($cnic)) { echo json_encode(array('condition' => 'error','data' => 'Please fill all inpu

我必须检查所有变量是否为空 我用下面的方法

if(empty($first_name) || 
   empty($last_name) || 
   empty($email) || 
   empty($password) || 
   empty($contact_no) || 
   empty($cnic)) {
    echo json_encode(array('condition' => 'error','data' => 'Please fill all inputs')); 
    exit;
}
上面的代码正常工作,但这看起来有点奇怪。是否有更短或更专业的方法来检查所有输入?Ta像这样:

$array = [
   'first_name' =>  'John',
    'last_name' => 'Doe',
    'email' => '',
];

if(count($array) != count(array_filter($array))){
    echo 'some rows are empty';
}
如果您想知道哪些特定字段为空,可以使用
array\u diff\u key

$array = [
   'first_name' =>  'John',
    'last_name' => 'Doe',
    'email' => '',
];

$filled = array_filter($array);

if(count($array) != count($filled)){
    $empty = array_diff_key($array, $filled);
    echo 'field(s) '.implode(', ', array_keys($empty)).' are empty';
}
输出:

field(s) email are empty
显然,您可以改进错误字符串,或者您所拥有的内容。所以在你的情况下,我会这样做

if(count($array) != count($filled)){
    $empty = array_keys(array_diff_key($array, $filled));
    $last = '';
    if(count($empty) > 1 ){
       $last = ' and '.array_pop($empty);
    }
    $fields = implode(', ', $empty).$last; 
    echo json_encode(array('condition' => 'error','data' => 'Please fill in '.$fields));
}
所以它应该输出如下内容:

Please fill in email

Please fill in first_name and email

Please fill in first_name, last_name and email
你甚至可以:

$empty = array_map(function($item){
   return ucwords(str_replace('_', ' ', $item));
}, $empty);
如果你想喜欢的话,可以选择,
first\u name
并将其改为
first name

所以。。。把所有这些放在一起:

$array = [
   'first_name' =>  '',
    'last_name' => '',
    'email' => '',
];

$filled = array_filter($array);

if(count($array) != count($filled)){
    $empty = array_map(function($item){
        return ucwords(str_replace('_', ' ', $item));
    },array_keys(array_diff_key($array, $filled)));
    $last = '';
    if(count($empty) > 1 ){
       $last = " and '".array_pop($empty)."'";
    }
    $fields = "'".implode("', '", $empty)."'".$last; 
    echo json_encode(['condition' => 'error','data' => 'Please fill in '.$fields]);
}
产出:

{"condition":"error","data":"Please fill in 'First Name', 'Last Name' and 'Email'"}
你可以看到


现在,它准确地告诉他们需要哪些字段,而不是一些一般性错误。

您可以使用数组来实现此目的,也可以对相同的代码执行另一种方法,如下所示

if($first_name || $last_name || $email || $password || $contact_no || $cnic){
echo json_encode(array('condition' => 'error','data' => 'Please fill all inputs'));    
exit; }
这将起作用,因为它与检查变量的空值相同。但如果未定义变量,则将失败

isset()函数用于检查是否设置了变量。如果变量已使用unset()函数取消设置,则将不再设置该变量。如果测试变量包含空值,则isset()函数返回false

版本:(PHP4及以上)

语法:

isset(variable1, variable2......) 
根据我的选择,我们可以这样做

if(isset($first_name,$last_name,$email,$password,$contact_no,$cnic) { 
   // true conditon
} else {
   // false condition 
}

使用数组、计数、过滤器、计数。只有当
Any
为空时,您才希望检查
All
是否为空,否则您将使用And和not or。您可以创建自己的输入验证程序类,例如使用策略设计模式。我认为这是一种更专业的方式。这不一样,
$first\u name
可以抛出一个未定义的变量,
empty($first\u name)
不会发出警告。当然,没有问题这似乎不适用于
$\u POST
作为数组:\n您将要将POST复制到数组中,因此,例如
$array=$\u POST
,然后从那里开始
$\u POST
是一个超全局的,可能有一些特殊的考虑,