The intval() function in PHP
The PHP intval() function is used to get the integer value of a data.
The syntax of the function is as follows:
intval(
mixed $value,
int $base = 10
): int
The function accepts two parameters:
- The
$valueto convert tointtype (required) - The
$basenumber system for the conversion (optional)
This function converts the $value to int type and return it. How the function extracts the converts that value is up to the PHP engine.
For a string of numbers, the integer representing the number will be returned.
If you pass a string of characters, it will return 0.
Here are some examples of running the function on different data types:
intval("101"); // returns int -> 101
intval("Jack"); // returns int -> 0
intval("4 weeks"); // 4
intval("week 4"); // 0
intval(3.14231); // returns int -> 3
intval(NULL); // returns int -> 0
intval(true); // returns int -> 1
intval(false); // returns int -> 0
The intval() function one of the ways you can convert a string to number.
For other methods, you can see the PHP convert string to number guide.
Now you’ve learned how the intval() function works in PHP. Nice! 👍
More PHP tutorials
See allPHP
PHP form validation basic example
Dec 22, 2022
PHP
PHP validate email address (with code examples)
Dec 22, 2022
PHP
How to redirect using PHP with code examples
Dec 22, 2022
PHP
Handling form data using PHP (GET and POST)
Dec 21, 2022
PHP
How to fix Wordpress PHP error missing MySQL extension
Dec 21, 2022