Intval Function in PHP

Introduction

In this article I explain the Intval() function in PHP. The Intval() function returns the integer value of a variable. You can not use the intval() function on an object.

Syntax

intval($var_name,base);

 

Parameter Description
Var name Scalar value to be converted into an integer
Base Base for the conversion

The default base is ten.

Example

In the following example, the empty array returns the output zero and the nonempty array returns the one:

<html>

<body bgcolor="#FFFFCC">

<?php

echo intval(33)."<br>";                

echo intval(3.4)."<br>";                  

echo intval('31')."<br>";                 

echo intval('+321')."<br>";               

echo intval('-313')."<br>";                 

echo intval(0213)."<br>";                    

echo intval('032')."<br>";                  

echo intval(2e10)."<br>";                   

echo intval('1e10')."<br>";                 

echo intval(0x1A)."<br>";                   

echo intval(312000000)."<br>";               

echo intval(3430000000000000000)."<br>";  

echo intval('313000000000000000000')."<br>";

echo intval(53, 9)."<br>";                  

echo intval('53', 9)."<br>";                

echo intval(array())."<br>";                

echo intval(array('class', 'name'));    

?>

</body>

</html>

The maximum value depends on the system such as 32-bit system integers are in the range -2147483648 to 2147483647 and 64-bit system integers are in the range 9223372036854775807.
The strings will most likly return zero although this depends on the leftmost characters of the string.

Output

Intval Function in PHP.jpg

Up Next
    Ebook Download
    View all
    Learn
    View all