• PHP Video Tutorials

PHP - intval() Function



Definition and Usage

The function intval() gets the integer value of a variable.

Syntax

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

Parameters

Sr.No Parameter & Description
1

value

Mandatory. This parameter serves as the variable which needs to be converted to its integer value.

2

base

Optional. It has no effect unless the value parameter is a string.

If base is not specified or 0, the base used is determined by the format of value:

  • If string contains a "0x" (or "0X") prefix, the base is taken as 16 (hex).
  • If string starts with "0", the base is taken as 8 (octal).
  • By default, the base is taken as 10 (decimal).

Return Values

This function returns interger value of value on success or 0 on failure. Empty arrays return 0, non-empty arrays return 1.

Dependencies

PHP 4 and above

Example

Following example demonstrates return values with different types of variables −

<?php
   echo intval(100).'<br>';                         //100
   echo intval(100.11).'<br>';                      //100
   echo intval('100').'<br>';                       //100
   echo intval(+100).'<br>';                        //100
   echo intval(-100).'<br>';                        //-100
   echo intval(0104).'<br>';                        //68
   echo intval('0104').'<br>';                      //104
   echo intval(1e10).'<br>';                        //10000000000
   echo intval('1e10').'<br>';                      //10000000000
   echo intval(0x2B).'<br>';                        //43
   echo intval(10400000).'<br>';                    //10400000
   echo intval(420000000000000000000).'<br>';//-4275113695319687168
   echo intval('420000000000000000000').'<br>';//9223372036854775807
   echo intval(11, 12).'<br>';                      //11
   echo intval('11', 12).'<br>';                    //13
?>

Output

This will produce following result −

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