Oihana PHP System

setIniIfExists.php

Table of Contents

Functions

setIniIfExists()  : bool
Set a PHP ini directive from a scalar or from a config array if the value exists and is not empty.

Functions

setIniIfExists()

Set a PHP ini directive from a scalar or from a config array if the value exists and is not empty.

setIniIfExists(string $key[, array<string|int, mixed>|string|int|float|bool|null $init = [] ]) : bool

Behavior:

  • If $init is an array, this function looks up $init[$key]; otherwise it treats $init as the value.
  • The value is cast to string and trimmed; empty strings are ignored.
  • ini_set() is invoked only if the function exists and a non-empty value is provided.
  • Returns true when ini_set() is called, false otherwise.

Notes:

  • Most ini values are strings; booleans are commonly expressed as "1"/"0". Passing true/false will be cast to "1"/"" by PHP.
  • Use ini_get($key) to read back the effective value after setting.
Parameters
$key : string

The ini key (e.g. 'display_errors', 'memory_limit').

$init : array<string|int, mixed>|string|int|float|bool|null = []

A config array (looked up by $key) or a direct scalar value.

Tags
throws
ConstantException
see
ini_get()
see
ini_set()
link

PHP manual: ini_set

example
use function oihana\init\setIniIfExists;

// From a configuration array
$config =
[
    'display_errors' => '1',
    'memory_limit'   => '256M',
];
setIniIfExists('display_errors', $config); // calls ini_set('display_errors', '1')
setIniIfExists('upload_max_filesize', $config); // returns false (not present)

// From a direct scalar
setIniIfExists('memory_limit', '512M'); // calls ini_set('memory_limit', '512M')
setIniIfExists('display_errors', "\t\n"); // returns false (empty after trim)
Return values
bool

True if ini_set() was called, false otherwise.


        
On this page

Search results