In PHP programming, sometimes we need to convert strings into numbers. Common situations include:
1. Convert the numeric string input by the user into a number for calculation, comparison and other operations.
2. Convert all string elements in an array into numbers.
3. Perform numerical operations, formatting and other operations on a string.
This article will introduce the mutual conversion method between strings and numbers in PHP to help readers better understand and deal with the conversion issues of strings and numbers.
1. Convert strings to numbers
In PHP, there are many ways to convert strings into numbers, including type conversion, function conversion, operator conversion, etc.
1. Type conversion
The most basic way to convert a string into a number is to use the type conversion operator. There are three type conversion operators in PHP, namely (int), (float), and (string), which convert variables into integer, floating-point, and string types respectively.
The syntax is as follows:
(int)$variable
(float)$variable
(string)$variable
The sample code is as follows:
$string_num = "123";
$int_num = (int) $string_num;
$float_num = (float) $string_num;
echo gettype($string_num); // Output string
echo gettype($int_num); // Output integer
echo gettype($float_num); // Output double
Run result:
string
integer
double
The type conversion operator can convert a string into a number, but you need to pay attention to the following points:
① If the string is a floating point number, using the (int) operator to convert will Convert it to the integer part, while the (float) operator conversion preserves the floating point form.
② If the string contains non-numeric characters, conversion using (int) and (float) operators will remove the non-numeric characters and return valid numbers.
③ If the string is an empty string or an illegal numeric string, 0 will be returned.
2. Function conversion
PHP provides multiple functions for converting strings into numbers, such as intval(), floatval(), number_format(), etc.
① intval(string $var [, int $base = 10]): Convert string to integer.
syntx: intval(string $var [, int $base = 10]).
The sample code is as follows:
$number = "123";
$integer = intval($number);
echo gettype($integer); // Output integer
operation result:
integer
② floatval(string $var): Convert a string into a floating point number.
syntax: floatval(string $var).
The sample code is as follows:
$number = "123.45";
$float = floatval($number);
echo gettype($float); // Output double
Run result:
double
③ number_format(float $num [, int $decimals = 0 [, string $dec_point = "." [, string $ thousands_sep = ","]]]): Format numbers.
syntax: number_format(float $num [, int $decimals = 0 [, string $dec_point = "." [, string $thousands_sep = ","]]]).
The sample code is as follows:
$number = "123456789.0123456";
$formatted_num = number_format($number, 2, ".", ",");
echo $formatted_num; // Output 123,456,789.01
Run result:
123,456,789.01
3. Operator conversion
During the operation, PHP will automatically Convert strings into numbers for calculations, such as addition, subtraction, multiplication, division, comparison, etc.
The sample code is as follows:
$x = "3";
$y = "6";
$sum = $x $y;
$minus = $x - $y;
$product = $x * $y;
$divide = $x / $y;
var_dump($sum); // Output int(9)
var_dump($minus); // Output int(-3)
var_dump($product); // Output int(18)
var_dump($divide); // Output float(0.5)
Run result:
int(9)
int(-3)
int(18)
float(0.5)
2. Convert numbers to characters String
The most basic way to convert a number into a string is to use the string concatenation operator (.) to concatenate the number and other strings together to form a new string.
The sample code is as follows:
$number = 123;
$string = "The number is " . $number;
echo gettype($string); / / Output string
Run result:
string
At the same time, PHP also provides multiple functions for converting numbers into strings, such as strval(), sprintf() wait.
1.strval(mixed $var): Convert variables into strings.
syntx:strval(mixed $var).
The sample code is as follows:
$number = 123;
$string = strval($number);
echo gettype($string); // Output string
Running results:
string
2.sprintf(string $format, mixed $var1 [, mixed $...]): Convert formatted strings and variables Combined into a new string.
syntax: sprintf(string $format, mixed $var1 [, mixed $...]).
The sample code is as follows:
$number1 = 123;
$number2 = 456;
$string = sprintf("The numbers are %d and %d", $number1 , $number2);
echo gettype($string); //Output string
Run result:
string
Summary
This article introduces the mutual conversion methods between strings and numbers in PHP, including type conversion, function conversion, operator conversion, etc. It should be noted that during the conversion process, the legality and validity of the data type need to be paid attention to to avoid conversion errors and calculation errors. In actual development, the most appropriate conversion method needs to be selected based on specific business needs.
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!