What are the functions for converting numeric values ​​in php?

青灯夜游
Release: 2023-03-17 22:18:02
Original
3465 people have browsed it

The conversion functions include: 1. intval(), which can convert a string into an integer value, the syntax is "intval($str)"; 2. floatval(), which can convert a string into a floating point Type value, syntax "floatval($str)"; 3. settype(), you can set the variable to an integer or floating point value, syntax "settype($str,"integer")" or "settype($str,"float ")"; 4. base_convert().

What are the functions for converting numeric values ​​in php?

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

1. intval() function--integer Type conversion function

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

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;;
$int = intval($str);
echo $int."<br>";
echo &#39;变量 $int 的类型为:&#39; . gettype($int) . &#39;<br>&#39;;
?>
Copy after login

What are the functions for converting numeric values ​​in php?

2. floatval() function--floating point conversion function

floatval — Get the floating point value of a variable

Example:

<?php   
$str="3.14";   
$float=floatval($str);   
var_dump($str); 
var_dump($float); 
?>
Copy after login

What are the functions for converting numeric values ​​in php?

3. settype() function

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

What are the functions for converting numeric values ​​in php?

Description:

The settype() function is used to set the variable $var 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.

4. Use the base_convert() function

base_convert() function to convert numbers between any bases, just set "

bindec(hex string, 16, 10)" to convert hexadecimal numbers into decimal numbers.

<?php
echo base_convert("1e", 16, 10) . "<br>";
echo base_convert("a", 16, 10) . "<br>";
echo base_convert("11ff", 16, 10) . "<br>";
echo base_convert("cceeff", 16, 10);
?>
Copy after login

What are the functions for converting numeric values ​​in php?

Description:

base_convert() function converts numbers between arbitrary bases. Syntax:

base_convert(number,frombase,tobase);
Copy after login
  • number Required. Specifies the number to be converted.​

  • #frombase Required. Specifies the original base of the number. Between 2 and 36 (inclusive). Numbers above decimal are represented by the letters a-z, such as a for 10, b for 11, and z for 35.​

  • #tobase Required. Specifies the base to be converted. Between 2 and 36 (inclusive). Numbers above decimal are represented by the letters a-z, such as a for 10, b for 11, and z for 35.

Recommended study: "

PHP Video Tutorial"

The above is the detailed content of What are the functions for converting numeric values ​​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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!