Fatal error: Uncaught TypeError: Unsupported operand types: string + int in

Viewed 11143

This is my error tittle:

Fatal error: Uncaught TypeError: Unsupported operand types: string + int in C:\xamppp\htdocs\file\thefile\config.php:105 Stack trace: #0 C:\xamppp\htdocs\file\thefile\category.php(84): alphaID('f', true) #1 {main} thrown in C:\xamppp\htdocs\file\thefile\config.php on line 105

The error on line 105:

{$out = $out + strpos($index, substr($in, $t, 1)) * $bcp;}

And this is the codes:

<?php

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

  if ($pass_key !== null) {

    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) {
    $len = strlen($in) - 1;

    for ($t = $len; $t >= 0; $t--) {
      $bcp = bcpow($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 {
    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 = bcpow($base, $t);
      $a   = floor($in / $bcp) % $base;
      $out = $out . substr($index, $a, 1);
      $in  = $in - ($a * $bcp);
    }
  }

  return $out;
}
?>
2 Answers

The error you've provided only occurs on PHP 8 and it is because you're trying to add a string to an integer. In previous versions the error message used to be "A non-numeric value encountered" which in some situations made the issue a lot clearer.

Your issue will be resolved by changing $out = '' to $out = null which has no adverse affect on the output.

  • In PHP v5.6 you would receive no error at all (and the code would work fine)
  • In PHP v7 you would be informed with a WARNING that "A non-numerical value was encountered"
  • In PHP v8 you are informed with a FATAL error that "Uncaught TypeError: Unsupported operand types: string + int"

i also have same error when i redirect to new page so i just use a (int) on my previous page where i use $_SERVER['QUERY_STRING'] statement just like (int) $_SERVER['QUERY_STRING'] in php 8.0 And my problem is solved hope it will help you

Related