输入值0 =空? [英] Input value 0 = empty?

查看:152
本文介绍了输入值0 =空?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  $ required_fields = 

我有一个页面,您可以注册您的狗,我希望所有这些字段都是必需的:数组('姓名','年龄','性别','品种','大小');
foreach($ _POST as $ key => $ value){
if(empty($ value)&& in_array($ key,$ required_fields)=== true){
$ errors [] ='所有标有*的域都是必需的。
break 1;






$ b

问题是,如果有人输入0(它我指示他们这样做,如果这只狗是一只小狗),提交似乎将该字段读取为空(给我这个错误)。我进一步检查删除任何没有整数等,但最好的解决方案和最简单的形式为用户,我仍然认为他们能够输入0作为价值。无论如何,有没有什么办法可以让我的PHP代码读取的值不为空?

解决方案

From 手册



以下事情被认为是空的:

 (一个空字符串)
0(0作为一个整数)
0.0(0作为浮点数)
0(0作为字符串)
NULL
FALSE
array()(一个空数组)
$ VAR; (声明的变量,但没有值)

您可能会考虑的一个修复方法是这:

  if((empty($ value)&& $ value!= 0)&& in_array( $ key,$ required_fields)=== true){


I have a page where you register your dog, and I want all these fields to be required:

$required_fields = array('name', 'age', 'gender', 'breed', 'size');
foreach ($_POST as $key => $value) {
    if (empty($value) && in_array($key, $required_fields) === true) {
        $errors[] = 'All fields marked with * are required.';
        break 1;
    }
}

The problem is, that if someone enters 0 (which I instruct them to do if the dog is a puppy), the submission seems to read that field as empty (giving me this error). I have checks further down removing any none integers etc., but the best solution and easiest form for users I still think is having them being able to enter 0 as a value. Anyway, is there any way I can make my php code read the value as not null?

解决方案

From the manual:

The following things are considered to be empty:

"" (an empty string)
0 (0 as an integer)
0.0 (0 as a float)
"0" (0 as a string)
NULL
FALSE
array() (an empty array)
$var; (a variable declared, but without a value)

A fix you may consider would be to do somethng like this:

if ((empty($value) && $value != 0) && in_array($key, $required_fields) === true) {

这篇关于输入值0 =空?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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