ToInt
ToInt - Casts value to integer.
Empty arrays return 0, non-empty arrays return 1.
ToInt only works with null
, bool
, float
, string
, array
, any other type
is ignored.
Basic usage
use Jawira\Sanitizer\Filters as Sanitizer;
class User {
#[Sanitizer\ToInt]
public int|string $age;
}
Parameters
- int
base
(optional): -
Sets the base, only used when value is string.
When the base is set to 0, the base is determined by string prefix.
If the string starts with:0b
or0B
the base is 2.0x
or0X
the base is 16.0
the base is 8.
Default value is 10.
Examples
Cast value to integer.
use Jawira\Sanitizer\Filters as Sanitizer;
class Product {
#[Sanitizer\ToInt]
public $quantity;
}
null → 0
true → 1
false → 0
450 → 450
8.0 → 8
8.1 → 8
"15" → 15
"-15" → -15
"70.99" → 70
"-70.99" → -70
"5e3" → 5000
[] → 0
['foo', 'bar'] → 1
Cast binary string to integer.
use Jawira\Sanitizer\Filters as Sanitizer;
class Product {
#[Sanitizer\ToInt(base: 2)]
public $quantity;
}
"10011" → 19
"0b10011" → 19
Detect string base.
use Jawira\Sanitizer\Filters as Sanitizer;
class Product {
#[Sanitizer\ToInt(base: 0)]
public $quantity;
}
"0b10011" → 19
"0x1A" → 26
"077" → 63
See also
ToString - Casts value to string.