PHP convert string to int | How to Convert a String to a Number in PHP

Summary: in this article, you will learn PHP convert string to int | How to Convert a String to a Number in PHP with Example? let’s understand in details.

Definition and Usage: PHP convert string to int

PHP is a weakly typed language. it implies that when initializing a variable in PHP, here, we need to PHP convert string to int(integer). furthermore, one does not require to declare the variable type. as you know that PHP implicitly declares a data type for your variable. It can save you from prospective type errors in code.

When we working with programming languages, it’s quite common to require to do things with numbers that are representing as strings. such as for example, performing the arithmetic operations, responding to a client request, and feeding the data to a database, etc. furthermore, even though PHP helps with implicit type conversion in some cases, it’s necessary to know about appropriate strategies which will facilitate type conversion.

In this article, we’ll cover the different method to convert a string to a number in PHP.

1. Background: Types of PHP Numbers

In PHP, Numbers can take four forms such as:

  • Integers
  • Floats
  • Infinity
  • NaN

Here, integers and floats represent a lot of usually used number formats in programming languages. On the other hand, Infinity and NaN aren’t as well-defined, not more used, and are more likely to be encountered in edge-cases. in this article, let understand these in more depth.

Integers

Integers are the numbers that don’t contain a decimal component or decimal part. they represent the set Z={..,-4,-3,-2,-1,0,1,2,3,4..}. in PHP, if you initialize a variable as a number that doesn’t have a decimal component or decimal part it takes the integer data type (unless it has a value greater than PHP_INT_MAX).
Furthermore, it can verify that if a variable is an integer by using the is_int() function, as given below.

boolean true
boolean false

PHP Floats

Float numbers are those numbers that contain a decimal component or decimal part, which are represented in an exponential form. Furthermore, these numbers encompass a higher range of numbers and take up more bytes per number, precise up to 14 decimal places. let’s see the examples of float numbers –
0.09, 3.29, 415.5, 6.0, 1.4e5, 4e30, etc.

.
Important Note: arithmetic operations in PHP performed between a float number and an integer, they always return a float number (even if the returned number doesn’t need a decimal part).
Furthermore, you can verify that if a variable is a float number by using the is_float() function, as given below.

boolean true
boolean false

PHP INF (Infinity)

In programming languages, ‘INF’ in PHP stands for infinity. it’s usually used to represent any number that’s greater than the maximum possible float value. INF is commonly encountered any time you happen to divide an integer(int) or float number by zero.

INF may also be within the negative form, which might be encountered when you perform an operation like log(0). You can verify if a variable is INF by using the is_infinite() function or is_finite() function as given below.

PHP NaN

NaN stands for ‘Not a Number’ in PHP. It represents outputs of mathematical operations that can’t be defined. as an example, the arc cosine of x, i.e. acos(x) is undefined for x < 1 and x > 1 . You can verify it, when if a variable is NaN by using the is_nan() function as shown given below.

2. Formatting Number Strings Using number_format()

how we can format number strings (numbers stored as strings). Some articles on the internet incorrectly claim that the PHP number_format() function can be used to convert a string to a number. This is not true.

The PHP number_format() function can be used to format numbers that are stored in the form of strings – by adding commas to separate between thousands and/or specifying the number of decimal places. furthermore, this function always returns a formatted string (not a number variable) and can be used as given below –

number_format(string_number, n_decimal_places , decimal_point_symbol, separator_symbol)
.
Important Note: In this syntax, here, all arguments except the first one (string_number) are optional.

Let’s us see some examples.

'51,200' (length=6)
'512,000,000' (length=11)
'51,200,000.00' (length=13)
'51,200.52' (length=9)

3. Convert a String to a Number Using intval() or floatval() function

If you want to convert a PHP string to a number, then we can also use the intval() or floatval() function. There are few below examples of intval()’s usage.

24
24
24
24
2480
20000

intval() function can also be used to convert strings from hexadecimal (i.e. base 16) and octal (i.e. base 8). furthermore, the number systems to integers of decimal representations (base 10). The intval() function can even use a 2nd parameter that specifies the base for conversion. The default base value is 10, to represent the decimal.

Apart from string to integer conversion, intval() function can also be used to convert strings of float numbers to integers.
Similarly, in the place of intval(), you can use floatval() function to convert to float numbers.

21
21
21.4
21.8
21.8
20

4. Convert a String to a Number Using Mathematical Operations

If you have a number that’s initialized as a string in your code, PHP allows or permits you to perform arithmetic operations directly on its string variable. This implies that PHP implicitly performs the type conversion in order that your code doesn’t raise an error. These are seemingly some of the benefits of weakly (or loosely) typed languages such as PHP.

Let’s see however we are able to leverage this implicit type conversion to our advantage.

Therefore, as you can be seen above, that even though we started with a string variable, a trivial arithmetic operation has implicitly converted it to an integer. Agreed, this isn’t the most elegant approach to convert a string to a number, however in several cases, it can still save you from an explicit type conversion.

5. Convert a String to a Number Using Type Casting

If you want to convert a PHP string to a number, then you can perform type casting using (int) or (float) keywords as given below.

Similarly, you can also cast PHP strings to float values by using (float).

Using (int), we can also convert PHP strings that represent float numbers (eg. “11.67”, “21.18”) directly to integers. This operation floors the float number (eg. “21.18”) down to its nearest integer (21). For example –

6. Conclusion: PHP convert string to int

In this article, you have learned different types of numbers in PHP – such as integers, float numbers, INF (infinity), and NaN. We also learned how we can convert PHP strings into numbers using different methods – by number_format() function, using typecasting, using intval() or floatval() ways, and also by implicit conversion using mathematical operations. Now that you know well about numbers in PHP, regarding the way to convert strings to numbers, and about number string formatting.
Stay healthy, stay safe! Happy coding! enjoy it!