# PHP Convert Integer to String with Casting and strval

> Convert integers to strings in PHP using string concatenation, (string) casting, or strval(), with formatting options via sprintf() and number_format()

**URL:** https://sentry.io/answers/converting-an-integer-to-a-string-in-php/

---

## 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:

```php
$myInteger = 42;
$myString = "" . $myInteger;

echo gettype($myString); // check the data type
```

Output:

```
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:

```php
$myInteger = 42;
$myString = (string)$myInteger;

echo gettype($myString); // check the data type
```

Output:

```
string
```

Here is an example using the `strval` function:

```php
$myInteger = 42;
$myString = strval($myInteger);

echo gettype($myString); // check the data type
```

Output:

```
string
```

### Formatting integers as strings

If you need to format your integer in a specific way, you can do this using the [`sprintf`](https://www.php.net/manual/en/function.sprintf.php) or [`number_format`](https://www.php.net/manual/en/function.number-format.php) functions.

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

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

Output:

```
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:

```php
$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`.

```php
$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:

```
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.

---

*Source: [sentry.io/answers/converting-an-integer-to-a-string-in-php/](https://sentry.io/answers/converting-an-integer-to-a-string-in-php/)*
