求教如何使用include文件中的变量,在不定义全局变量的情况下
情况是这样的。
我在1.php里include(2.php);
在2.php里定义个doInit()函数:
function doInit() {
$UserID=$_SESSION['UserID'];
$UserName=$_SESSION['UserNmae'];
}
我在1.php里调用doInit()函数后,如何才能使用$UserID变量。
不要定义全局变量啊。
因为很多页面都需要doInit()里的内容,所以我打算采用include的方式写在一个页面里。只要能实现这个功能的办法就可以。
------解决方案--------------------
如果不是函数返回,不行!
------解决方案--------------------
function doInit() {
$UserID=$_SESSION['UserID'];
$UserName=$_SESSION['UserNmae'];
return array($userId, $UserName);
}
$ar = doInit();
echo $ar[0];
echo $ar[1]; <div class="clear"></div>
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号