note 115469 added to function.ini-get
| From: | espertalhao04 at hotmail dot com | Date: | Wed, 30 Jul 2014 10:01:04 +0000 |
| Subject: | note 115469 added to function.ini-get | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-200070@lists.php.net to get a copy of this message | ||
I was needing a small function to return the memory limit in a system, in whatever value was in
php.ini
I made this tiny function (can be reduced) which works well (at least on 64bit systems, may not work
on 32bit systems for GB values).
<?php
function memory_limit()
{
if(!function_exists('ini_get')||($m=@ini_get('memory_limit'))===null)return
null;
$x=array(''=>1,'k'=>1024,'m'=>1048576,'g'=>1073741824);
return($n=$m/1)*$x[strtolower(str_replace($n,'',$m))];
}
?>
Readable and commented version:
<?php
function memory_limit()
{
//verifies if the function ini_get exists and is enabled
//(it's impossible to avoid using the error suppression operator here)
if( !function_exists('ini_get') || ($memory = @ini_get('memory_limit')) ===
null )
return null;
//this will be used to multiply by the final value, to obtain the size in bytes
$units=array(
//this is used when there is no unit
//when there is no unit, php interprets as if the value
// was already a value in bytes
''=>1,
'k'=>1024,//kbytes
'm'=>1048576,//mbytes = 1024 * 1024
'g'=>1073741824 //gbytes = 1024 * 1024 * 1024
);
$amount = $memory/1;//convert the value to a number
//removes the memory amount, leaving behind only the unit if present
$unit = strtolower(str_replace($amount,'',$memory));
//makes the calculation
return $amount*$units[$unit];
}
?>
This might not be the fastest, but surely its the simplest way.
----
Server IP: 72.52.91.14
Probable Submitter: 93.108.96.50
----
Manual Page -- http://php.net/manual/en/function.ini-get.php
Edit -- https://master.php.net/note/edit/115469
Del: integrated -- https://master.php.net/note/delete/115469/integrated
Del: useless -- https://master.php.net/note/delete/115469/useless
Del: bad code -- https://master.php.net/note/delete/115469/bad+code
Del: spam -- https://master.php.net/note/delete/115469/spam
Del: non-english -- https://master.php.net/note/delete/115469/non-english
Del: in docs -- https://master.php.net/note/delete/115469/in+docs
Del: other reasons-- https://master.php.net/note/delete/115469
Reject -- https://master.php.net/note/reject/115469
Search -- https://master.php.net/manage/user-notes.php