How to convert a string into a number using PHP
  John Mwaniki /   20 Dec 2021

How to convert a string into a number using PHP

In most programming languages, it is a requirement to specify the data type that a variable should hold before using the variable. These data types include char, string, int, double, float, array, boolean, etc.

Some programming languages are not so strict and thus you don't have to specify which data type it should store eg. Javascript and PHP.

For instance, in JavaScript, all you have to do is use the keywords var or let before the variable name.


var num1 = 25;
var num2 = 14;
var sum = num1 + num2;

In PHP, all you have to do is write a dollar sign $ followed by the variable name without having to specify the type.


$num1 = 25;
$num2 = 14;
$sum = $num1 + $num2;

However, by default, if the value of a variable is enclosed with quotes (single or double), it is considered as a string and in some languages, it will not give the desired results when used in arithmetic operations.

Example in Javascript


var num1 = "25";
var num2 = "14";
var sum = num1 + num2;
console.log(sum);

Output:

2514

As you can see, in Javascript the two variables are treated as strings and their values were concatenated instead of an addition to take place. So in Javascript, you must not define numeric variables with values enclosed within quotes. Else, you will have to first use the parseInt() to convert the string into integer type or parseFloat() to convert the string into a float before attempting to perform arithmetic operations.


var num1 = "25";
var num2 = "14";
var sum = parseInt(num1) + parseInt(num2);
console.log(sum);

Output:

39

In a similar way, PHP treats every value enclosed within quotes as a string. Let's compare two numbers, one enclosed in quotes, and the other without.

Example

<?php
$num1 = "20";
$num2 = 20;

if($num1 === $num2){
  echo "num1 and num2 are identical";
}
else{
  echo "num1 and num2 are not identical";
}

Output:

num1 and num2 are not identical

$num1 is considered a string while $num2 is considered an integer therefore not identical.

In PHP, we can check the data type of a variable using either the gettype() or the var_dump() function.

Example

<?php
$num1 = "20";
$num2 = 20;

echo "num1 is a ".gettype($num1);
echo "<br>";
echo "num2 is ".gettype($num2);

Output:

num1 is a string
num2 is integer

Or

<?php
$num1 = "20";
$num2 = 20;

echo var_dump($num1);
echo "<br>";
echo var_dump($num2);

Output:

string(2) "20"
int(20)

However, in PHP, unlike Javascript, you can add two numbers when one is of integer type and the other a string (or both are strings) without any problem.

Example

<?php
$num1 = "12";
$num2 = 18;

echo $num1 + $num2;

Output:

30

The output is of integer type.

While strings in PHP can be converted to numbers (ie, int, float, or double) very easily, in most cases it won’t be required since PHP automatically does implicit type conversion at the time of using variables.

However, there are some instances where it may not be a good idea to wait for PHP to do the conversion for you.

For example on a parameterized PDO query, there will be a struggle sometimes for the parser to realize it is a number and not a string, and then you end up with a 0 in an integer field because you did not cast the string to an int in the parameter step.

Another scenario is when you are sending data in JSON format via an API to an app that expects it to be a number, and that is built in a language that doesn't do implicit type conversion. This will result in an error.

Due to these, among other reasons and scenarios, you may want to do type conversion explicitly.

Methods of string to number conversion in PHP

There are multiple ways in which you can do explicit type conversion from string to number in PHP as explained below.

1. Using the settype() function

The settype() function is used to convert a variable to a specific data type.

Syntax

settype(variable, type);

Parameters

Parameter Requirement Description
variable Required Specifies the variable whose data type is to be converted.
type Required Specifies the data type in which the variable should be converted to. The possible types are: boolean, bool, integer, int, float, double, string, array, object, null

Example

<?php
$num1 = "22";
$num2 = "3.142";

settype($num1, "integer");
settype($num2, "double");

echo "The num1 type is ".gettype($num1);
echo "<br>";
echo "The num2 type is ".gettype($num2);

Output:

The num1 type is integer
The num2 type is double

2. Using the identity arithmetic operator

There is a little-known about and rarely used arithmetic operator in PHP called "identity", used for converting a numeric string to a number.

Syntax

$a = +$a;

All you have to do is place a plus sign + before the string numeric variable or value. It converts the number to a float and an int value.

Example

<?php
$num = +"47";
echo "The type is ".gettype($num);

Output:

The type is integer

3. Cast the strings to numeric primitive data types

Typecasting is the conversion of data from one data type to another data type. You can use (int) or (integer) to cast a numeric string to an integer, or use (float), (double), or (real) to cast a numeric string to float. Similarly, you can use the (string) to cast a variable to string, and so on.

Example

<?php
$num1 = (int)"35";
$num2 = (double) "3.142"; // same as (float) "3.142";

echo "The num1 type is ".gettype($num1);
echo "<br>";
echo "The num2 type is ".gettype($num2);

Output:

The num1 type is integer
The num2 type is double

4. Perform math operations on the strings

In PHP, performing mathematical operations converts a numeric string to an integer or float implicitly.

You can perform operations such as ceil(), round() or floor(). However, these will round the number up or down. If you don't want to change the value then do as below.

You can easily convert a string to a number by performing an arithmetic operation that won't change the value such as addition with a zero (0) or multiplication by one (1).

Example

<?php
$num1 = "35" * 1;
$num2 = "3.142" + 0;

echo "The num1 type is ".gettype($num1);
echo "<br>";
echo "The num2 type is ".gettype($num2);

Output:

The num1 type is integer
The num2 type is double

5. Use intval() or floatval()

These functions intval() and floatval() can also be used to convert a numeric string into its corresponding integer and float value respectively.

Example

<?php
$num1 = intval("10");
$num2 = floatval("3.142");

var_dump($num1);
echo "<br>";
var_dump($num2);

Output:

int(10)
float(3.142)

An advantage of this method is that you can convert multiple numeric strings in an array at once using the array_map() function which you can't do with the other methods.

Example

<?php
$array1 = array("23", "14", "33", "76", "29", "54");
$array2 = array("3.142", "10.12", "92.01", "25.98", "54.37");

$array1 = array_map("intval", $array1);
$array2 = array_map("floatval", $array2);
var_dump($array1);
echo "<br>";
var_dump($array2);

Output:

array(6) { [0]=> int(23) [1]=> int(14) [2]=> int(33) [3]=> int(76) [4]=> int(29) [5]=> int(54) }
array(5) { [0]=> float(3.142) [1]=> float(10.12) [2]=> float(92.01) [3]=> float(25.98) [4]=> float(54.37) }

That's all!

It's my hope that now you can comfortably do data conversion from strings to numbers explicitly in PHP.