检查多个字符串是否为空 [英] Check if multiple strings are empty
问题描述
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...
这篇关于检查多个字符串是否为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!