如何访问两个php文件中的全局变量?

如何访问两个php文件中的全局变量?,php,html,forms,Php,Html,Forms,我在file1.php中有一个变量: global $name; $name = ""; 在同一目录下的file2.php中 <label> <span>Username:</span> <input id="name" type="text" name="username" value ="<?php echo $GLOBALS['name'];?>" placeholder="Enter your user name"

我在file1.php中有一个变量:

global $name;
$name = "";
在同一目录下的file2.php中

<label>
    <span>Username:</span>
    <input id="name" type="text" name="username" value ="<?php echo $GLOBALS['name'];?>" placeholder="Enter your user name" maxlength="20" />
</label>

用户名:

全局变量在运行时声明并加载到内存中。这意味着要使全局
$name
存在于任何作用域中,必须先解析
file1.php


正如评论者指出的,您的解决方案是在运行
file2.php
之前包含
file1.php

全局变量在运行时声明并加载到内存中。这意味着要使全局
$name
存在于任何作用域中,必须先解析
file1.php


正如评论者指出的,您的解决方案是在运行
file2.php
之前包含
file1.php

您不需要全局,因为变量
$name
不在任何函数中。即使它位于函数内部,也将此
$name
作为参数传递给它

file1.php

<?php
$name = "PHP rocks";
<?php
include_once('file1.php'); //<---- Just include the above file inside this
?>
<label>
    <span>Username:</span>
    <input id="name" type="text" name="username" value ="<?php echo $name;?>" placeholder="Enter your user name" maxlength="20" />
</label>

用户名:

您不需要全局,因为变量
$name
不在任何函数中。即使它位于函数内部,也将此
$name
作为参数传递给它

file1.php

<?php
$name = "PHP rocks";
<?php
include_once('file1.php'); //<---- Just include the above file inside this
?>
<label>
    <span>Username:</span>
    <input id="name" type="text" name="username" value ="<?php echo $name;?>" placeholder="Enter your user name" maxlength="20" />
</label>

用户名:

您可以将file1包含在file2^-«您在20分钟前刚刚问过这个问题。(完全重复)避免使用
$GLOBALS
@Fred ii-这个问题与那个问题不同。这与全局变量无关。我试图解析一个值,但在这里我试图从不同的文件中读取它!您可以将file1包含在file2^-«您在20分钟前刚刚问过这个问题。(完全重复)避免使用
$GLOBALS
@Fred ii-这个问题与那个问题不同。这与全局变量无关。我试图解析一个值,但在这里我试图从不同的文件中读取它!投票被否决的原因,我可以知道你为什么爱环球吗?投票被否决的原因,我可以知道你为什么爱环球吗?