Method: 1. Add "(int)" or "(integer)" before the value, for example "3.14(int)"; 2. Use the intval() function, the syntax is "intval(value)" ;3. Use the ceil() function, the syntax is "ceil(value)"; 4. Use the floor function, the syntax is "floor(value)".
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
php transfers the value For an integer
1, add "(int)" or "(integer)" before the value
<?php $str = '123.456abc'; $int = (int)$str; echo $int."<br>"; $float = 3.14; $int = (integer)$float; echo $int."<br>"; ?>
2. Use the intval() function
<?php $str = '123.456abc'; $int = intval($str); echo $int."<br>"; $float = 5.64; $int = intval($float); echo $int."<br>"; $bool = true; $int = intval($bool); echo $int."<br>"; ?>
##3. Use the ceil() function
ceil() function is used to round up. If there is a decimal, add 1 to the integer part<?php header("Content-type:text/html;charset=utf-8"); $num = 123.456; $int = ceil($num); echo '变量 $int 的类型为:'.gettype($int).'<br>'; echo $int; ?>
4. Use the floor function
floor function is used to round down<?php header("Content-type:text/html;charset=utf-8"); $num = 123.456; $int = floor($num); echo '变量 $int 的类型为:'.gettype($int).'<br>'; echo $int; ?>
PHP Video Tutorial"
The above is the detailed content of How to convert value to integer in php. For more information, please follow other related articles on the PHP Chinese website!