Sentry Answers>PHP>

Converting an integer to a string in PHP

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:

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

Output:

Click to Copy
string

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:

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

Output:

Click to Copy
string

Here is an example using the strval function:

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

Output:

Click to Copy
string

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:

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

Output:

Click to Copy
9.22e+18

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:

Click to Copy
$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.

Click to Copy
$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";

Output:

Click to Copy
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.

  • Syntax.fm logo
    Listen to the Syntax Podcast

    Tasty treats for web developers brought to you by Sentry. Get tips and tricks from Wes Bos and Scott Tolinski.

    SEE EPISODES

Considered “not bad” by 4 million developers and more than 100,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.

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