如何在 PHP 中将字符串转换为数字?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8529656/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 04:55:06  来源:igfitidea点击:

How do I convert a string to a number in PHP?

phpcastingtype-conversion

提问by Sara

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?

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

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

回答by deceze

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, castit:

您通常不需要这样做,因为在大多数情况下 PHP 会为您强制类型。对于那些你想显式转换型的情况下,它:

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

回答by fardjad

There are a few ways to do so:

有几种方法可以做到这一点:

  1. Cast the strings to numeric primitive data types:

    $num = (int) "10";
    $num = (double) "10.12"; // same as (float) "10.12";
    
  2. Perform math operations on the strings:

    $num = "10" + 1;
    $num = floor("10.1");
    
  3. Use intval()or floatval():

    $num = intval("10");
    $num = floatval("10.1");
    
  4. Use settype().

  1. 将字符串转换为数字原始数据类型:

    $num = (int) "10";
    $num = (double) "10.12"; // same as (float) "10.12";
    
  2. 对字符串执行数学运算:

    $num = "10" + 1;
    $num = floor("10.1");
    
  3. 使用intval()floatval()

    $num = intval("10");
    $num = floatval("10.1");
    
  4. 使用settype().

回答by gopeca

To avoid problems try intval($var). Some examples:

为避免出现问题,请尝试intval($var)。一些例子:

<?php
echo intval(42);                      // 42
echo intval(4.2);                     // 4
echo intval('42');                    // 42
echo intval('+42');                   // 42
echo intval('-42');                   // -42
echo intval(042);                     // 34 (octal as starts with zero)
echo intval('042');                   // 42
echo intval(1e10);                    // 1410065408
echo intval('1e10');                  // 1
echo intval(0x1A);                    // 26 (hex as starts with 0x)
echo intval(42000000);                // 42000000
echo intval(420000000000000000000);   // 0
echo intval('420000000000000000000'); // 2147483647
echo intval(42, 8);                   // 42
echo intval('42', 8);                 // 34
echo intval(array());                 // 0
echo intval(array('foo', 'bar'));     // 1
?>

回答by Your Common Sense

In whatever (loosely-typed) language you can always cast a string to a number by adding a zero to it.

在任何(松散类型的)语言中,您始终可以通过向字符串添加零来将字符串转换为数字。

However, there is very little sense in this as PHP will do it automatically at the time of using this variable, and it will be cast to a string anyway at the time of output.

但是,这没有什么意义,因为 PHP 会在使用此变量时自动执行此操作,并且在输出时无论如何都会将其转换为字符串。

Note that you may wish to keep dotted numbers as strings, because after casting to float it may be changed unpredictably, due to float numbers' nature.

请注意,您可能希望将虚线数字保留为字符串,因为在转换为浮点数之后,由于浮点数的性质,它可能会发生不可预测的更改。

回答by noobie-php

You can use:

您可以使用:

(int)(your value);

Or you can use:

或者你可以使用:

intval(string)

回答by Aleksei Akireikin

If you want get a float for $value = '0.4', but int for $value = '4', you can write:

如果你想得到一个 float for $value = '0.4',但 int for $value = '4',你可以写:

$number = ($value == (int) $value) ? (int) $value : (float) $value;

It is little bit dirty, but it works.

它有点脏,但它有效。

回答by webNeat

Instead of having to choose whether to convert the stringto intor float, you can simply add a 0to it, and PHP will automatically convert the result to a numeric type.

不必选择是否将 转换stringintfloat,您只需向其添加 a 0,PHP 会自动将结果转换为数字类型。

// Being sure the string is actually a number
if (is_numeric($string))
    $number = $string + 0;
else // Let the number be 0 if the string is not a number
    $number = 0;

回答by Boykodev

You can always add zero to it!

你总是可以给它加零!

Input             Output
'2' + 0           2 (int)
'2.34' + 0        2.34 (float)
'0.3454545' + 0   0.3454545 (float)

回答by aldemarcalazans

Yes, there is a similar method in PHP, but it is so little known that you will rarely hear about it. It is an arithmetic operator called "identity", as described here:

是的,PHP 中也有类似的方法,但它鲜为人知,您很少会听到它。它是一个称为“身份”的算术运算符,如下所述:

Aritmetic Operators

算术运算符

To convert a numeric string to a number, do as follows:

要将数字字符串转换为数字,请执行以下操作:

$a = +$a;

回答by Kashif Khan

In PHPyou can use intval(string) or floatval(string)functions to convert strings to numbers.

PHP 中,您可以使用intval(string) or floatval(string)函数将字符串转换为数字。