PHP is blessed with various functions that make the life of programmers easy, in this article we would exploring one such function that is Intval in PHP. Following Pointers will be covered in this article,
Intval In PHP
It is an inbuilt function that is used to get the integer value of a variable. Using the specified base for the conversion, it returns the integer value of the variable provided. Default base is 10. It should not be used on objects else it would return E_NOTICE level error and return 1.
Syntax: intval(var_name, base)
Var_name: It specifies the scalar value provided will be converted into an integer
Base: It specifies the base for the conversion to its corresponding integer.
Some of the important key points regarding base:
- If base is 0, the base used is determined by the format of var:
- If string includes prefix “0x” (or “0X”), the base is taken as 16 (hex)
- The base is taken as 8 (octal), if the string starts with “0”
- Else the base is taken as 10 (decimal).
It returns the integer value of var on success else it returns 0 on failure. Non-empty arrays return 1 and empty arrays return 0. Generally, the maximum value depends on the system. Maximum signed integer range for 32 bit systems is -2147483648 to 2147483647 and maximum signed integer value for 64 bit systems is 9223372036854775807.
Most likely strings will return 0, Even though this depends on the leftmost characters of the string. The common rules of integer casting apply for this.
Let us now take a look at some programs,
Example For Intval
<?php $var = "81"; echo intval($var)." ".intval($var, 9); ?>
Output:
<?php $var = '9.236886'; $var1 = intval($var); echo $var1; ?>
Output:
<?php $var = 0x1A; $var1 = intval($var); echo $var1; ?>
Output:
With this we come to an end of this article, I hope you understood the inbuilt function intval in php. If you found this blog relevant, check out the PHP Certification Training by Edureka, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe.
Got a question for us? Please mention it in the comments section of ”Intval in PHP” and I will get back to you.