The to-int operation converts the input value into an integer (whole number). This is useful when you need numeric values in a clean, standardized format for calculations, conditions, or API requests.
How it works
- String containing a number
- “42” → 42
- “1.25” → 1 (decimal part removed)
- Float
- 1.25 → 1
- -3.9 → -3
(the decimal part is truncated, not rounded)
- Boolean
- true → 1
- false → 0
- Null
- null → 0
- Other types (arrays, objects, resources)
- Not supported — results in an error.
Examples
Input | Result |
---|---|
“42” | 42 |
“1.25” | 1 |
1.25 | 1 |
-3.9 | -3 |
true | 1 |
false | 0 |
null | 0 |
[1,2,3] | ❌ Error |
Usage in PalDock
Use to-int when you need to:
- Normalize numeric fields before sending them to APIs that require integers.
- Strip decimals when only whole numbers are accepted.
- Convert boolean-like values into numeric flags (true → 1, false → 0).
- Ensure that empty or null values default to 0.
Best Practices
- Remember that decimals are truncated, not rounded (2.9 → 2).
- Text that is not numeric will cause an error. Validate or sanitize your input first.
- Use this operation only when the target field should always be a whole number.
References
https://www.php.net/manual/en/language.types.integer.php#language.types.integer.casting