0

When I migrated my app on a new server with PHP 7.2, the following message is showing up "Warning: A non-numeric value encountered in…"

The problem comes from this line (in the middle of the following code) $out = $out + strpos($index, substr($in, $t, 1)) * $bcp;.

How to solve it ? Thanks !

public function alphaID($in, $to_num = false, $pad_up = false, $pass_key = null)
{
    $out = '';
    $index = 'abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $base = strlen($index);

    if ($pass_key !== null) {
        // Although this function's purpose is to just make the ID short

        for ($n = 0; $n < strlen($index); $n++) {
            $i[] = substr($index, $n, 1);
        }

        $pass_hash = hash('sha256', $pass_key);
        $pass_hash = (strlen($pass_hash) < strlen($index) ? hash('sha512', $pass_key) : $pass_hash);

        for ($n = 0; $n < strlen($index); $n++) {
            $p[] = substr($pass_hash, $n, 1);
        }

        array_multisort($p, SORT_DESC, $i);
        $index = implode($i);
    }

    if ($to_num) {
        // Digital number  <<--  alphabet letter code
        $len = strlen($in) - 1;

        for ($t = $len; $t >= 0; $t--) {
            $bcp = pow($base, $len - $t);
            $out = $out + strpos($index, substr($in, $t, 1)) * $bcp;
        }

        if (is_numeric($pad_up)) {
            $pad_up--;

            if ($pad_up > 0) {
                $out -= pow($base, $pad_up);
            }
        }
    } else {
        // Digital number  -->>  alphabet letter code
        if (is_numeric($pad_up)) {
            $pad_up--;

            if ($pad_up > 0) {
                $in += pow($base, $pad_up);
            }
        }

        for ($t = ($in != 0 ? floor(log($in, $base)) : 0); $t >= 0; $t--) {
            $bcp = pow($base, $t);
            $a = floor($in / $bcp) % $base;
            $out = $out . substr($index, $a, 1);
            $in = $in - ($a * $bcp);
        }
    }

    return $out;
}
Zorkzyd
  • 929
  • 3
  • 12
  • 30
  • 1
    `$out = '';` ... and then you `$out = $out + strpos...`? Change that intial defintion to `$out = 0;` – IncredibleHat Jan 11 '20 at 15:02
  • Woah. Basic error. THANKS A LOT IncredibleHat ! – Zorkzyd Jan 11 '20 at 15:06
  • PHP 7 helps us all be honest now in coding with many new warnings and notices ;) – IncredibleHat Jan 11 '20 at 15:09
  • 1
    @IncredibleHat Your solution does not work when `$to_num = false`. Because `$out = $out . substr($index, $a, 1);` will then generate a `0` in front of the string. Better: Insert a new line with `$out = 0;` just after // Digital number <<-- alphabet letter code $len = strlen($in) - 1; – Tom Kuschel Jan 11 '20 at 15:11
  • @TomKuschel ... yeah... I see that bit down later in his code. Weird mash up of strings and numeric logic. Curious what this function is really being used for. Almost seems too complicated for its own good ;) – IncredibleHat Jan 11 '20 at 15:13
  • @IncredibleHat Switching from one type to the other is very ugly as seen here. Not recommended good programming :) – Tom Kuschel Jan 11 '20 at 15:18

0 Answers0