gpt4 book ai didi

php - 如何使用声明了 require/include 的 php 文件中的变量

转载 作者:行者123 更新时间:2023-11-29 06:00:53 24 4
gpt4 key购买 nike

基本上,我有一个用于连接到 mysql 数据库的 config.php 文件,以及一个包含一些函数的 functions.php 文件。在我的所有文件中(例如索引、登录、注册),我使用 require('config.php'); 行,在 config.php 中我有这一行 require('functions .php'); 在我的索引、登录和 register.php 文件中使用行 require('config.php'); 将配置 + 函数包含在一起。

基本上我的问题是,我在 config.php 中声明的变量在 functions.php 中无法识别。我如何让它发挥作用?

提前致谢。

最佳答案

使用global 语句将函数内的变量声明为全局变量。

function myfunction() {
global $myvar; // $myvar set elsewhere can be read within this function
// and if its value changes in this function, it changes globally
}

关于php - 如何使用声明了 require/include 的 php 文件中的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45089740/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com