问题描述
我有这个代码:
if(!isset($_GET["act"]))
{
$display->display("templates/install_main.html");
if(isset($_POST["proceed"]))
{
$prefix = $_POST["prefix"];
}
}
if($_GET["act"] == "act")
{
echo $prefix;
}
基本上我之前提出过类似的问题,问题是,如何使变量可访问?请提及是否有任何方法可以做到这一点,即使改变了它的完成方式(有人告诉我可以使用课程,但不太确定如何完成),或任何其他使其可访问的方式.
Basically I've made a similar question before, thing is, HOW can I make the variable accessible? please mention if there is any way to do so, even with changing the way it's done (someone told me it's possible with a class but not quite sure how it can be done), or any other way to make it accessible.
谢谢!
推荐答案
PHP 的变量作用域是函数级的.$prefix 将在您的第二个 if()
IF 中可用,另一个 if() 评估为真并实际执行了 $prefix = ...
代码.
PHP's variable scope is function-level. $prefix would be available in your second if()
IF the other if()'s evaluated to true and actually executed that $prefix = ...
code.
例如
if (true) {
$foo = 'bar'; // always executes
}
if (false) {
$baz = 'qux'; // never executes
}
echo $foo; // works just fine
echo $baz; // undefined variable, because $baz='qux' never executed.
另请注意,PHP 不能进行时间旅行:
Also note that PHP is not capable of time travel:
echo $x; // undefined variable;
$x = 'y';
echo $y; // spits out 'y'
较早"的代码不会有较晚的"变量可用,因为实际创建/分配值给这些变量的代码还没有执行.
"earlier" code will not have "later" variables available, because the code that actually creates/assigns values to those variables won't have executed yet.
这篇关于在 PHP 中使用来自另一个条件的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!