Number Parsing in PHP

Parsing numbers from strings is a basic but common task in many programs; here’s how to do it in PHP.

<?php

// PHP provides built-in functions for number parsing

// With floatval(), we can parse floating-point numbers
$f = floatval("1.234");
echo $f . "\n";

// For parsing integers, we can use intval()
$i = intval("123");
echo $i . "\n";

// intval() can also recognize hex-formatted numbers
$d = intval("0x1c8", 0);
echo $d . "\n";

// For parsing unsigned integers, we can use the same intval() function
$u = intval("789");
echo $u . "\n";

// There's no direct equivalent to Atoi in PHP, but we can use intval()
$k = intval("135");
echo $k . "\n";

// Parse functions in PHP typically return 0 on bad input, rather than an error
$e = intval("wat");
echo $e . "\n";

?>

To run the program, save it as number-parsing.php and use php:

$ php number-parsing.php
1.234
123
456
789
135
0

In PHP:

  1. We use floatval() to parse floating-point numbers. It doesn’t require specifying precision like in some other languages.

  2. intval() is used for parsing integers. It can handle different bases, including hexadecimal.

  3. There’s no separate function for parsing unsigned integers in PHP. intval() is used for both signed and unsigned integers.

  4. PHP doesn’t have a direct equivalent to Atoi, but intval() serves the same purpose for base-10 integer parsing.

  5. Unlike some other languages, PHP’s parsing functions typically return 0 for invalid input rather than throwing an error. You may need to use additional checks if you need to distinguish between a valid zero and an error condition.

Next, we’ll look at another common parsing task: URLs.

Markdown Formatted for Hugo:

Parsing numbers from strings is a basic but common task in many programs; here's how to do it in PHP.

<?php

// PHP provides built-in functions for number parsing

// With floatval(), we can parse floating-point numbers
$f = floatval("1.234");
echo $f . "\n";

// For parsing integers, we can use intval()
$i = intval("123");
echo $i . "\n";

// intval() can also recognize hex-formatted numbers
$d = intval("0x1c8", 0);
echo $d . "\n";

// For parsing unsigned integers, we can use the same intval() function
$u = intval("789");
echo $u . "\n";

// There's no direct equivalent to Atoi in PHP, but we can use intval()
$k = intval("135");
echo $k . "\n";

// Parse functions in PHP typically return 0 on bad input, rather than an error
$e = intval("wat");
echo $e . "\n";

?>
To run the program, save it as `number-parsing.php` and use `php`:
$ php number-parsing.php
1.234
123
456
789
135
0
In PHP: 1. We use `floatval()` to parse floating-point numbers. It doesn't require specifying precision like in some other languages. 2. `intval()` is used for parsing integers. It can handle different bases, including hexadecimal. 3. There's no separate function for parsing unsigned integers in PHP. `intval()` is used for both signed and unsigned integers. 4. PHP doesn't have a direct equivalent to `Atoi`, but `intval()` serves the same purpose for base-10 integer parsing. 5. Unlike some other languages, PHP's parsing functions typically return 0 for invalid input rather than throwing an error. You may need to use additional checks if you need to distinguish between a valid zero and an error condition. Next, we'll look at another common parsing task: URLs.