Converting an integer to a string in PHP

Nadia S.

The Problem

How do I convert an integer to a string in PHP?

The Solution

You can convert an integer to a string by concatenating it to a string. In this example, we concatenate the $myInteger to an empty string:

$myInteger = 42; $myString = "" . $myInteger; echo gettype($myString); // check the data type



This approach works because PHP allows type coercion, which means that PHP handles the conversion for you when the two different data types are concatenated. It is also the fastest way to convert an integer to a string in PHP.

Alternative ways to convert an integer to a string in PHP

If you specifically need to use typecasting or a function (for example, because you depend on type hints or you need to pass the conversion method as part of a callback function) then you can use (string)$myInteger or strval($myInteger).

Here is an example using casting:

$myInteger = 42; $myString = (string)$myInteger; echo gettype($myString); // check the data type



Here is an example using the strval function:

$myInteger = 42; $myString = strval($myInteger); echo gettype($myString); // check the data type



Formatting integers as strings

If you need to format your integer in a specific way, you can do this using the sprintf or number_format functions.

Here is an example using sprintf, which outputs our large number in scientific notation with two decimal points:

$myInteger = 9223372036854775807; $myString = sprintf("%.2e", $myInteger); echo($myString);



Here is an example using number_format, which lets us specify custom characters for both the decimal point and a thousands separator to make large numbers easier to read:

$myInteger = 9223372036854775807; echo number_format($myInteger, 2, '.', ",") . "\n";

Potential pitfalls with converting integers to strings in PHP

Note that for very large or very small numbers, PHP will automatically switch to using a double type instead of using integer. This can lead to some unexpected behavior when casting to string.

$myInteger = 9223372036854775807; echo gettype($myInteger) . "\n"; echo $myInteger . "\n"; echo number_format($myInteger, 2, '.', ",") . "\n"; echo "\n"; $myLargeInteger = 922337203685477580799999; echo gettype($myLargeInteger) . "\n"; echo $myLargeInteger . "\n"; echo number_format($myLargeInteger, 2, '.', ",") . "\n";


integer 9223372036854775807 9,223,372,036,854,775,808.00 double 9.2233720368548E+23 922,337,203,685,477,580,800,000.00

Note how PHP uses a double type for the second number, as it is too large for a standard integer, and rounds the value. In both the outputs, using echo or number_format, the number is converted into a string, and in each case it is rounded so that we lose some precision in the final digits.

Loved by over 4 million developers and more than 90,000 organizations worldwide, Sentry provides code-level observability to many of the world’s best-known companies like Disney, Peloton, Cloudflare, Eventbrite, Slack, Supercell, and Rockstar Games. Each month we process billions of exceptions from the most popular products on the internet.

Share on Twitter
Bookmark this page
Ask a questionJoin the discussion

Related Answers

A better experience for your users. An easier life for your developers.

© 2024 • Sentry is a registered Trademark
of Functional Software, Inc.