PHP intval

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

PHP : intval() function

Description

The intval() function is used to get the integer value of a variable.

Version

(PHP 4 and above)

Syntax

intval(var_name, base) 

Parameter

Name

Description

Required /
Optional

Type

var_name

The scalar value being converted to an integer

Required

Mixed*

base

The base for the conversion. (default is base 10)

Optional

Integer

*Mixed : Mixed indicates that a parameter may accept multiple (but not necessarily all) types.

Return value

The integer value of var on success, or 0 on failure

Value Type : Integer.

Example :

  1. echo intval(102).'
    ';  

  2. echo intval(102.22).'
    ';  

  3. echo intval('102').'
    ';  

  4. echo intval(+102).'
    ';  

  5. echo intval(-102).'
    ';  

  6. echo intval(0102).'
    ';  

  7. echo intval('0002').'
    ';  

  8. echo intval(1e20).'
    ';  

  9. echo intval(0x1B).'
    ';  

  10. echo intval(10200000).'
    ';  

  11. echo intval(10200000000000000000).'
    ';  

  12. echo intval(10, 2).'
    ';  

  13. echo intval('10', 2).'
    ';  

  14. ?>  

Output :

102
102
102
102
-102
66
2
0
27
10200000
0
10
2