难以传递变量包括使用全局变量

发布于 2024-12-25 00:46:41 字数 576 浏览 1 评论 0原文

我有一个三层树,用于在页面上显示内容。它使用 include 根据 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'] == 'white')      
       {$color = 'white';}
else 
       {$color = 'blue';}

global $color;
global $user;
include 'page2.php';

page2.php

global $color;
global $user;
echo 'hi '.$user.'I hear you like '.$color;

I have a three tier tree for displaying content on a page. It uses includes to display specific PHP pages based on the URL. What doesn't happen is the variables are not understood in included PHP files.

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';

page2.php

global $color;
global $user;
echo 'hi '.$user.'I hear you like '.$color;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

故人如初 2025-01-01 00:46:41

根本不需要这些 $global 行。主脚本中定义的任何变量都在 included 文件中定义。这基本上就像获取 included 文件中的代码并将其推入 include 调用的位置(有一些例外)

There is no need at all for those $global lines. Any variables defined in the main script are defined in the included file. It's basically like taking the code in the included file and shoving it in the place of the include call (with a few exceptions)

追我者格杀勿论 2025-01-01 00:46:41

此行:

include_once 'page2.php;

应更改为:

include_once 'page2.php';

您缺少报价。

This line:

include_once 'page2.php;

should change to:

include_once 'page2.php';

You have a missing quote.

π浅易 2025-01-01 00:46:41

您是否尝试过删除所有这四条全局线?我不知道这是否是问题所在,但它们根本没有必要!

当包含或需要文件时,上面声明的所有变量都可用于包含/需要的文件。

如果这没有解决问题,也许你的包含路径错误。

Have you tried removing ALL those four global lines? I don't know if that's the problem, but they're not necessary at all!

When including or requiring a file all the variables declared above are available to the included/required file.

If that didn't solve it, maybe you have the wrong path on include.

无所谓啦 2025-01-01 00:46:41

index.php

<?php
    $user = $_GET['user'];

    if ($_GET['color'] == 'red')        
           {$color = 'red';}
    elseif ($_GET['color'] == 'white')      
           {$color = 'white';}
    else 
           {$color = 'blue';}
    include 'page2.php';
?>

page2.php

<?php
    echo 'hi '.$user.'I hear you like '.$color;
?>

全局示例

function dosomethingfunky(){
    global $user, $color;
    echo 'hi '.$user.'I hear you like '.$color;
}

index.php

<?php
    $user = $_GET['user'];

    if ($_GET['color'] == 'red')        
           {$color = 'red';}
    elseif ($_GET['color'] == 'white')      
           {$color = 'white';}
    else 
           {$color = 'blue';}
    include 'page2.php';
?>

page2.php

<?php
    echo 'hi '.$user.'I hear you like '.$color;
?>

global example

function dosomethingfunky(){
    global $user, $color;
    echo 'hi '.$user.'I hear you like '.$color;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文