在PHP中使用另一个条件的变量 [英] Using a variable from another condition in PHP
问题描述
我有这个代码:
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()的计算结果为true并且实际执行 $ 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中使用另一个条件的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!