Home >Backend Development >PHP Problem >How to convert string to integer type in php
Conversion method: 1. Use the intval() function, syntax "intval($str)"; 2. Force the variable type to be converted by adding the target type enclosed in parentheses before the string variable "( integer)", syntax "(integer)$str".
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
php will string Convert to integer type
Method 1: Use the intval() function
intval(): used to obtain the integer value of the variable;
Example:
<?php header("Content-type:text/html;charset=utf-8"); $str = '125.456abc'; $int = intval($str); var_dump($int); ?>
Output result:
Method 2: Add parentheses before the string variable The target type is forced to convert the variable type "(integer)"
(integer): String variables can be converted into integer types
Example:
<?php header("Content-type:text/html;charset=utf-8"); $str = '1253.456abc'; $int = (integer)$str; var_dump($int); ?>
Output results:
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of How to convert string to integer type in php. For more information, please follow other related articles on the PHP Chinese website!