How To Convert A String To A Number In PHP?

Published October 11, 2024

Problem: Converting Strings to Numbers in PHP

Converting strings to numbers is a common task in PHP programming. It's important when working with user input or data from external sources that may be in string format but needs to be used as a numerical value for calculations or comparisons.

PHP's Built-in Type Conversion Methods

Using Type Casting in PHP

PHP offers type casting methods to convert strings to numbers. To convert a string to an integer, use the (int) cast. For floating-point numbers, use the (float) cast.

Examples:

$stringInteger = "42";
$integer = (int)$stringInteger;

$stringFloat = "3.14";
$float = (float)$stringFloat;

Tip: Handling Boolean Conversions

When casting to boolean, PHP considers certain values as false: empty strings, "0", null, false, and empty arrays. All other values are considered true.

The intval() Function for Integer Conversion

The intval() function converts a value to an integer. It converts strings to integers and can handle different string formats.

Syntax:

int intval(mixed $value, int $base = 10)

Usage examples:

$result1 = intval("42");    // Returns 42
$result2 = intval("42.8");  // Returns 42
$result3 = intval("0042");  // Returns 42
$result4 = intval("42abc"); // Returns 42

The floatval() Function for Float Conversion

The floatval() function converts a value to a floating-point number. It converts strings to floats and handles precision.

Syntax:

float floatval(mixed $value)

Usage examples:

$result1 = floatval("3.14");     // Returns 3.14
$result2 = floatval("3.14e2");   // Returns 314
$result3 = floatval("0.0314E+2"); // Returns 3.14

Note that floatval() may cause precision issues due to floating-point representation in computers.

Example: Handling Non-Numeric Strings

$result = floatval("abc123.45"); // Returns 0
$result = floatval("123abc45.67"); // Returns 123

In these cases, floatval() stops at the first non-numeric character or returns 0 if no valid number is found.

Practical Examples of String-to-Number Conversion

Converting Integer Strings

Converting integer strings to numbers in PHP is simple. Here are some examples with whole numbers:

$wholeNumber1 = (int)"123";
$wholeNumber2 = intval("456");

echo $wholeNumber1; // Outputs: 123
echo $wholeNumber2; // Outputs: 456

When handling leading zeros, PHP ignores them during conversion:

$leadingZeros1 = (int)"00042";
$leadingZeros2 = intval("007");

echo $leadingZeros1; // Outputs: 42
echo $leadingZeros2; // Outputs: 7

Tip: Handling Binary Strings

When converting binary string representations to integers, use the base parameter of intval():

$binaryString = "1010";
$decimalValue = intval($binaryString, 2);
echo $decimalValue; // Outputs: 10

Converting Decimal Strings

To convert decimal strings to floating-point numbers, you can use type casting or the floatval() function:

$decimalNumber1 = (float)"3.14";
$decimalNumber2 = floatval("2.718");

echo $decimalNumber1; // Outputs: 3.14
echo $decimalNumber2; // Outputs: 2.718

PHP can handle different decimal formats, including scientific notation:

$scientificNotation = floatval("6.022e23");
$commaDecimal = floatval("1,234.56");

echo $scientificNotation; // Outputs: 6.022E+23
echo $commaDecimal; // Outputs: 1234.56 (comma is ignored)

Note that when dealing with comma as a decimal separator, PHP ignores the comma and treats it as a regular decimal point. For accurate conversion of locale-specific number formats, you may need to use extra string manipulation before conversion.