duanhuan1147 2013-07-28 12:50
浏览 66

php全局变量在包含的文件中不起作用

I've searched for a day to find a solution for this,But no luck yet, I have a function called : online() this function is inside a file called client.php which is located in root/dir/includes/client.php:

In client.php:

// db connection
include "base.php";

// check if availabe
$available = "false";
$check = mysql_query("SELECT available FROM users ");
while ($row=mysql_fetch_array($check)) {
    if($row['available'] == "yes") {
        $available = "true";
    }
}
// get config
$fetch = mysql_query("SELECT * FROM config ");
$config = mysql_fetch_array($fetch);

// functions
function online() {
// globals
global $available,$config,$path;

// build box

    if($available == "true") {
        ?>
        <div id="online">

        <?
    } else {
            ?><div id="offline"> <?
        }
echo 'client.php is included';
}
}

?>

About client.php:

first it establishes a database connection, then checks if a user is available, if yes : $available = "true"; else: $available = "false";

Then I include client.php in index.php (located in root), So i Have:

in index.php:

$path = "dir/";
include $path . "includes/client.php";

So far so good, everything works,

The problem is...

I need to use this function in other pages in sub directories too, to be more specific, I'm trying to add this function to my wordpress website which is located in: root/wp in my wordpress header I include client.php :

include "../dir/includes/client.php";

And I get the output , So i'm sure it is included, But, none of my global variables work when I open my wordpress (root/wp) resulting in $available = null while it is expected to be "false" as it is defined in client.php

The confusing thing is that, when I echo $available inside my wordpress header I can get the value, but when I echo it inside client.php it is null again, so if I echo both in wordpress header and client.php , when I open my wordpress page, I can see the one I included in header, and the other one inside client.php is null.

Any help would be much appreciated.

  • 写回答

2条回答 默认 最新

  • doufan9377 2013-07-28 13:07
    关注

    It may be an issue with a variable scope. Try to use

    $GLOBALS['available']
    

    instead of

    $available
    

    in your client.php. So it will be defined in global scope. At the definition section also:

    $GLOBALS['available'] = "false"; and $GLOBALS['available'] = "true";
    
    评论

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)