Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
215 views
in Technique[技术] by (71.8m points)

php - 如何在PHP中将字符串转换为数字?(How do I convert a string to a number in PHP?)

I want to convert these types of values, '3' , '2.34' , '0.234343' , etc. to a number.

(我想将这些类型的值'3''2.34''0.234343'等转换为数字。)

In JavaScript we can use Number() , but is there any similar method available in PHP?

(在JavaScript中,我们可以使用Number() ,但是PHP中有可用的类似方法吗?)

Input             Output
'2'               2
'2.34'            2.34
'0.3454545'       0.3454545
  ask by Sara translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You don't typically need to do this, since PHP will coerce the type for you in most circumstances.

(通常不需要这样做,因为PHP在大多数情况下都会为您强制类型。)

For situations where you do want to explicitly convert the type, cast it:

(对于那些你想显式转换型的情况下, 它:)

$num = "3.14";
$int = (int)$num;
$float = (float)$num;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...