Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP从$u GET[';var';]设置变量_Php - Fatal编程技术网

PHP从$u GET[';var';]设置变量

PHP从$u GET[';var';]设置变量,php,Php,是否可以从$\u GET['var']设置变量值,方法如下: if( $foo = isset($_GET['foo']) ) // Or something close to this. I.e if $_GET['foo'] is set assign it's value then and there 而不是像我现在这样 if( isset($_GET['foo']) ) $foo = $_GET['foo']; 当它只有一个或两个时,这是可以的,但就像我一样,当你有10+$\u G

是否可以从
$\u GET['var']
设置变量值,方法如下:

if( $foo = isset($_GET['foo']) ) // Or something close to this. 
I.e if $_GET['foo'] is set assign it's value then and there
而不是像我现在这样

if( isset($_GET['foo']) )
$foo = $_GET['foo'];
当它只有一个或两个时,这是可以的,但就像我一样,当你有10+$\u GET时,它会变得丑陋。

用户三元运算符

 $foo = isset($_GET['foo']) ? $_GET['foo'] : "";
试一试


不,这是不可能的。你可以用一个三元语句来速记,但这并不是一个简单得多的语句

您最好编写如下函数:

function extractFromGet(array $keys) {
    $ret = []; // empty array

    foreach ($keys as $key) {
        $ret[$key] = isset($_GET[$key]) ? $_GET[$key] : null;
    }

    return $ret;
}
然后,您可以按如下方式调用它:

list ($name, $email, $telephone, $address) = extractFromGet(
    ['name', 'email', 'telephone', 'address']
);

我看到这个问题时不时地被问到。显然,没有人想到编写自定义函数来解决重复的代码任务;-)@阿尔瓦罗格·维卡里奥——很可能他根本没有想过使用函数。@Suresh Kamrushi在引导我
list ($name, $email, $telephone, $address) = extractFromGet(
    ['name', 'email', 'telephone', 'address']
);