$p = (isset($_REQUEST["p"])?$_REQUEST["p"]:"");
这是我通常在我的php代码中使用的常用行。我一直认为是否有更好的(小而快)的方式来写同样的东西?
$p = (isset($_REQUEST["p"])?$_REQUEST["p"]:"");
这是我通常在我的php代码中使用的常用行。我一直认为是否有更好的(小而快)的方式来写同样的东西?
你想要的更短吗?
当然,如果你使用这个每一次访问请求的值,你应该什么地方创建一个函数,然后使用:
function reqVal($val, $default = "", $no_sql = true)
{
$var = isset($_REQUEST[$val]) ? $_REQUEST[$val] : $default;
$var = $no_sql ? nosql($var) : $var;
return $var;
}
function getVal($val, $default = "", $no_sql = true)
{
$var = isset($_GET[$val]) ? $_GET[$val] : $default;
$var = $no_sql ? nosql($var) : $var;
return $var;
}
function postVal($val, $default = "", $no_sql = true)
{
$var = isset($_POST[$val]) ? $_POST[$val] : $default;
$var = $no_sql ? nosql($var) : $var;
return $var;
}
现在添加SQL incjection检查:
function nosql($var)
{
if (is_array($var)) {
foreach ($var as $key => $elem) $var[$key] = nosql($elem);
} else if ($var === null) {
return null;
} else {
if (get_magic_quotes_gpc()) $var = stripslashes($var);
$var = mysql_real_escape_string($var);
}
return $var;
}
和访问它总是简单的像这样:
$p = reqVal('p', 0);
$p = getVal('p', 'something', false);
$p = postVal('p'); // or just forget the 2nd and 3rd parameter
这确实很常见,我想知道在PHP中没有原生的方式。大多数开发人员编写自己的函数以安全地从数组中读取。
/**
* Gets the value associated with the specified key from an array.
* @param array $array The array to search for the key.
* @param mixed $key The key of the value to get.
* @param mixed $default The default value to return, if the
* specified key does not exist.
* @return mixed Value that is associated with the specified
* key, or the default value, if no such key exists.
*/
function getValueFromArray($array, $key, $default = null)
{
$containsKey = isset($array[$key]);
if ($containsKey)
return $array[$key];
else
return $default;
}
/**
* Gets the value associated with the specified key from an array.
* @param array $array The array to search for the key.
* @param mixed $key The key of the value to get.
* @param mixed $value Retrieves the found value, or is set to null
* if the key could not be found.
* @return bool Returns true if the key could be found, otherwise false.
*/
public function tryGetValueFromArray($array, $key, &$value)
{
$containsKey = isset($array[$key]);
if ($containsKey)
$value = $array[$key];
else
$value = null;
return $containsKey;
}
尽管我都是面向对象编程,但如果OP要求更短更快的方法:函数和特别是类不是要走的路 –
@EliasVanOotegem - 它取决于观点,一旦你有了函数,你必须写的代码确实比较短,只需调用'getValueFromArray($ _ REQUEST,'p')'。就我所知,没有本地功能。 – martinstoeckli
当然,一旦函数完全写出来,你不得不少写几个字符,但函数调用更昂贵,并且速度更慢(与OP现在使用的内联三元相比)。看到他正在经历一个更小更快的选择,功能提供更短的代码,但速度更慢。只是一个侧面说明,在速度方面 –
您可以在这里找到http://php.net/manual/en/function.isset.php在用户提供人注意部分不同的解决方案的例子很多。
试试这个:
function get_if_set($varname, $parent=null) {
if (!is_array($parent) && !is_object($parent)) {
$parent = $GLOBALS;
}
return array_key_exists($varname, $parent) ? $parent[$varname] : null;
}
是包装在一个函数的现有代码的回答都不错 - 他们确确实实收拾的代码,如果你有一群人。
但是更好的解决方案是在开始之前根据一组期望值清理整个请求数组。
例如:
function sanitiseRequest() {
$expected = array(
'p' => '',
'id' => 0,
//etc
);
//throw away any input that wasn't expected...
foreach($_REQUEST as $key=>$value) {
if(!isset($expected[$key]) { unset $_REQUEST[$key]; }
}
//and for any expected values that weren't passed, set them to the defaults.
foreach($expected as $key=>$defvalue) {
if(!isset($_REQUEST[$key]) { $_REQUEST[$key] = $defvalue; }
}
}
然后简单的代码开始添加调用此功能,您将不必担心你的代码做isset($_REQUEST[..])
其他地方。
此概念可以扩展为强制传入参数成为正确的数据类型,或者您可能想要执行的任何其他数据清理。这可以让您完全相信传入的数据按照您的预期填充。
希望有所帮助。
,如果你想要的东西更短,而且内容与空(字符串)默认值,以下工作:
$p = @$_REQUEST['p'];
@是错误抑制操作,并会不断的表达从给人如果警告该值未设置。
http://www.php.net/manual/en/language.operators.errorcontrol.php
这太棒了。感谢分享和链接! –
这一个很适合我。 而且你不必再写两次这个名字。 如果它已经设置,它不会改变它。因此,使用register_globals对旧应用程序进行快速n脏转换是安全的。
function getIfSet($key, $default = null)
{
global $$key;
if(!isset($$key)){
if(isset($_REQUEST[$key])){
$$key=$_REQUEST[$key];
}else{
if(!is_null($default)){
$$key = $default;
}
}
}
}
function getIfSetArray($list){
foreach($list as $item){
getIfSet($item);
}
}
getIfSet('varname');
getIfSetArray(['varname_1','varname_2']);
echo $varname;
echo $varname_1;
内联条件越短越好。你当然可以定义一个函数。问题不在于你的代码,而在于你不知道是否实际设置了请求参数。你的情况最好和最干净的方式可能是通过使用检查存在的函数将请求值映射到它来初始化所需的变量。 – matthias
这可能会帮助你http://stackoverflow.com/a/12798041/1226894 – Baba
你不能这么做。为什么你想要它更短?不够短? –