PHP做一个简单的if-isset-empty函数 [英] PHP Make a simple if-isset-empty function

查看:117
本文介绍了PHP做一个简单的if-isset-empty函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为打印机公司编写工作表应用程序。
我越来越多的表单。
对于每一个输入字段,我都要检查是否设置了 $ _ POST 变量,并且如果回显值。 (如有错误,例如验证错误后,用户不应重新键入整个表单)。

  if(isset($ _ POST ['time'])&&!empty($ _ POST ['time'])){echo $ _POST [时间'];} 

我必须实现这个约一百次。
所以我试图找出某种功能来使这个简单易读。



类似这样:

 函数if_post_echo($ key,$ default =){
if(isset($ _ POST [$ key])&&!empty($ _POST [$ key])){
echo $ _POST [$ key];
} else {
echo $ default;
}
}

但这不行。
我尝试为 $ key 变量传递 $ _ POST ,如下所示:

  if_post_echo($ _ POST ['time'])

函数if_request_echo($ key,$ default = ){
if(isset($ key)&&!empty($ key)){
echo $ key;
} else {
echo $ default;
}
}

我也试过这个:

 函数if_request_echo($ key,$ default = null){
return isset($ _ REQUEST [$ key])&&!空($ _ REQUEST [$ key])? $ _REQUEST [$ key]:$ default;
}

没有任何合理的结果。

问题:



如何伪造一个查找必要的 $ _ POST 变量的函数并返回它,或者如果未设置,则返回一个空字符串。
还有一种方法可以为 $ _GET $ _REQUEST 执行此操作吗? (或简单复制?)

解决方案

您的PHP测试函数:

<?php 
函数test_req($ key,$ default =''){
if(isset($ _ REQUEST [$ key])and
!empty($ _ REQUEST [$ key])){
return $ _REQUEST [$ key];
} else {
return $ default;
}
}
?>

然后在您的表单HTML中:

 < input name =my_fieldvalue =<?php echo htmlentities(test_req('my_field'));?> /> 

$ _ REQUEST (链接)是 PHP super global ,它包含POST( $ _ POST )和GET(<$ c $

如果您只想捕获POST请求参数,那么它将是:

 <?php 
函数test_req($ key,$ default =''){
if(isset($ _ POST [ $ key])和
!empty($ _ POST [$ key])){
return $ _POST [$ key];
} else {
return $ default;
}
}
?>

例如。

I'm coding a worksheet app for a printer company. I'm getting flood of forms. For every single input field I have to check if the $_POST variables are set, and if, so echo back the value. (In case of some error, for example after a validation error, the user shouldn't retype the whole form)

Sample code:

if(isset($_POST['time'])&&!empty($_POST['time'])){echo $_POST['time'];}

I had to implement this about a hundred times. So I tried to figure out some kind of function to make this simple and readable.

Something like this:

function if_post_echo($key, $default = "") {
    if(isset($_POST[$key])&&!empty($_POST[$key])){
    echo $_POST[$key];   
    }else{
    echo $default;   
    }
}

But this wont work. I have tried to pass in the $_POST for the $key variable like this:

if_post_echo($_POST['time'])

function if_request_echo($key, $default = "") {
        if(isset($key)&&!empty($key)){
        echo $key;   
        }else{
        echo $default;   
        }
    }

And I also tried this:

function if_request_echo($key, $default = null) {
    return isset($_REQUEST[$key])&&!empty($_REQUEST[$key]) ? $_REQUEST[$key] : $default;
}

Without any reasonable outcome.

The question:

How can I forge a function that looks for the necessary $_POST variable and returns it or if its unset then returns an empty string. And is there a way to do this for $_GET and $_REQUEST, too? (Or simply duplicate?)

解决方案

Your PHP testing function:

<?php
function test_req($key, $default = '') {
    if(isset($_REQUEST[$key]) and
       !empty($_REQUEST[$key])) {
        return $_REQUEST[$key];
    } else {
        return $default;
    }
}
?>

Then in your form HTML:

<input name="my_field" value="<?php echo htmlentities(test_req('my_field')); ?>" />

$_REQUEST (linked) is a PHP super global that contains both POST ($_POST) and GET ($_GET) request parameters.

If you only want to capture POST request parameters then it would be:

<?php
function test_req($key, $default = '') {
    if(isset($_POST[$key]) and
       !empty($_POST[$key])) {
        return $_POST[$key];
    } else {
        return $default;
    }
}
?>

For example.

这篇关于PHP做一个简单的if-isset-empty函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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