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

How to convert string to number in php

青灯夜游
Release: 2023-03-17 19:52:01
Original
5628 people have browsed it

4 conversion methods: 1. Use the intval() function to convert, the syntax is "intval($val)"; 2. Use the settype() function to convert, the syntax is "settype($val,"integer")" ; 3. Add the target type "(int)" enclosed in parentheses before the variable, the syntax "(int)$val"; 4. Use the " " operator to add the string and the number 0, the syntax "$val" 0".

How to convert string to number in php

The operating environment of this tutorial: windows7 system, PHP8 version, DELL G3 computer

php will value Forced to a numeric type

#Method 1: Use the specific conversion function intval()

intval() function is used to obtain the integer of the variable value.

intval() function returns the integer value of variable var by using the specified base conversion (default is decimal). intval() cannot be used with object, otherwise an E_NOTICE error will be generated and 1 will be returned.

<?php
header("Content-type:text/html;charset=utf-8");
$str = &#39;123.456abc&#39;;
echo $str."<br>";
$int = intval($str);
echo $int."<br>";
echo &#39;变量 $int 的类型为:&#39; . gettype($int) . &#39;<br>&#39;;
echo &#39;<hr>&#39;;

$str = &#39;abc123.456&#39;;
echo $str."<br>";
$int = intval($str);
echo $int."<br>";
echo &#39;变量 $int 的类型为:&#39; . gettype($int) . &#39;<br>&#39;;
echo &#39;<hr>&#39;;


$str = &#39;123.abc456&#39;;
echo $str."<br>";
$int = intval($str);
echo $int."<br>";
echo &#39;变量 $int 的类型为:&#39; . gettype($int) . &#39;<br>&#39;;
?>
Copy after login

How to convert string to number in php

Note: When using the intval() function to convert a string to a number, you can only extract the number before the character. If it starts with a letter, the number extracted is 0.

Method 2: Use the settype() function

<?php
header("Content-type:text/html;charset=utf-8");
$str = &#39;123.456abc&#39;;
echo $str."<br>";
settype($str,"integer");
echo $str."<br>";
echo &#39;修改后的类型为:&#39; . gettype($str) . &#39;<br>&#39;;
echo &#39;<hr>&#39;;

$str = &#39;abc123.456&#39;;
echo $str."<br>";
settype($str,"integer");
echo $str."<br>";
echo &#39;修改后的类型为:&#39; . gettype($str) . &#39;<br>&#39;;
?>
Copy after login

How to convert string to number in php

##Description:

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

settype ( $var ,$type )
Copy after login

$type Settable values:

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

  • "integer" (or "int" since PHP 4.2.0)

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

  • "string"

  • "array"

  • "object"

  • ##"null" (from PHP 4.2.0 onwards)
  • settype() function will change The type of the variable itself.

Method 3: Add the target type "(int)" or "(integer)" enclosed in parentheses before the variable to be converted

<?php
header("Content-type:text/html;charset=utf-8");
$str = &#39;123.456abc&#39;;
$int = (int)$str;
echo $int."<br>";
echo &#39;变量 $int 的类型为:&#39; . gettype($int) . &#39;<br>&#39;;
?>
Copy after login

How to convert string to number in php

Method 4: Use " " operator to add string and number 0

<?php
header("Content-type:text/html;charset=utf-8");
$str = &#39;123abc&#39;;
echo $str."<br>";
$int = $str+0;
echo $int."<br>";
echo &#39;变量 $int 的类型为:&#39; . gettype($int) . &#39;<br>&#39;;
?>
Copy after login

How to convert string to number in phpBecause PHP is weakly typed Languages ​​that perform invisible numeric type conversion.

Recommended learning: "

PHP Video Tutorial

"

The above is the detailed content of How to convert string to number 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