The intval() function is a built-in function in PHP that converts a variable to an integer. It is similar to the floatval() function, which converts a variable to a float.


The syntax of the intval() function is as follows:

int intval(mixed $var [, int $base = 10 ])

The function takes two parameters. The first parameter, $var, is the variable to be converted to an integer. The second parameter, $base, is an optional parameter that specifies the base of the number system to use for the conversion. The default value is 10.

Example Usage

Here is an example of how to use the intval() function in PHP:

$var1 = "42";
$var2 = "101010";
$var3 = "2c";
$var4 = "not a number";
echo intval($var1) . "\n";  // output: 42
echo intval($var2, 2) . "\n";  // output: 42 (binary to decimal conversion)
echo intval($var3, 16) . "\n";  // output: 44 (hexadecimal to decimal conversion)
echo intval($var4) . "\n";  // output: 0 (cannot convert "not a number" to an integer)

In this example, we define four variables with different data types: $var1, $var2, $var3, and $var4. We then use the intval() function to convert each variable to an integer and output the result. The second parameter is used to specify the base of the number system in the case of $var2 and $var3. The output shows the integer value of each variable, or 0 if the variable cannot be converted.


The intval() function is a useful tool for converting a variable to an integer in PHP. It can be used to convert strings, floats, and booleans to integers, among other data types. By using this function, developers can ensure that their code is working with the correct data types and avoid errors that may occur when working with mixed data types.

Practice Your Knowledge

What does the intval() function do in PHP?

Quiz Time: Test Your Skills!

Ready to challenge what you've learned? Dive into our interactive quizzes for a deeper understanding and a fun way to reinforce your knowledge.

Do you find this helpful?