如何在PHP中将字符串转换为数字? [英] How do I convert a string to a number in PHP?

查看:175
本文介绍了如何在PHP中将字符串转换为数字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将这些类型的值'3''2.34''0.234343'等转换为数字.在JavaScript中,我们可以使用Number(),但是PHP中有可用的类似方法吗?

I want to convert these types of values, '3', '2.34', '0.234343', etc. to a number. In JavaScript we can use Number(), but is there any similar method available in PHP?

Input             Output
'2'               2
'2.34'            2.34
'0.3454545'       0.3454545

推荐答案

您通常不需要这样做,因为在大多数情况下,PHP都会为您强制类型.对于确实要明确转换类型的情况, 播放 它:

You don't typically need to do this, since PHP will coerce the type for you in most circumstances. For situations where you do want to explicitly convert the type, cast it:

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

这篇关于如何在PHP中将字符串转换为数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆