PHP: is_int() vs is_numeric()
Both of the functions looks similar, but there is a difference, which can screw your time if you’re not aware of it and using blindly! is_int() seems same to is_numeric(), checking the variable if it’s integer or not, but it’s not exactly what you’re thinking.
The key difference between these two functions is that is_int() checks the type of variable, while is_numeric() checks the value of the variable.
From PHP.net,
is_int: Find whether the type of a variable is integer
is_numeric: Finds whether a variable is a number or a numeric string
So, if you check something like:
1 | $var = "123"; |
$var is a string of numbers, not an integer value.
Therefor is_int should return false as it’s not an integer, it’s a string.
However it is a numeric string, so hence is_numeric should return true.
Leave a comment
Welcome to my Blog
Certifications
Honor
Recognition
Contributions
Categories
- Apache (2)
- Domain name (2)
- eCommerce (2)
- htaccess (1)
- Humor (3)
- Instagram API (1)
- jQuery (4)
- JSON (1)
- Linux (10)
- Magento (142)
- Magento admin (58)
- Magento Certification (5)
- Magento error (13)
- Magento frontend (68)
- Magento Imagine (2)
- Magento Interview (5)
- Magento Master (1)
- Magento2 (10)
- Mobile (1)
- MySQL (7)
- OroCRM (2)
- Performance (2)
- PHP (8)
- Prototype JS (3)
- Security (4)
- Wordpress (3)
- XML (2)