如果 POST/GET 数据为空/假,我可以为它设置默认值吗,比如
$this->input->post("varname", "value-if-falsy")
?
所以我不必像这样编码
$a = $this->input->post("varname") ?
$this->input->post("varname") :
"value-if-falsy"
最佳答案
您必须覆盖默认行为。
在 application/core
创建 MY_Input.php
class MY_Input extends CI_Input
{
function post($index = NULL, $xss_clean = FALSE, $default_value = NULL)
{
// Check if a field has been provided
if ($index === NULL AND ! empty($_POST))
{
$post = array();
// Loop through the full _POST array and return it
foreach (array_keys($_POST) as $key)
{
$post[$key] = $this->_fetch_from_array($_POST, $key, $xss_clean);
}
return $post;
}
$ret_val = $this->_fetch_from_array($_POST, $index, $xss_clean);
if(!$ret_val)
$ret_val = $default_value;
return $ret_val;
}
}
然后在您的 Controller 中:
$this->input->post("varname", "", "value-if-falsy")
关于forms - CodeIgniter POST/GET 默认值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28622113/