Home  >  Article  >  Backend Development  >  How to convert string to integer type in php

How to convert string to integer type in php

青灯夜游
青灯夜游Original
2021-11-02 18:42:302316browse

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".

How to convert string to integer type in php

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 = &#39;125.456abc&#39;;
$int = intval($str);
var_dump($int);
?>

Output result:

How to convert string to integer type in php

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 = &#39;1253.456abc&#39;;
$int = (integer)$str;
var_dump($int);
?>

Output results:

How to convert string to integer type in php

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn