The Integer Trap in PHPExcel Type Detection
The error message "Trying to access array offset on value of type int" is a classic symptom of a logic error, not a framework bug. It specifically indicates that your code attempted to use square bracket notation ([]) or unset() on an integer variable instead of an array or string. In the context of the DefaultValueBinder class in PHPExcel (now part of PHPSpreadsheet), this crash occurs inside the dataTypeForValue method when trying to determine how to treat a cell's content.
Looking at the specific line where the failure happensâif ($pValue[0] === '=' && strlen($pValue) > 1)âthe issue becomes immediately apparent. The condition checks if the first character of $pValue is an equals sign. However, this check assumes that $pValue is always a string or at least array-like.
The problem arises when PHP's loose typing allows an integer to be passed into this function, or when a previous operation implicitly cast a value to an integer in a way that bypasses the initial null or empty checks. If $pValue is an integer (for example, 123), the expression $pValue[0] attempts to access the first character of that integer. Since integers are not arrays and do not support string offset access, PHP throws a fatal error before it can evaluate the rest of the condition.
Understanding the Root Cause
The logic flow in dataTypeForValue is designed to detect formulas by checking for a leading =. This is valid Excel syntax: any cell starting with = is treated as a formula reference. However, the function lacks a strict type guard before accessing the string offset.
In older versions of PHP (prior to 8.0), this might have triggered a warning and execution would continue depending on how the error was handled, but in modern PHP environments or strict configurations, it halts execution. Even if you are using PHPSpreadsheet within a Laravel application, the underlying PHPExcel logic remains sensitive to the data types it receives. If your export button is passing an integer directly to this method without ensuring it is treated as a string first, the crash is inevitable when that specific branch of the if statement evaluates to true or even just attempts to access the index.
The Solution: Explicit Type Guarding
To resolve this, you must ensure that $pValue is treated as a string before attempting to access its characters. The most robust approach is to cast the value explicitly at the start of the function or within the specific condition checking for the equals sign.
Here is how you can modify the dataTypeForValue method to prevent the error:
public static function dataTypeForValue($pValue = null)
{
// Ensure we are working with a string for offset operations
$strValue = (string)$pValue;
if ($strValue === '') {
return PHPExcel_Cell_DataType::TYPE_STRING;
} elseif ($strValue instanceof PHPExcel_RichText) {
return PHPExcel_Cell_DataType::TYPE_INLINE;
} elseif ($strValue[0] === '=' && strlen($strValue) > 1) { // Safe now
return PHPExcel_Cell_DataType::TYPE_FORMULA;
} elseif (is_bool($pValue)) {
return PHPExcel_Cell_DataType::TYPE_BOOL;
} elseif (is_float($pValue) || is_int($pValue)) {
return PHPExcel_Cell_DataType::TYPE_NUMERIC;
} elseif (preg_match('/^[\+\-]?([0-9]+\\.?[0-9]*|[0-9]*\\.?[0-9]+)([Ee][\-\+]?[0-2]?\d{1,3})?$/i', $strValue)) {
// Handle numeric formatting logic here...
} elseif (is_string($pValue) && array_key_exists($pValue, PHPExcel_Cell_DataType::getErrorCodes())) {
return PHPExcel_Cell_DataType::TYPE_ERROR;
}
return PHPExcel_Cell_DataType::TYPE_STRING;
}
By casting $pValue to a string immediately, you ensure that operations like $strValue[0] work as intended. If an integer 123 is passed, it becomes the string "123", and accessing index [0] safely returns "1".
Why This Matters for Excel Exports
When building export features, data types can be unpredictable. User inputs might come through GET parameters or forms as strings, but database queries often return integers or floats directly. If your controller logic passes these raw values to PHPSpreadsheet without sanitizing or casting them, you risk triggering this exact exception.
Additionally, be aware of the implications on numeric formatting. Casting an integer to a string changes how it is interpreted regarding leading zeros or large numbers exceeding PHP_INT_MAX. The original code had specific checks for these scenarios (like checking if $pValue > PHP_INT_MAX), so ensure your casting logic does not inadvertently strip precision needed for those edge cases.
If you are looking for more stable alternatives to PHPSpreadsheet that handle type coercion more gracefully out of the box, you might consider exploring modern libraries like Spatie/Laravel-Excel or Maatwebsite/Laravel-Excel, which often abstract away these low-level PHPSpreadsheet nuances. However, if you must stick with the legacy library, explicit casting in your dataTypeForValue override is the definitive fix.