abs(): Argument #1 ($num) must be of type int|float, string given
You encountered a strict error in PHP 8: abs(): Argument #1 ($num) must be of type int|float, string given. This isn't just a syntax change; it's a deliberate tightening of type safety. In previous versions, passing a string to abs() might have worked silently or resulted in unpredictable behavior depending on the context. PHP 8 now throws a fatal error if you attempt to pass a non-numeric value directly into this function without explicit conversion.
In your specific case, the error originates from inside the recursive logic of the konversi function. The line $x = abs($x); is intended to handle negative numbers by converting them to positive integers before processing the number names. However, based on the context of your PDF preview code and the variable name $x, it is highly likely that this input is coming from a database column or a form field stored as a string (e.g., "123" or "-50").
When PHP 8 tries to execute abs("abc") or, in your specific scenario, if the logic somehow passes a non-numeric string representation before the check, it fails. Even if $x looks like a number, if it contains hidden characters or is explicitly defined as a string type by Laravel's type hinting (via ?int or similar traits on properties), the cast inside abs() without prior sanitization triggers this exception.
The Root Cause in Recursive Logic
The real danger here lies in how you are passing variables into the recursive calls. Look at these lines:
$temp = konversi($x / 10) . " Belas";
$temp = konversi($x / 100) . " Ratus" . konversi($x % 10);
In PHP, if $x is a string (e.g., "123"), the mathematical operators / and % will attempt to cast it internally. While 123 / 10 becomes 12.3, the issue arises when you pass that result back into abs() at the very top of the function. If the initial $x passed to the function is a string representing a number, the first line executes:
$x = abs($x); // Error if $x is strictly typed or contains non-numeric chars
However, the error message string given suggests that $x is not being treated as a numeric type at all. This often happens when data comes from a JSON response, an API payload, or a database column defined as VARCHAR. If your PDF generation logic pulls data from a view that renders strings from the model, you are passing strings into a math-heavy recursive function.
Implementing Safe Type Conversion
To resolve this without relying on implicit casting (which PHP 8 warns against in strict contexts), you must explicitly convert the input to an integer or float before calling abs(). The most robust way is using (int) casting for whole numbers, which are required for counting number words.
Here is the corrected approach:
function konversi($x) {
// Explicitly cast to int at the start.
// This handles both "123" (string) and 123 (int).
$num = (int)$x;
// Now it is safe to use abs() if negatives were possible
$num = abs($num);
$angka = ["", "Satu", "Dua", "Tiga", "Empat", "Lima", "Enam",
"Tujuh", "Delapan", "Sembilan", "Sepuluh", "Sebelas"];
if ($num < 12) {
$temp = " " . $angka[$num];
} else if ($num < 20) {
$temp = konversi($num - 10) . " Belas";
} else if ($num < 100) {
// Recursive call must use the converted integer, not raw division results that might remain floats
$temp = konversi((int)floor($num / 10)) . " Puluh" . konversi($num % 10);
} else if ($num < 200) {
$temp = " Seratus" . konversi($num - 100);
} else if ($num < 1000) {
$temp = konversi((int)floor($num / 100)) . " Ratus" . konversi($num % 100);
} else if ($num < 2000) {
$temp = " Seribu" . konversi($num - 1000);
} else if ($num < 1000000) {
$temp = konversi((int)floor($num / 1000)) . " Ribu" . konversi($num % 1000);
} else if ($num < 1000000000) {
$temp = konversi((int)floor($num / 1000000)) . " Juta" . konversi($num % 1000000);
}
return $temp;
}
Handling the Laravel Context
Since you mentioned this is a Laravel view (cetak_permen_1.blade.php) and involves PDF preview, the data flow likely passes through a controller or a helper before reaching the view. If your model property is not explicitly cast to an integer in the Eloquent query (e.g., $user->age vs (int)$user->age), Laravel might pass strings from the database depending on the schema definition.
Before invoking konversi, ensure the value is sanitized:
// In your controller or helper
$number = (int) request('number'); // Or $data['amount']
$words = konversi($number);
Using (int) forces PHP to parse the string as an integer. If the string contains non-numeric characters (like "123abc"), the cast results in 0 or throws a warning depending on strictness, but it prevents the specific abs() type error because the value is no longer treated as a generic string object by the function signature logic.
Why Implicit Casting Fails in PHP 8
In PHP 7 and earlier, abs("123") worked because the engine implicitly cast the string to an integer before calling the function. PHP 8 removed this convenience for built-in functions like abs() to enforce stricter type discipline. This forces developers to write code that is resilient to data coming from external sources (JSON, DB, APIs) which often arrive as strings.
By explicitly casting $x to (int) at the entry of your function, you decouple your internal logic from the input format. This ensures that whether the user sends a string "500" or the database returns 500, your recursive number-to-words algorithm functions correctly without triggering fatal errors during PDF generation.