Php 传递变量有困难包括使用全局变量
我有一个三层树,用于在页面上显示内容。它使用includes根据URL显示特定的PHP页面。但没有发生的是,包含的PHP文件中没有理解这些变量Php 传递变量有困难包括使用全局变量,php,include,global,Php,Include,Global,我有一个三层树,用于在页面上显示内容。它使用includes根据URL显示特定的PHP页面。但没有发生的是,包含的PHP文件中没有理解这些变量 index.php // example url http://fakesite.com/?color=red&user=999 $user = $_GET['user']; if ($_GET['color'] == 'red') {$color = 'red';} elseif ($_GET['color']
index.php
// example url http://fakesite.com/?color=red&user=999
$user = $_GET['user'];
if ($_GET['color'] == 'red')
{$color = 'red';}
elseif ($_GET['color'] == 'white')
{$color = 'white';}
else
{$color = 'blue';}
global $color;
global $user;
include 'page2.php';
<?php
$user = $_GET['user'];
if ($_GET['color'] == 'red')
{$color = 'red';}
elseif ($_GET['color'] == 'white')
{$color = 'white';}
else
{$color = 'blue';}
include 'page2.php';
?>
<?php
echo 'hi '.$user.'I hear you like '.$color;
?>
page2.php
global $color;
global $user;
echo 'hi '.$user.'I hear you like '.$color;
根本不需要那些
$global
行。主脚本中定义的任何变量都在include
d文件中定义。这基本上就像在include
d文件中获取代码,然后将其推到include
调用的位置(除了少数例外)根本不需要那些$global
行。主脚本中定义的任何变量都在include
d文件中定义。这基本上就像在include
d文件中获取代码,然后将其推到include
调用的位置(除了少数例外)这一行:
include_once 'page2.php;
应改为:
include_once 'page2.php';
您缺少报价。此行:
include_once 'page2.php;
应改为:
include_once 'page2.php';
您缺少一个引号。您是否尝试删除所有这四条全局行?我不知道这是否是问题所在,但根本没有必要 当包含或需要一个文件时,上面声明的所有变量都可用于包含/需要的文件
如果这不能解决问题,那么可能是您在include上的路径错误。您是否尝试过删除所有这四条全局行?我不知道这是否是问题所在,但根本没有必要 当包含或需要一个文件时,上面声明的所有变量都可用于包含/需要的文件
如果这不能解决问题,那么可能您在include上的路径错误。index.php
// example url http://fakesite.com/?color=red&user=999
$user = $_GET['user'];
if ($_GET['color'] == 'red')
{$color = 'red';}
elseif ($_GET['color'] == 'white')
{$color = 'white';}
else
{$color = 'blue';}
global $color;
global $user;
include 'page2.php';
<?php
$user = $_GET['user'];
if ($_GET['color'] == 'red')
{$color = 'red';}
elseif ($_GET['color'] == 'white')
{$color = 'white';}
else
{$color = 'blue';}
include 'page2.php';
?>
<?php
echo 'hi '.$user.'I hear you like '.$color;
?>
index.php
// example url http://fakesite.com/?color=red&user=999
$user = $_GET['user'];
if ($_GET['color'] == 'red')
{$color = 'red';}
elseif ($_GET['color'] == 'white')
{$color = 'white';}
else
{$color = 'blue';}
global $color;
global $user;
include 'page2.php';
<?php
$user = $_GET['user'];
if ($_GET['color'] == 'red')
{$color = 'red';}
elseif ($_GET['color'] == 'white')
{$color = 'white';}
else
{$color = 'blue';}
include 'page2.php';
?>
<?php
echo 'hi '.$user.'I hear you like '.$color;
?>
我注意到您使用了
include\u once()
。您之前是否在代码中的其他地方包含了page2.php
?@Michael No和我更改了它以避免更多混淆。我注意到您使用了include_once()
。您是否碰巧在代码前面的其他地方包含了page2.php
?@Michael No和我更改了它以避免更多的混淆。仍然没有任何内容通过。仍然没有任何内容通过。include有效。page2无法识别索引中的变量。include有效。page2无法识别索引中的变量。