How to convert numeric string to numeric type in php

青灯夜游
Release: 2023-03-15 15:14:02
Original
3653 people have browsed it

Conversion method: 1. Use intval() function, syntax "intval($str)"; 2. Use floatval() function, syntax "floatval($str)"; 3. Use settype() function , syntax "settype($str,"float")".

How to convert numeric string to numeric type in php

The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

php Lieutenant General Number Convert string to numeric type

#Method 1: Use intval() function to convert to integer type

intval(): used to obtain the variable Integer value;

<?php
header("Content-type:text/html;charset=utf-8");
$str = &#39;2334.976&#39;;
var_dump(intval($str));
?>
Copy after login

How to convert numeric string to numeric type in php

Method 2: Use floatval() function to convert to floating point type

floatval(): used Get the floating point value of the variable;

<?php
header("Content-type:text/html;charset=utf-8");
$str = &#39;2334.976&#39;;
var_dump(floatval($str));
?>
Copy after login

How to convert numeric string to numeric type in php

Method 3: Use the settype() function to convert to an integer or floating point type

settype ($var,$type) The function is used to set the variable $var to the specified data type $type.

Example:

<?php
header("Content-type:text/html;charset=utf-8");
$str = &#39;2334.976&#39;;
settype($str,"integer");
var_dump($str);

$str = &#39;2334.976&#39;;
settype($str,"float");
var_dump($str);
?>
Copy after login

How to convert numeric string to numeric type in php

The value of $type can be:

  • "boolean" ( or "bool", starting from PHP 4.2.0)

  • "integer" (or "int", starting from PHP 4.2.0)

  • "float" (only available after PHP 4.2.0, "double" used in older versions is now deprecated)

  • "string"

  • "array"

  • "object"

  • "null" (from PHP 4.2.0)

# The settype() function will change the original variable.

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to convert numeric string to numeric type in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
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