Home >Backend Development >PHP Problem >php string number conversion method

php string number conversion method

藏色散人
藏色散人Original
2020-11-27 09:42:552428browse

php string number conversion method: 1. Use the intval function to convert the string into an integer, such as "intval($sum);"; 2. Use the floatval function to convert the string into a floating point number. Statements such as "floatval($sum);".

php string number conversion method

Recommended: "PHP Video Tutorial"

The operating environment of this tutorial: Windows7 system, PHP7.3.3, the The method is applicable to all brands of computers.

php7.3.3-Conversion between strings and numbers

In usual PHP projects, we often use it to convert strings and numbers. , today I will introduce to you the methods of converting between them.

Convert strings to numbers. Here we use two functions of PHP, intval() and floatval();

<?php
$sum=&#39;99.99&#39;;
//利用intval函数将字符串转换为整数
echo intval($sum);
echo &#39;<hr/>&#39;;
//利用floatval函数将字符串转换为浮点数
echo floatval($sum);

Print results

99
99.99

To convert numbers to strings, here we can directly use PHP’s system function strval().

<?php
$int=99;
echo strval($int);

Print results

99

To summarize, intval() converts a string into a number, while the strval() function converts a number into a string.

The above is the detailed content of php string number conversion method. 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