Page 1 of 1

PHP7 Notice: "A non well formed numeric value encountered"

Posted: Sun Apr 11, 2021 12:18 am
by peter_b
[PROBLEM]
I'm checking how much memory PHP has free to use, before further execution:

Code: Select all

ini_get('memory limit')
usually returns a number with a size modifier character, like: "128M"

Before PHP7 (7.3?), php would just use that string as-is, ignore the non-numeric part, when used in a calculation.
This seems to have changed and now I get the following warning:
PHP Notice: A non well formed numeric value encountered in /opt/dva-profession/devel/trunk/bin/include/ArchiveStructure.php on line 2372

[SOLUTION]
There's an example for converting a php.ini value like this to a number in PHP docs for "ini_get()", but It seems to suffer from the same new PHP7 stricter-ness :D

So I've changed the first lines to this:

Code: Select all

  $val = trim(strtolower($val));
  $last = $val[strlen($val)-1];
  $val = rtrim($val, "gmk");
I lowercase the ini value first, then extract the last character - and then remove any of the letters G, M or K (in lowercase) and store the number-only in "$val".

Now it works without warnings:

Code: Select all

   /**
     * Converts a PHP config value data size with a modifier character to a
     * numeric byte value.
     *
     * Code is an exact copy from:
     * https://www.php.net/manual/en/function.ini-get.php
     */
    public static function return_bytes($val) {
        $val = trim(strtolower($val));
        $last = $val[strlen($val)-1];
        $val = rtrim($val, "gmk");

        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;
    }