- ini_get
- Parameters
- Return Values
- Examples
- Notes
- See Also
- User Contributed Notes 12 notes
- 3 ways to get PHP memory limit value
- Quick solutions
- What is PHP memory limit and why is it necessary?
- How to get PHP memory limit configuration value
- 1. Using PHP-CLI (PHP Command Line Interface)
- 2. Using phpinfo function
- Editing PHP memory limit setting
- You might also like
- References
ini_get
Returns the value of the configuration option on success.
Parameters
The configuration option name.
Return Values
Returns the value of the configuration option as a string on success, or an empty string for null values. Returns false if the configuration option doesn’t exist.
Examples
Example #1 A few ini_get() examples
/*
Our php.ini contains the following settings:
?php
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
case ‘g’ :
$val *= 1024 ;
case ‘m’ :
$val *= 1024 ;
case ‘k’ :
$val *= 1024 ;
>
The above example will output something similar to:
display_errors = 1 register_globals = 0 post_max_size = 8M post_max_size+1 = 9 post_max_size in bytes = 8388608
Notes
Note: When querying boolean values
A boolean ini value of off will be returned as an empty string or «0» while a boolean ini value of on will be returned as «1». The function can also return the literal string of INI value.
Note: When querying memory size values
Many ini memory size values, such as upload_max_filesize, are stored in the php.ini file in shorthand notation. ini_get() will return the exact string stored in the php.ini file and NOT its int equivalent. Attempting normal arithmetic functions on these values will not have otherwise expected results. The example above shows one way to convert shorthand notation into bytes, much like how the PHP source does it.
Note:
ini_get() can’t read «array» ini options such as pdo.dsn.*, and returns false in this case.
See Also
- get_cfg_var() — Gets the value of a PHP configuration option
- ini_get_all() — Gets all configuration options
- ini_restore() — Restores the value of a configuration option
- ini_set() — Sets the value of a configuration option
User Contributed Notes 12 notes
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
function return_bytes ( $size_str )
switch ( substr ( $size_str , — 1 ))
case ‘M’ : case ‘m’ : return (int) $size_str * 1048576 ;
case ‘K’ : case ‘k’ : return (int) $size_str * 1024 ;
case ‘G’ : case ‘g’ : return (int) $size_str * 1073741824 ;
default: return $size_str ;
>
>
?>
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.
Here is another way to get the result in bytes using PHP8
/**
* @param string $size
* @return int
* @author DevsrealmGuy
*/
public function getBytes ( string $size ): int
$size = trim ( $size );
#
# Separate the value from the metric(i.e MB, GB, KB)
#
preg_match ( ‘/(6+)[\s]*([a-zA-Z]+)/’ , $size , $matches );
$value = (isset( $matches [ 1 ])) ? $matches [ 1 ] : 0 ;
$metric = (isset( $matches [ 2 ])) ? strtolower ( $matches [ 2 ]) : ‘b’ ;
#
# Result of $value multiplied by the matched case
# Note: (1024 ** 2) is same as (1024 * 1024) or pow(1024, 2)
#
$value *= match ( $metric ) ‘k’ , ‘kb’ => 1024 ,
‘m’ , ‘mb’ => ( 1024 ** 2 ),
‘g’ , ‘gb’ => ( 1024 ** 3 ),
‘t’ , ‘tb’ => ( 1024 ** 4 ),
default => 0
>;
#
# TEST: This default to 0 if it doesn’t conform with the match standard
#
echo getBytes ( ‘2GB’ ) . «» ;
# OUTPUT: 2147483648
echo getBytes ( ‘4tb’ ) . «» ;
# OUTPUT: 4398046511104
echo getBytes ( ‘5345etrgrfd’ ) . «» ;
# OUTPUT: 0
echo getBytes ( ‘357568336586’ ) . «» ;
# OUTPUT: 0
?>
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.
This version of return_bytes takes care of the MB, GB, KB cases along with the M,G,K ones.
Hope this is helpful!
public static function return_bytes ( $val )
if(empty( $val ))return 0 ;
preg_match ( ‘#(8+)[\s]*([a-z]+)#i’ , $val , $matches );
$last = » ;
if(isset( $matches [ 2 ])) $last = $matches [ 2 ];
>
if(isset( $matches [ 1 ])) $val = (int) $matches [ 1 ];
>
switch ( strtolower ( $last ))
case ‘g’ :
case ‘gb’ :
$val *= 1024 ;
case ‘m’ :
case ‘mb’ :
$val *= 1024 ;
case ‘k’ :
case ‘kb’ :
$val *= 1024 ;
>
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.
Yet another implementation of return_bytes:
function return_bytes ( $val )
assert ( ‘1 === preg_match(«/^\d+([kmg])?$/i», $val)’ );
static $map = array ( ‘k’ => 1024 , ‘m’ => 1048576 , ‘g’ => 1073741824 );
return (int) $val * @( $map [ strtolower ( substr ( $val , — 1 ))] ?: 1 );
>
?>
If you’re using PHP >= 7, you might replace ?: with ?? to avoid the use of the @ silencer.
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.
Here is how to accurately test for boolean php.ini values:
function ini_get_bool ( $a )
$b = ini_get ( $a );
switch ( strtolower ( $b ))
case ‘on’ :
case ‘yes’ :
case ‘true’ :
return ‘assert.active’ !== $a ;
case ‘stdout’ :
case ‘stderr’ :
return ‘display_errors’ === $a ;
Here is a version combining a few of the examples here that does *not* require php8 nor does it generate a warning
/**
* gets the value in bytes converted from a human readable string like 10G’
*
* @param mixed $val the human readable/shorthand version of the value
* @return int the value converted to bytes
*/
function return_bytes($val) $val = trim($val);
preg_match(‘/(1+)[\s]*([a-zA-Z]+)/’, $val, $matches);
$value = (isset($matches[1])) ? intval($matches[1]) : 0;
$metric = (isset($matches[2])) ? strtolower($matches[2]) : ‘b’;
switch ($metric) case ‘tb’:
case ‘t’:
$value *= 1024;
case ‘gb’:
case ‘g’:
$value *= 1024;
case ‘mb’:
case ‘m’:
$value *= 1024;
case ‘kb’:
case ‘k’:
$value *= 1024;
>
return $value;
>
In a similar vein, converting flags to booleans proper:
function return_boolean ( $val )
static $map = array ( ‘on’ => true , ‘true’ => true , ‘off’ => false , ‘false’ => false );
return @( $map [ strtolower ( $val )] ?: (bool) $val );
>
?>
If you’re using PHP >= 7, consider replacing ?: with ?? and removing the @ silencer.
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’.
- PHP Options/Info Functions
- assert_options
- assert
- cli_get_process_title
- cli_set_process_title
- dl
- extension_loaded
- gc_collect_cycles
- gc_disable
- gc_enable
- gc_enabled
- gc_mem_caches
- gc_status
- get_cfg_var
- get_current_user
- get_defined_constants
- get_extension_funcs
- get_include_path
- get_included_files
- get_loaded_extensions
- get_required_files
- get_resources
- getenv
- getlastmod
- getmygid
- getmyinode
- getmypid
- getmyuid
- getopt
- getrusage
- ini_alter
- ini_get_all
- ini_get
- ini_parse_quantity
- ini_restore
- ini_set
- memory_get_peak_usage
- memory_get_usage
- memory_reset_peak_usage
- php_ini_loaded_file
- php_ini_scanned_files
- php_sapi_name
- php_uname
- phpcredits
- phpinfo
- phpversion
- putenv
- set_include_path
- set_time_limit
- sys_get_temp_dir
- version_compare
- zend_thread_id
- zend_version
- get_magic_quotes_gpc
- get_magic_quotes_runtime
- restore_include_path
3 ways to get PHP memory limit value
Knowing what PHP memory limit value is will help in configuring and optimize for your PHP scripts to run efficiently. This article will guide you on how to quickly get PHP memory limit configuration value, and more in-depth guides on how the methods work.
Quick solutions
php -r "echo ini_get('memory_limit');"
In this case, the PHP memory limit value is 512 MB. 2. By phpinfo : Create a new PHP file with this line:
Save the file, open it on the browser, and look for memory_limit , which will show the current setting value. Above are quick solutions for getting the setting value of PHP memory limit. To know more details on PHP memory limit, and the processes to get its value, you can continue with the article within the below sections.
What is PHP memory limit and why is it necessary?
A PHP script needs memory assigned to it for it to be running properly. As in PHP.net, it is the maximum amount of memory in bytes that a script is allowed to allocate. This helps poorly written PHP scripts won’t consume too much server memory, as well as malicious PHP scripts that always eat up the server bandwidth. Too low memory limit setting value can cause issues on the PHP scripts needing fair memory allocated for it to run properly. In general, 256MB ( 256M in PHP memory_limit syntax) is more than enough for most PHP scripts to run properly.
How to get PHP memory limit configuration value
There are multiple ways to get PHP memory limit value, here we will introduce the most common methods along with explanation on how they work.
1. Using PHP-CLI (PHP Command Line Interface)
1.A. Using SSH terminal (in Linux based operating systems) or Windows command line, we can use this command to get the PHP memory limit value and print it to the terminal:
php -r "echo ini_get('memory_limit');"
- php is the required command to execute a PHP command in terminal
- -r is required for running commands without the script tags ( ), as defined in PHP.net Command line options
- echo ini_get(‘memory_limit’); (whole command in bracket) is the command to print ( echo ) the value of ini_get(‘memory_limit’); , in which ini_get is the function to get PHP configuration value.
The result will simply display the configuration value, here is 512M, which is 512 MB in PHP configuration syntax:
1.B. Using SSH terminal in Linux based operating systems only, type this command:
- -i is for getting PHP configuration values
- grep command for only getting the memory_limit value, not every PHP configuration values.
[[email protected] ~]# php -i | grep "memory_limit" memory_limit => 512M => 512M
This will show two values for PHP memory limit configuration, which the first one is the local value set in the current web directory , the second one is the master value defined in php.ini (system-wide) .
2. Using phpinfo function
We can create a php file with the phpinfo() function to get the memory limit setting:
Open the file on browser, and look for memory_limit value will show the setting we need:
Editing PHP memory limit setting
When you can get PHP memory limit value, the next step is adjust it properly.
We have a detailed article on how to edit PHP memory limit configuration values:
You might also like
References