PHP 制作一个简单的 if-isset-empty 函数
我正在为一家打印机公司编写一个工作表应用程序。 我收到了大量的表格。 对于每个输入字段,我必须检查 $_POST
变量是否已设置,如果设置,则回显该值。 (如果出现某些错误,例如在验证错误之后,用户不应重新输入整个表单)
示例代码:
if(isset($_POST['time'])&&!empty($_POST['time'])){echo $_POST['time'];}
我必须实现大约一百次。 所以我试图找出某种函数来使其简单易读。
像这样:
function if_post_echo($key, $default = "") {
if(isset($_POST[$key])&&!empty($_POST[$key])){
echo $_POST[$key];
}else{
echo $default;
}
}
但这行不通。 我尝试将 $_POST
传递给 $key
变量,如下所示:
if_post_echo($_POST['time'])
function if_request_echo($key, $default = "") {
if(isset($key)&&!empty($key)){
echo $key;
}else{
echo $default;
}
}
我也尝试过:
function if_request_echo($key, $default = null) {
return isset($_REQUEST[$key])&&!empty($_REQUEST[$key]) ? $_REQUEST[$key] : $default;
}
没有任何合理的结果。
问题:
如何伪造一个函数来查找必要的 $_POST 变量并返回它,或者如果它未设置则返回一个空字符串。 有没有办法对 $_GET
和 $_REQUEST
执行此操作? (或者只是复制?)
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?)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
您的 PHP 测试函数:
然后在您的表单 HTML 中:
$_REQUEST
(链接)是一个 PHP 超级全局,其中包含POST ($_POST
) 和 GET ($_GET
) 请求参数。如果您只想捕获 POST 请求参数,那么它将是:
例如。
Your PHP testing function:
Then in your form HTML:
$_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:
For example.
如果您有大量字段,我建议您还使用默认值数组:
$fields
将包含带有 POST 数据的表单值列表或预设的默认值。没有问题,明白吗?array_filter 手册页 特别是:如果未提供回调,则输入的所有条目等于 FALSE 的将被删除。以某种方式解释该解决方案背后的工作原理。
If you have a large amount of fields, I would propose that you also use an array of defaults:
$fields
will then contain a list of form values with either the POST data or a preset default. No isset, see?array_filter man page particularly: If no callback is supplied, all entries of input equal to FALSE will be removed. Goes some way to explaining the working behind this solution.
这应该有效:
如果您遇到问题,我建议您尝试
var_dump($_POST)
或print_r($_POST)
来查看所有内容是否已正确发布。This should work:
If you're having problems I recommend that you try
var_dump($_POST)
orprint_r($_POST)
to see if everything has been properly posted.请注意,这是多余的:
未设置的变量将始终为“空”,因此
isset()
隐含在您的empty()
调用中。对于您的逻辑,您可以通过以下方式获得相同的结果:
Just to note, this is redundant:
An unset variable is going to always be "empty", so
isset()
is implied in yourempty()
call.For your logic you can achieve the same result with just:
你的第一个功能对我来说非常完美。
你为什么认为它不起作用?
然而,更好的变体是
使用它:
Your first function works perfectly to me.
Why do you think it doesn't work?
However, a better variant would be
To use it :
如果您想具体了解:
像以前的函数一样调用。
If you want to know specifically:
Called like previous function.
您的方法在这里似乎工作正常:
我使用名称 test 进行了简单的输入,表单方法是
POST
并使用echo if_post_echo('test');
。它在页面上发布了文本框中的内容。
Your method seems to work fine here:
I made a simple input with the name test and the form method is
POST
and usingecho if_post_echo('test');
.It posted on the page what was in the text box.
此功能在 PHP 7 中添加为“空合并运算符”,使用两个问号:
echo ($_GET['message'] ?? 'default-if-not-set');
< a href="https://wiki.php.net/rfc/isset_ternary" rel="nofollow">https://wiki.php.net/rfc/isset_ternary
This feature is being added in PHP 7 as the "Null Coalesce Operator" using two question marks:
echo ($_GET['message'] ?? 'default-if-not-set');
https://wiki.php.net/rfc/isset_ternary