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

How to convert string to integer type in php

青灯夜游
Release: 2023-03-13 16:50:02
Original
2347 people have browsed it

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);
?>
Copy after login

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);
?>
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template