How to Convert a String to a Number in PHP

How do I convert a string to a number in 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;

How to convert string to number in php?

You cast it to an integer.

(int) $Str;

http://php.net/manual/en/language.types.type-juggling.php#language.types.typecasting

To explicitly convert a value to integer, use either the (int) or (integer) casts. However, in most cases the cast is not needed, since a value will be automatically converted if an operator, function or control structure requires an integer argument. A value can also be converted to integer with the intval() function.

In php, how to convert string value into an int?

To convert string to int in PHP, you can use Type Casting method or PHP built-in function intval().

<?php
$string = "56";
$int = intval( $string );
echo $int;
?>

Convert a string to number in php

You can either cast as int or use intval():

 $num=(int)$_GET['idEdu'];

or

 $num=intval($_GET['idEdu']);

Casting should be 4 times as fast.

How to convert string number to int number using php

You should multiple with 10, not with 100, also you can use number_format()

$first_wtd_item_first_option =  str_replace( ',', '', $number);
$first_wtd_item_second_option = str_replace( ',', '', $number);
$first_wtd_item_third_option = str_replace( ',', '', $number);

$first_wtd_item_computation1 = $first_wtd_item_second_option + $first_wtd_item_third_option;
$first_wtd_item_computation2 = ($first_wtd_item_first_option / $first_wtd_item_computation1);
$first_wtd_item_computation3 = number_format((float) $first_wtd_item_computation2 * 10, 1, '.', '');
$first_wtd_item_final_result = $first_wtd_item_computation3.'%';

Result:

5.0%

Fastest way to convert string to integer in PHP

I've just set up a quick benchmarking exercise:

Function             time to run 1 million iterations
--------------------------------------------
(int) "123": 0.55029
intval("123"): 1.0115 (183%)

(int) "0": 0.42461
intval("0"): 0.95683 (225%)

(int) int: 0.1502
intval(int): 0.65716 (438%)

(int) array("a", "b"): 0.91264
intval(array("a", "b")): 1.47681 (162%)

(int) "hello": 0.42208
intval("hello"): 0.93678 (222%)

On average, calling intval() is two and a half times slower, and the difference is the greatest if your input already is an integer.

I'd be interested to know why though.


Update: I've run the tests again, this time with coercion (0 + $var)

| INPUT ($x)      |  (int) $x  |intval($x) |  0 + $x   |
|-----------------|------------|-----------|-----------|
| "123" | 0.51541 | 0.96924 | 0.33828 |
| "0" | 0.42723 | 0.97418 | 0.31353 |
| 123 | 0.15011 | 0.61690 | 0.15452 |
| array("a", "b") | 0.8893 | 1.45109 | err! |
| "hello" | 0.42618 | 0.88803 | 0.1691 |
|-----------------|------------|-----------|-----------|

Addendum: I've just come across a slightly unexpected behaviour which you should be aware of when choosing one of these methods:

$x = "11";
(int) $x; // int(11)
intval($x); // int(11)
$x + 0; // int(11)

$x = "0x11";
(int) $x; // int(0)
intval($x); // int(0)
$x + 0; // int(17) !

$x = "011";
(int) $x; // int(11)
intval($x); // int(11)
$x + 0; // int(11) (not 9)

Tested using PHP 5.3.1

PHP convert String number with whitespace to Float

Most likely you have a non visible character as the thousand separator character. It's more safe to filter all characters except digits and . when sanitizing the input string. This can be achieved with the preg_replace function.

Your script can be modified to something like this:

$a = "3 999.99";
$b = "1 500.11";

$aa = floatval(preg_replace('/[^0-9.]/', '', $a));
$bb = floatval(preg_replace('/[^0-9.]/', '', $b));

$c = $aa - $bb;
echo $aa . ' - ' . $bb . ' = ' . $c;

Additionally, it's a bit dangerous and non exact to use floats when handling money in particular. Please have a look at this repository which helps a lot with this problem: https://github.com/moneyphp/money



Related Topics



Leave a reply



Submit