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

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

May 31, 2016 pm 01:16 PM
ini_get php php.ini variable Obtain

本文实例讲述了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程序设计有所帮助。

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP Working with Database CakePHP Working with Database Sep 10, 2024 pm 05:25 PM

Working with database in CakePHP is very easy. We will understand the CRUD (Create, Read, Update, Delete) operations in this chapter.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

In this chapter, we are going to learn the following topics related to routing ?

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

See all articles