Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

PHP Variable Handling intval() Function



The PHP Variable Handling intval() function is used to convert a value to an integer (a whole number). It accepts two inputs: the value to change and the base (number system) to use. It defaults to base 10 (decimal), but it also supports binary, octal and hexadecimal.

If the value is not a number, PHP will try to convert it with special rules. If the value is null or not a number, intval() usually returns zero. If the value is an array, an empty array returns 0 and a non-empty array returns 1. On 32-bit processors, the largest number it may return is 2,147,483,647. On 64-bit platforms, it can produce even greater results up to 9,223,372,036,854,775,807.

Syntax

Below is the syntax of the PHP Variable Handling intval() function −

int intval ( mixed $value, int $base = 10 )

Parameters

Below are the parameters of the intval() function −

  • $value − It is the value that needs to be converted into an integer. It can be a number, string or other scalar value.

  • $base − It is the number system to use for conversion. Default is 10 (decimal).

Return Value

The intval() function returns the integer version of the given value. If conversion fails, it returns 0. Empty array returns 0, non-empty array returns 1.

PHP Version

First introduced in core PHP 4, the intval() function continues to function easily in PHP 5, PHP 7, and PHP 8.

Example 1

This basic program converts a floating-point number into an integer with the help of the PHP Variable Handling intval() function. The decimal part is removed and only the whole number remains.

<?php
   // A floating-point number
   $num = 12.98; 

   // Convert to integer
   $result = intval($num); 
   echo $result; 
?>

Output

Here is the outcome of the following code −

12

Example 2

In the below PHP code we will use the intval() function and convert a string into a integer number. So the program will convert a numeric string into an integer. If the string starts with numbers they are kept, otherwise it will returns 0.

<?php
   // A string with numbers
   $str = "150 apples"; 

   // Convert to integer
   $result = intval($str); 
   echo $result; 
?> 

Output

This will generate the below output −

150

Example 3

Now the below code uses different bases like binary, octal and hexadecimal. And the program converts strings in different number systems into integers with the help of the intval() function and specifying the base.

<?php
   // Binary (2)
   $binary = "110"; 

   // Octal (8)
   $octal = "075"; 

   // Hexadecimal (16)
   $hex = "0x1A"; 

   echo intval($binary, 2) . "\n"; 
   echo intval($octal, 8) . "\n"; 
   echo intval($hex, 16) . "\n";
?> 

Output

This will create the below output −

6
61
26

Example 4

In the following example, we are using the intval() function to show return values with different types of variables.

<?php
   echo intval(42);                      
   echo intval(4.2);                     
   echo intval('42');                    
   echo intval('+42');                   
   echo intval('-42');                   
   echo intval(042);                     
   echo intval('042');                   
   echo intval(1e10);                    
   echo intval('1e10');                  
   echo intval(0x1A);                    
   echo intval('0x1A');                  
   echo intval('0x1A', 0);               
   echo intval(42000000);                
   echo intval(420000000000000000000);   
   echo intval('420000000000000000000'); 
   echo intval(42, 8);                   
   echo intval('42', 8);                 
   echo intval(array());                 
   echo intval(array('foo', 'bar'));     
   echo intval(false);                   
   echo intval(true);                    
?> 

Output

Following is the output of the above code −

100
100
100
100
-100
68
104
10000000000
10000000000
43
10400000
-4275113695319687168
9223372036854775807
11
13
php_variable_handling_functions.htm
Advertisements