我正在尝试解决 PHP 练习,但无法理解发生了什么
I am trying to solve a PHP exercise but am unable to understand what's happening
首先,我很抱歉我的问题标题不相关,因为我不知道如何写得更好。
首先,我是一个 PHP 初学者。我正在解决一些 PHP 练习,我遇到了一个我不知道从哪里开始的问题:
function q3() {
// I am supposed to write stuff here and not change anything to get the question right.
}
function a3($admin = false) {
assertion_group("Question 3");
foreach ($GLOBALS as $k => $v) $$k = $v;
if ($admin) {
$file = q3("edsi.pem");
}
$key = @file_get_contents($file);
$key = substr($key, 0, 4);
assert($key == substr(file_get_contents(__FILE__), 0, 4));
return $key;
}
首先,我明白 $GLOBALS
的作用,但为什么要将 $$k
分配给 $v
(因此 $k 值分配给 $v 值)? $GLOBALS
是否获取函数内的值?
如何设置$admin = true
?我相信通过 q3()
,但我不明白如何...
接下来让我最感到困惑的是:$file = q3("edsi.pem")
。由于我的 q3 函数没有任何参数,而且我不应该添加一个,我该如何使用它?!
非常感谢大家的回答。我再次为这个非常模糊的问题道歉...
编辑:
在@mario 的帮助下更好地理解整个混乱,基本上我必须在 q3 中输入的是:
if ($info == 'edsi.pem') {
$info = __FILE__;
return $info;
}
加上 q3
(q3($info)
) 的参数并在 header...
中添加 ?admin=true
再次感谢!
First of all, I understand what $GLOBALS does, but why assign the $$k to $v (so the $k value to the $v value)? And does $GLOBALS get the values within functions?
foreach … $$k = $v;
片段所做的基本上是 extract($GLOBALS);
这不是传递参数的非常有用的方法。仅当全局变量具有一定的描述性名称,并且它们没有被误用作不同代码段之间的状态标志时,使用全局变量才有意义。
不,并非所有函数都立即提供全局变量。继续阅读 variable scope。
How can I set $admin = true? I believe through q3(), but I don't see how...
您混淆了这里的函数名(正是因为它们不是很有用的函数名)。您可以在调用 a3()
时传递 $admin 参数,而不是:
a3(true);
Next thing that confuses me most is: $file = q3("edsi.pem"). As my q3 function doesn't have any arguments, and that I'm not supposed to add one, how can I use that?!
在 q3
中获取参数的唯一方法是根据 func_get_arg()
.
而且真的,除非这是关于如何不编写代码的练习或关于奇怪用例的教程,否则你真的不应该再费心了。
首先,我很抱歉我的问题标题不相关,因为我不知道如何写得更好。
首先,我是一个 PHP 初学者。我正在解决一些 PHP 练习,我遇到了一个我不知道从哪里开始的问题:
function q3() {
// I am supposed to write stuff here and not change anything to get the question right.
}
function a3($admin = false) {
assertion_group("Question 3");
foreach ($GLOBALS as $k => $v) $$k = $v;
if ($admin) {
$file = q3("edsi.pem");
}
$key = @file_get_contents($file);
$key = substr($key, 0, 4);
assert($key == substr(file_get_contents(__FILE__), 0, 4));
return $key;
}
首先,我明白 $GLOBALS
的作用,但为什么要将 $$k
分配给 $v
(因此 $k 值分配给 $v 值)? $GLOBALS
是否获取函数内的值?
如何设置$admin = true
?我相信通过 q3()
,但我不明白如何...
接下来让我最感到困惑的是:$file = q3("edsi.pem")
。由于我的 q3 函数没有任何参数,而且我不应该添加一个,我该如何使用它?!
非常感谢大家的回答。我再次为这个非常模糊的问题道歉...
编辑:
在@mario 的帮助下更好地理解整个混乱,基本上我必须在 q3 中输入的是:
if ($info == 'edsi.pem') {
$info = __FILE__;
return $info;
}
加上 q3
(q3($info)
) 的参数并在 header...
?admin=true
再次感谢!
First of all, I understand what $GLOBALS does, but why assign the $$k to $v (so the $k value to the $v value)? And does $GLOBALS get the values within functions?
foreach … $$k = $v;
片段所做的基本上是 extract($GLOBALS);
这不是传递参数的非常有用的方法。仅当全局变量具有一定的描述性名称,并且它们没有被误用作不同代码段之间的状态标志时,使用全局变量才有意义。
不,并非所有函数都立即提供全局变量。继续阅读 variable scope。
How can I set $admin = true? I believe through q3(), but I don't see how...
您混淆了这里的函数名(正是因为它们不是很有用的函数名)。您可以在调用 a3()
时传递 $admin 参数,而不是:
a3(true);
Next thing that confuses me most is: $file = q3("edsi.pem"). As my q3 function doesn't have any arguments, and that I'm not supposed to add one, how can I use that?!
在 q3
中获取参数的唯一方法是根据 func_get_arg()
.
而且真的,除非这是关于如何不编写代码的练习或关于奇怪用例的教程,否则你真的不应该再费心了。