What is the use of intval in php?
View Discussion Show Improve Article Save Article View Discussion Improve Article Save Article The intval() function is an inbuilt function in PHP which returns the integer value of a variable. Syntax: int intval ( $var, $base ) Parameters: This function accepts two parameters out of which one is mandatory while the other one is optional. Parameters are described below:
Return Value: It returns the corresponding integer value of $var. Examples: Input : $var = '120', $base = 8 Output : 80 Input : $var = 0x34 Output : 52 Input : $var = 034 Output : 28 Below programs illustrate the use of intval() function in PHP: Program 1:
Program 2:
Program 3:
Reference: http://php.net/manual/en/function.intval.php (PHP 4, PHP 5, PHP 7, PHP 8) intval — Get the integer value of a variable Descriptionintval(mixed Parametersvalue The scalar value being converted to an integer base The base for the conversion
Return Values The integer value of The maximum value depends on the system. 32 bit systems have a maximum signed integer range of -2147483648 to 2147483647. So
for example on such a system, Strings will most likely return 0 although this depends on the leftmost characters of the string. The common rules of integer casting apply. Examples
Example #1 intval() examples The following examples are based on a 32 bit system.
Notes
See Also
Ken ¶ 10 years ago
leon at leonidasjp dot nl ¶ 5 years ago
winbill at hotmail dot com ¶ 11 years ago
zak at php dot net ¶ 22 years ago
Ben Laurienti ¶ 16 years ago
spoon_reloaded at gmail dot com ¶ 13 years ago
Anony Moose ¶ 2 years ago
Anonymous ¶ 2 years ago
Anthony ¶ 4 years ago
espertalhao04 at hotmail dot com ¶ 9 years ago
int('j18ugj9hu0gj5hg'); tuxedobob at mac dot com ¶ 18 years ago
pfreet at gmail dot com ¶ 9 years ago
mkamerma at science dot uva dot nl ¶ 16 years ago
yves ¶ 11 years ago
taylorsarrafian at gmail dot com ¶ 7 years ago
echo strval( -0.0001 ); // -.0001 simon at npkk dot cz ¶ 16 years ago
How do I cast an int in PHP?You can use (int) or (integer) to cast a variable to integer, use (float) , (double) or (real) to cast a variable to float. Similarly, you can use the (string) to cast a variable to string, and so on. The PHP gettype() function returns "double" in case of a float for historical reasons.
How do I cast a string in PHP?Answer: Use the strval() Function
You can simply use type casting or the strval() function to convert an integer to a string in PHP.
How do I check if a variable is an int in PHP?The is_int() function checks whether a variable is of type integer or not. This function returns true (1) if the variable is of type integer, otherwise it returns false.
Is a string PHP?Definition and Usage. The is_string() function checks whether a variable is of type string or not. This function returns true (1) if the variable is of type string, otherwise it returns false/nothing.
|