Summary of Usage of php intval Function

  • 2021-12-05 05:49:30
  • OfStack

Syntax:


int intval ( $var, $base )

Parameters:

This function accepts two arguments, one of which is required and the other is optional.

The parameters are as follows:

$var: This is a required parameter to be used as a variable to be converted to an integer value.

$base: This is an optional parameter that specifies the basis on which $var is converted to the corresponding integer. If $base is not specified. If $var contains 0x (or 0X) as a prefix, base is treated as 16. If $var contains 0, the cardinality is 8; Otherwise, the cardinality is 10.

Return value:

Returns the corresponding integer value of $var.

The intval () function code uses example 1:


<?php 

$var = '7.423'; 

$int_value = intval($var); 

echo $int_value; 

?>

Output:

7

The intval () function code uses example 2:


<?php 

$var = 0x423; 

$int_value = intval($var); 

echo $int_value; 

?>

Output:

1059

The intval () function code uses example 3:


<?php 

$var = "64"; 

echo intval($var)."\n".intval($var, 8); 

  

?>

Output:

64

52

Examples


<?php 
echo intval(42); // 42 
echo intval(4.2); // 4 
echo intval('42'); // 42 
echo intval('+42'); // 42 
echo intval('-42'); // -42 
echo intval(042); // 34 
echo intval('042'); // 42 
echo intval(1e10); // 1410065408 
echo intval('1e10'); // 1 
echo intval(0x1A); // 26 
echo intval(42000000); // 42000000 
echo intval(420000000000000000000); // 0 
echo intval('420000000000000000000'); // 2147483647 
echo intval(42, 8); // 42 
echo intval('42', 8); // 34 
?> 


Related articles: