PHP检查所有变量是否为空 [英] PHP Check if all variables are empty

查看:223
本文介绍了PHP检查所有变量是否为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将大量数据从表单提交到php脚本,并且我想要一种有效的方法来检查所有数据是否正确提交.

I am submitting a lot of data from a form to a php script and I want an efficient way to check if all the data was submitted correctly.

<?php
$Name = $_POST['Name'];
$ID = $_POST['ID'];
$Submit = $_POST['submit'];
$Reset = $_POST['reset'];
$Topic_1 = $_POST['1'];
$Topic_2 = $_POST['2'];
$Topic_3 = $_POST['3'];
$Topic_4 = $_POST['4'];
$Topic_5 = $_POST['5'];
$Topic_6 = $_POST['6'];
$Topic_7 = $_POST['7'];
$Topic_8 = $_POST['8'];
$Topic_9 = $_POST['9'];
$Topic_10 = $_POST['10'];
$Topic_11 = $_POST['11'];
$Topic_12 = $_POST['12'];
$Topic_13 = $_POST['13'];
$Topic_14 = $_POST['14'];
$Topic_15 = $_POST['15'];

推荐答案

$fields_names = array_merge(array(('Name','ID','submit')) , range(1,15)); /// instead of manually setting the numbered variables , used range function / @Fred-ii-

foreach($fields_names as $key)
{
 if(empty($_POST[$key]) && $_POST[$key] != 0) // Making sure it's not 0 / @deceze
 {
  $error = true;
  echo $key . ' is not set';
 }
}

if(!$error)
{
  //Anything is set.
}

注意 我不确定检查值是否不是empty就足够了. 考虑添加更多验证规则.

NOTICE I'm not sure that checking if the value isn't empty is enough. Consider adding more validation rules.

这篇关于PHP检查所有变量是否为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆