在 PHP 中使用来自另一个条件的变量

2023-10-12php开发问题
1

本文介绍了在 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() 评估为真并实际执行了 $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 中使用来自另一个条件的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

PHP实现DeepL翻译API调用
DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以...
2025-08-20 php开发问题
168

PHP通过phpspreadsheet导入Excel日期数据处理方法
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的...
2024-10-23 php开发问题
287

mediatemple - 无法使用 codeigniter 发送电子邮件
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)...
2024-08-23 php开发问题
11

Laravel Gmail 配置错误
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)...
2024-08-23 php开发问题
16

将 PHPMailer 用于 SMTP 的问题
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)...
2024-08-23 php开发问题
4

关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)...
2024-08-23 php开发问题
17