Home > Backend Development > PHP Tutorial > php用ini_get获取php.ini里变量值的方法_PHP

php用ini_get获取php.ini里变量值的方法_PHP

WBOY
Release: 2016-05-31 13:16:31
Original
889 people have browsed it

本文实例讲述了php用ini_get获取php.ini里变量值的方法。分享给大家供大家参考。具体分析如下:

要得到php.ini里的变量值,当然,你可以用phpinfo();来得到所有php配置信息,但如果要想得到某个变量值的话,你又要怎样获取呢?

php里提供一个获取php.ini里的变量值的函数:ini_get()

ini_get()的用法非常简单,下面通过实例说明它是如何使用的。

语法:

string ini_get ( string varname )
Copy after login

返回值如果为布尔型则为0或1

实例:

<&#63;php
/*
Our php.ini contains the following settings:
display_errors = On
register_globals = Off
post_max_size = 8M
*/
echo 'display_errors = ' . ini_get('display_errors') . "\n";
echo 'register_globals = ' . ini_get('register_globals') . "\n";
echo 'post_max_size = ' . ini_get('post_max_size') . "\n";
echo 'post_max_size+1 = ' . (ini_get('post_max_size')+1) . "\n";
echo 'post_max_size in bytes = ' . return_bytes(ini_get('post_max_size'));
function return_bytes($val) {
  $val = trim($val);
  $last = strtolower($val[strlen($val)-1]);
  switch($last) {
    // The 'G' modifier is available since PHP 5.1.0
    case 'g':
      $val *= 1024;
    case 'm':
      $val *= 1024;
    case 'k':
      $val *= 1024;
  }
  return $val;
}
&#63;>
Copy after login

上述代码的运行结果类似如下:

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

如果想获取整个php.ini里的变量值,我们可以用ini_get的加强函数 ini_get_all()。

ini_get_all()函数以数组的形式返回整个php的环境变量,用法也很简单。

实例一:

<&#63;php
print_r(ini_get_all("pcre"));
print_r(ini_get_all());
&#63;>
Copy after login

上述代码的运行结果类似如下:

Array
(
  [pcre.backtrack_limit] => Array
    (
      [global_value] => 100000
      [local_value] => 100000
      [access] => 7
    )
  [pcre.recursion_limit] => Array
    (
      [global_value] => 100000
      [local_value] => 100000
      [access] => 7
    )
)
Array
(
  [allow_call_time_pass_reference] => Array
    (
      [global_value] => 0
      [local_value] => 0
      [access] => 6
    )
  [allow_url_fopen] => Array
    (
      [global_value] => 1
      [local_value] => 1
      [access] => 4
    )
  ...
)
Copy after login

实例二:

<&#63;php
print_r(ini_get_all("pcre", false)); // Added in PHP 5.3.0
print_r(ini_get_all(null, false)); // Added in PHP 5.3.0
&#63;>
Copy after login

输出结果类似如下:

Array
(
  [pcre.backtrack_limit] => 100000
  [pcre.recursion_limit] => 100000
)
Array
(
  [allow_call_time_pass_reference] => 0
  [allow_url_fopen] => 1
  ...
)
Copy after login

与ini_get()相对的函数是ini_set(),ini_set具有更改php.ini设置的功能。例如当某脚本运行超时时,可以设置其最大执行时间。

希望本文所述对大家的php程序设计有所帮助。

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template