百科狗-知识改变命运!
--

ini_get() - php 选项信息函数

乐乐1年前 (2023-11-21)阅读数 21#技术干货
文章标签选项

ini_get()

(PHP 4, PHP 5, PHP 7)

获取一个配置选项的值

说明

ini_get(string $varname): string

成功时返回配置选项的值。

参数

$varname

配置选项名称。

返回值

成功是返回配置选项值的字符串,null的值则返回空字符串。如果配置选项不存在,将会返回FALSE

范例

一些ini_get()例子

以上例程的输出类似于:

display_errors = 1
register_globals = 0
post_max_size = 8M
post_max_size+1 = 9
post_max_size in bytes = 8388608

注释

Note:当查询一个 boolean 值

一个off的 boolean ini 值将会以空字符串或者"0"返回;on的 ini 值会以"1"返回。此函数也会返回 INI 值的文字字符串。Note:当查询一个内存尺寸的值

许多内存尺寸的 ini 值,类似upload_max_filesize是以简写表示法储存在php.ini文件里。ini_get()会返回php.ini文件中储存的确切字符串,而不是它的等量integer。尝试对这些值使用常规算术运算函数将不会得到预期的结果。以上例子显示了转换简写表示法为字节的一种方式,和 PHP 源码所做的比较像。Note:

ini_get() - php 选项信息函数

ini_get()无法读取"array"的 ini 选项,例如 pdo.dsn.*,在这个例子中会返回FALSE

更新日志

版本说明
5.3.0当配置项不存在,之前会返回空字符串,现在会返回FALSE

参见

  • get_cfg_var() 获取 PHP 配置选项的值
  • ini_get_all() 获取所有配置选项
  • ini_restore() 恢复配置选项的值
  • ini_set() 为一个配置选项设置值
another version of return_bytes which returns faster and does not use multiple multiplications (sorry:). even if it is resolved at compile time it is not a good practice;
no local variables are allocated;
the trim() is omitted (php already trimmed values when reading php.ini file);
strtolower() is replaced by second case which wins us one more function call for the price of doubling the number of cases to process (may slower the worst-case scenario when ariving to default: takes six comparisons instead of three comparisons and a function call);
cases are ordered by most frequent goes first (uppercase M-values being the default sizes);
specs say we must handle integer sizes so float values are converted to integers and 0.8G becomes 0;
'Gb', 'Mb', 'Kb' shorthand byte options are not implemented since are not in specs, see
http://www.php.net/manual/en/faq.using.php#faq.using.shorthandbytes 
Be aware that max_execution_time can be altered by XDebug.
While debugging a script locally that made use of  it returned 0 when XDebug remote debugging was enabled and the IDE was listening to it.
It makes sense, since debugging manually takes time so we don't want the script to time out ; but in that particular case, it made it look to the script like max_execution_time was 0, so calculations were wrong.
You can see in phpinfo() that local value is 0 in that case, but master value is the correct one you set in your php.ini.
The above example function called return_bytes() assumes that ini_get('upload_max_filesize') delivers only one letter at the end. As I've seen 'Mb' and things like that, I'd suggest to change the $last = ... part into $last = strtolower(substr($val,strlen($val/1),1)).
I'd call it $unit then.
Concerning the value retourned, it depends on how you set it.
I had the problem with horde-3 which test the safe_mode value.
THan :
- if you set the value with php_admin_value safe_mode Off (or On) ini_get returns the string
- if you set the value with php_admin_flag safe_mode Off (or On) ini_get returns the boolean.
Yet another implementation of return_bytes:

If you're using PHP >= 7, you might replace ?: with ?? to avoid the use of the @ silencer.
This version of return_bytes takes care of the MB, GB, KB cases along with the M,G,K ones.
Hope this is helpful! 
In a similar vein, converting flags to booleans proper:

If you're using PHP >= 7, consider replacing ?: with ?? and removing the @ silencer.
Here is how to accurately test for boolean php.ini values: 
You can set custom entries in the ini file to provide globals such as database details.
However these must be retrieved with get_cfg_var, ini_get won't work.
It might be useful for included scripts that include other files to extend the 'include_path' variable:

Sometimes, it may also be useful to store the current 'include_path' in a variable, overwrite it, include, and then restore the old 'include_path'.

鹏仔微信 15129739599 鹏仔QQ344225443 鹏仔前端 pjxi.com 共享博客 sharedbk.com

免责声明:我们致力于保护作者版权,注重分享,当前被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理!邮箱:344225443@qq.com)

图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!

内容声明:本文中引用的各种信息及资料(包括但不限于文字、数据、图表及超链接等)均来源于该信息及资料的相关主体(包括但不限于公司、媒体、协会等机构)的官方网站或公开发表的信息。部分内容参考包括:(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供参考使用,不准确地方联系删除处理!本站为非盈利性质站点,本着为中国教育事业出一份力,发布内容不收取任何费用也不接任何广告!)