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

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

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

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

This stack trace points directly to line 105 in config.php, where a PHP arithmetic operation fails because the operands have incompatible types. The specific line triggering the crash is:

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

The error message Unsupported operand types: string + int indicates that $out is currently a string, while the expression on the right side evaluates to an integer. In PHP, attempting to add a string and an integer using the + operator does not perform implicit type casting in all contexts; it throws a TypeError when the result needs to be treated as a numeric value but one operand is strictly a string.

In this specific implementation, which appears to be a base-62 ID conversion utility (similar to Base36 or Base64 encoding), $out serves as an accumulator for the final numeric representation of the input string. The logic iterates through each character of the input string ($in), calculates its position in the custom alphabet defined by $index, and multiplies that position by a power of the base ($bcp).

The root cause is likely how $out was initialized or modified prior to this iteration, or perhaps an earlier version of the code returned a string. However, looking at the function signature and the if ($to_num) block where this error occurs, $out is initialized as an empty string ($out = '';) on line 5.

An empty string in PHP behaves like zero in many arithmetic contexts, but strict type checking in newer PHP versions (7.4+, and especially 8.0+) can trigger a TypeError if the code expects a numeric result immediately. The fix is straightforward: ensure $out starts as an integer before entering the accumulation loop.

Change line 5 from:

$out = '';

to:

$out = 0;

By initializing $out to 0, you explicitly define it as an integer. Consequently, when the + operator is used on line 105, PHP will correctly perform integer arithmetic without throwing a type error. This is a common pattern in base-conversion algorithms where a string accumulator works for concatenation (.), but a numeric accumulator requires integer initialization.

The Role of $pass_key and Type Safety

The function also supports an optional $pass_key argument to obfuscate the character mapping using SHA256 or SHA512 hashing. While this adds security by randomizing which character maps to which index, it introduces complexity regarding the data types involved.

When $pass_key is provided:

  1. The code generates an array of characters from the original index.
  2. It hashes the key and creates a corresponding hash string.
  3. It uses array_multisort to reorder the characters based on the hash values.
  4. Finally, it reassembles the new index string.

This logic modifies $index, but it does not affect the initialization of $out inside the if ($to_num) block. The sorting and reassembly happen before the conversion loop starts. Therefore, regardless of whether a custom key is used to shuffle the alphabet, the accumulator $out must still be an integer when calculating the numeric value.

If you are maintaining this legacy function or integrating it into a modern PHP 8+ project (such as a Laravel application running on XAMPP), always verify that variables intended for mathematical accumulation are initialized with numeric types. Relying on implicit casting with empty strings is a fragile practice that breaks under strict error reporting (error_reporting(E_ALL);).

Debugging the Stack Trace

The stack trace shows the call originating from category.php at line 84, invoking alphaID('f', true). The second argument true sets the $to_num flag. This confirms the code is entering the branch designed to convert an alpha string into a numeric integer. Since the function returns $out, and the caller expects a number, ensuring $out remains numeric throughout the execution flow is critical.

If you encounter similar issues in your own projects involving custom base-conversion logic:

  1. Check all assignments to accumulator variables.
  2. Ensure string concatenation (.) and arithmetic addition (+) use compatible types.
  3. If a variable starts as an empty string but needs to be summed, cast it or initialize it correctly immediately.

Fixing the initialization on line 5 resolves the immediate crash, allowing the function to correctly parse strings like 'f' into their corresponding numeric values based on the shuffled alphabet.

Our network