php里面如何跨文件访问变量?

比如我的a.php的内容为:
<?php
$DbServer = '192.168.1.106:1265';
$User = 'wx';
$Password = 'wx123456';
?>
然后在我的b.php里面有一个方法为:
function CreateConn()
{
$con = mysql_connect($DbServer, $User, $Password);
if (!$con)
die('Could not connect: ' . mysql_error());

mysql_select_db("wenxibiz_wxstock", $con);
return $con;
}

会报错说:
Notice: Undefined variable: DbServer in C:\xampp\htdocs\wx\Utilities\GenConn.php on line 6

Notice: Undefined variable: User in C:\xampp\htdocs\wx\Utilities\GenConn.php on line 6

Notice: Undefined variable: Password in C:\xampp\htdocs\wx\Utilities\GenConn.php on line 6
请问这个是怎么回事,该如何解决呢?

神不在的星期二
浏览 1587回答 2
2回答

守候你守候我

你这个是涉及到全局变量问题,有几种方法首先,肯定要引入a文件include&nbsp;'a.php'&nbsp;一、global12345678$str&nbsp;=&nbsp;'hello';function&nbsp;test(){&nbsp;&nbsp;&nbsp;&nbsp;global&nbsp;$str;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;$str;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;$GLOBAL['str'];//这两个效果一样}test();//输出&nbsp;hello二、define定义全局变量123456define('STR','hello');function&nbsp;test(){&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;STR;}test();//输出hello&nbsp;注意,这个STR前面不要$&nbsp;
打开App,查看更多内容
随时随地看视频慕课网APP