检查多个字符串是否为空

编程入门 行业动态 更新时间:2024-10-24 04:31:50
本文介绍了检查多个字符串是否为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

可能重复: 更简洁的方法检查数组是否仅包含数字(整数) PHP检查是否为空字段

Possible Duplicate: More concise way to check to see if an array contains only numbers (integers) PHP checking if empty fields

我有一个提交10个字段的表单,应该填写其中7个,这就是我现在在PHP中检查它的方式:

I have form that submits 10 fields, and 7 of them should be filled, here is how i chek it now in PHP:

if (!$name || !$phone || !$email || !$mobile || !$email || !$state || !$street || ! $city) { echo '<div class="empty_p">You have empty fields!!!</div>';} else{ //process order or do something }

我的问题是:还有更简单的方法吗?因为有时候我还要检查更多的字符串(12-15)

My question is: is there more simple way to do this? Because sometimes I have even more strings to check (12-15)

推荐答案

<input type="text" name="required[first_name]" /> <input type="text" name="required[last_name]" /> ... $required = $_POST['required']; foreach ($required as $req) { $req = trim($req); if (empty($req)) echo 'gotcha!'; }

/*更新*/

好!伙计们,容易...

OK! guys, easy...

您可以使它更安全,就像类型转换一样,就像我们程序员为处理即将到来的数据(例如$id = (int) $_GET['id'],$username = (string) addslashes($_POST['username'])等)所要做的一样……

You can make it more secure, just with type casting as all we programmers do for out coming data, like $id = (int) $_GET['id'], like $username = (string) addslashes($_POST['username']) and so on...;

$required = (array) $_POST['required'];

然后,无论来自发布字段的内容是什么,这些代码都只是在寻找所需内容. 这就对了!嗯...

And then, what ever comes from post fields let them come, this code just seek what it need. That is it! Uhh...

更多推荐

检查多个字符串是否为空

本文发布于:2023-08-07 13:54:11,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1319414.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   字符串   为空

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!