PHP:isset和!空的捷径? [英] PHP: a short cut for isset and !empty?

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

问题描述

我想知道是否有更好的想法来解决下面的问题,

我有一个带有多个输入字段的表单,比如

 < input name =pg_titletype =textvalue =/> 
< input name =pg_subtitletype =textvalue =/>
< input name =pg_descriptiontype =textvalue =/>
< input name =pg_backdatetype =textvalue =/>

但是有时在我的表单中不需要某些输入字段,例如,我只需要我的数据库注入的页面标题,

 < input name =pg_titletype =textvalue =/> 

我有另一个php页面来处理 $ _POST data,

  $ pg_title = null; 
$ pg_subtitle = null;
$ pg_description = null;
$ pg_backdate = null;

if(isset($ _ POST ['pg_title'])&&!empty($ _ POST ['pg_title']))$ pg_title = $ _POST ['pg_title'];
if(isset($ _ POST ['pg_subtitle'])&&!empty($ _ POST ['pg_subtitle']))$ pg_subtitle = $ _POST ['pg_subtitle'];
if(isset($ _ POST ['pg_description'])&&!empty($ _ POST ['pg_description']))$ pg_description = $ _POST ['pg_description'];
if(isset($ _ POST ['pg_backdate'])&&!empty($ _ POST ['pg_backdate']))$ pg_backdate = $ _POST ['pg_backdate'];

每次我都要检查 $ _ POST 不是空的,否则它的变量将被设置为 null ,这样我不会注入一个空的空间到我的分贝。



我发现 isset $!$!
$ b

是否有任何默认的php函数到'缩短'上面的过程?或者我必须编写一个用户定义函数来处理这个问题?



或者有另外一种方法可以做到这一点?



感谢。

编辑:

非常感谢帮助。



只需在我的php页面中处理$ _POST数据的额外代码,

  $ sql =
UPDATE root_pages
SET
pg_url =?,
pg_title =?,
pg_subtitle =?,
pg_backdate =?,
pg_description =?,
...
updated_by =?
WHERE pg_id =?
;

$ result = $ connection-> run_query($ sql,array(
$ pg_url,
$ pg_title,
$ pg_subtitle,
$ pg_backdate ,
$ pg_description,
...
$ pg_id
));

当您看到 $ pg_subtitle,$ pg_backdate,$ pg_description等总是出现在我的查询中。所以如果我得到 $ pg_subtitle =''而不是 $ pg_subtitle = null ,那么当我没有数据时,我的数据库记录将为该列有一个空的空间。



感谢: - )

解决方案

你可以使用一个简单的函数

$ p $函数post_value_or($ key,$ default = NULL){
return isset($ _ POST [$ key])&& !empty($ _ POST [$ key])? $ _POST [$ key]:$ default;
}

然后使用:

  $ pg_title = post_value_or('pg_title'); 
//或使用默认
$ pg_title = post_value_or('pg_title','无标题');


I wonder if there any better ideas to solve the problem below,

I have a form with a number of input fields, such as,

<input name="pg_title" type="text" value="" />
<input name="pg_subtitle" type="text" value="" />
<input name="pg_description" type="text" value="" />
<input name="pg_backdate" type="text" value="" />
etc

But sometimes don't need certain input fields above in my form, for instance, I only need the page title for my db injection,

<input name="pg_title" type="text" value="" />
 etc

And I have another php page to handle the $_POST data,

$pg_title = null;
$pg_subtitle = null;
$pg_description = null;
$pg_backdate = null;

 if(isset($_POST['pg_title']) && !empty($_POST['pg_title']) ) $pg_title = $_POST['pg_title'];
 if(isset($_POST['pg_subtitle']) && !empty($_POST['pg_subtitle']) ) $pg_subtitle = $_POST['pg_subtitle'];
 if(isset($_POST['pg_description']) && !empty($_POST['pg_description']) ) $pg_description = $_POST['pg_description'];
 if(isset($_POST['pg_backdate']) && !empty($_POST['pg_backdate']) ) $pg_backdate = $_POST['pg_backdate'];

Every time I will have to check if the $_POST of certain input field is set and not empty, otherwise its variable will be set to null, so that I won't inject an empty space into my db.

I find the isset and !empty in the if-condition are very repetitive when I have a long list of variable to handle.

Is there any default php function to 'shorten' the process above? Or do I have to write an user define function to handle this?

Or maybe there is another way doing this?

Thanks.

EDIT:

Thanks so much guys for the help.

Just some extra code in my php page that handle the $_POST data,

$sql = "
    UPDATE root_pages
    SET 
        pg_url = ?, 
        pg_title = ?,
        pg_subtitle = ?,
        pg_backdate = ?,
        pg_description = ?,     
        ...
        updated_by = ?
    WHERE pg_id = ?
    ";

    $result = $connection->run_query($sql,array(
        $pg_url, 
        $pg_title,
        $pg_subtitle,
        $pg_backdate,
        $pg_description,        
        ...
        $pg_id
        ));

as you see that $pg_subtitle, $pg_backdate, $pg_description, etc always present in my query. so if I get $pg_subtitle = '' instead of $pg_subtitle = null when there is no data in it, my db record will have an empty space for that column.

Thanks :-)

解决方案

You can use a simple function

function post_value_or($key, $default = NULL) {
    return isset($_POST[$key]) && !empty($_POST[$key]) ? $_POST[$key] : $default;
}

Then use:

$pg_title = post_value_or('pg_title');
// OR with a default
$pg_title = post_value_or('pg_title', 'No Title');

这篇关于PHP:isset和!空的捷径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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