略微加速

PHP官方手册 - 互联网笔记

PHP - Manual: get_cfg_var

2024-12-21

get_cfg_var

(PHP 4, PHP 5, PHP 7, PHP 8)

get_cfg_var获取 PHP 配置选项的值

说明

get_cfg_var(string $option): mixed

获取 PHP 配置选项 option 的值。

此函数不会返回 PHP 编译的配置信息,或从 Apache 配置文件读取。

检查系统是否使用了一个配置文件,并尝试获取 cfg_file_path 的配置设置的值。 如果有效,将会使用一个配置文件。

参数

option

配置选项的名称。

返回值

返回 option 指定的当前 PHP 配置变量的值,错误发生时返回 false

更新日志

版本 说明
5.3.0 get_cfg_var() 被修复,能够返回 "array" 的 ini 选项。

参见

add a noteadd a note

User Contributed Notes 6 notes

up
15
surfchen at gmail dot com
15 years ago
get_cfg_var returns the value from php.ini directly,while the ini_get returns   the runtime config value. I have tried it on PHP 5.1.6

[EDIT by danbrown AT php DOT net: The author of this note means that ini_get() will return values set by ini_set(), .htaccess, a local php.ini file, and other functions at runtime.  Conversely, get_cfg_var() will return strictly the server php.ini.]
up
8
gabriel b
9 years ago
settings with the value of 'yes' will be returned as '1'.

<?php
//#my ini file
//A = 1
//B = any-thing
//C = yes
//D = /some/path/file

get_cfg_var('A') // returns '1'
get_cfg_var('B') // returns 'any-thing'
get_cfg_var('C') // returns '1', wait, why?
get_cfg_var('D') // returns '/some/path/file'
?>

I had my setting = yes and then checked it as ==="yes" for epic fail.
up
4
tbrix13 at uzitech dot com
7 years ago
keep in mind get_cfg_var() returns a string(1) '1' for the value: On

<?php
//in php.ini
//A = On

$A1 = get_cfg_var("A") === "On";
$A2 = get_cfg_var("A") === 1;
$A3 = get_cfg_var("A") === "1";

//$A1 is false
//$A2 is false
//$A3 is true
?>
up
2
techno dot rahul1988 at gmail dot com
8 years ago
The difference between ini_get() and get_cfg_var() is as follows:
@) ini_get(): returns the current value in .htaccess or as defined in PHP_INI_USER or PHP_INI_PERDIR
@) get_cfg_var: returns the values defined in the php.ini
up
0
sinus at sinpi dot net
2 years ago
Boolean-like values are evaluated as follows: "true", "on", "yes" evaluate to "1" (string 1), while "false", "off", "no" evaluate to "" (empty string).
up
-8
Stephen
15 years ago
Regarding the statement by the earlier poster that:

"Unfortunately, you almost never want to know the original value in the config file. Instead, you want to know the value currently in effect."

I have found this useful for changing the error reporting levels for a few specific pages while testing. I turn on all error_reporting while testing, but for a few pages I want to turn off notices. So, I put this at the top of the page:

<?php
error_reporting
(8183);
?>

and this at the bottom:

<?php
error_reporting
(get_cfg_var('error_reporting'));
?>

to put it back to whatever default I had at the time.

官方地址:https://www.php.net/manual/en/function.get-cfg-var.php

北京半月雨文化科技有限公司.版权所有 京ICP备12026184号-3