gpt4 book ai didi

php - 在 PHP 中创建配置文件

转载 作者:IT老高 更新时间:2023-10-28 11:56:07 26 4
gpt4 key购买 nike

我想为我的 PHP 项目创建一个配置文件,但我不确定最好的方法是什么。

到目前为止,我有 3 个想法。

1-使用变量

$config['hostname'] = "localhost";
$config['dbuser'] = "dbuser";
$config['dbpassword'] = "dbpassword";
$config['dbname'] = "dbname";
$config['sitetitle'] = "sitetitle";

2-使用常量

define('DB_NAME', 'test');
define('DB_USER', 'root');
define('DB_PASSWORD', '');
define('DB_HOST', 'localhost');
define('TITLE', 'sitetitle');

3-使用数据库

我将在类里面使用该配置,所以我不确定哪种方式最好,或者是否有更好的方式。

最佳答案

一种简单而优雅的方法是创建一个 config.php 文件(或任何你称之为的文件),它只返回一个数组:

<?php

return array(
'host' => 'localhost',
'username' => 'root',
);

然后:

$configs = include('config.php');

关于php - 在 PHP 中创建配置文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14752470/

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