Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PHP

PHP Casting Strings and Floats to Integers

<?php
// Converting float to integer
$x = 23.72;
$float_int = (int)$x;
echo $int_cast;
/* This outputs 23. From this, we observe that the floor is calculated of the
float and not rounding or ceiling of the float*/

echo "<br>";

// Converting string to integer
$x = "62.924";
$int_cast = (int)$x;
echo $int_cast;
/* This outputs 62. From this, we observe that the floor is calculated of the
float and not the rounded number or ceiling of the string*/
// Converting string to integer
$x = "Text";
$int_cast = (int)$x;
echo $int_cast;
/* This outputs 0. From this, we observe that if the string contains text, 
it outputs*/
?>
 
PREVIOUS NEXT
Tagged: #PHP #Casting #Strings #Floats #Integers
ADD COMMENT
Topic
Name
2+4 =