Home  >  Article  >  Backend Development  >  How to convert php string to number

How to convert php string to number

藏色散人
藏色散人Original
2021-02-08 09:21:225223browse

How to convert string numbers in php: 1. Use the intval() function to get the integer value of the string variable, the syntax is "intval($str)"; 2. Add the use before the string data Parenthesized target type "(int)" or "(integer)", syntax "(int)$str" or "(integer)$str".

How to convert php string to number

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

php Lieutenant General Method to convert string to number:

Method 1: Use intval() function

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.

Syntax:

int intval ( mixed $var [, int $base = 10 ] )

Parameter description:

  • $var: The quantity value to be converted to integer.

  • #$base: The base used for conversion.

If base is 0, the base used is determined by detecting the format of var:

  • If the string includes "0x" ( or "0X"), use hexadecimal (hex); otherwise,

  • If the string starts with "0", use octal (octal); otherwise,

  • Decimal will be used.

Return value: The integer value of var is returned on success, and 0 is returned on failure. An empty array returns 0, a non-empty array returns 1.

Example: Convert string to number

<?php 
header("Content-type:text/html;charset=utf-8");  
$num1=3.1415926;   
$num2=intval($num1); 
echo "浮点型转为int型:";
var_dump($num1); 
var_dump($num2); 

$str1="123d";   
$str2=intval($str1); 
echo "字符串型转为int型:";
var_dump($str1); 
var_dump($str2); 
?>

How to convert php string to number

Method 3: Add the target type enclosed in parentheses "( before the data int)" or "(integer)"

  • (int), (integer): Convert to integer type;

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

How to convert php string to number

Note:

Integer values ​​can be expressed in decimal, hexadecimal, octal or binary, and can be preceded by an optional symbol (- or). You can use the negative operator to represent a negative integer.

Binary expressed integer is available since PHP 5.4.0.

To use octal expression, 0 (zero) must be added before the number. To use hexadecimal representation, the number must be preceded by 0x. To use binary representation, the number must be preceded by 0b.

Starting from PHP 7.4.0, integer values ​​may contain underscores (_). For a better reading experience, these underscores will be filtered out by PHP when displayed.

(Recommended: "PHP Video Tutorial")

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