Nadia S.
—How do I convert an integer to a string in PHP?
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
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.
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
Output:
string
Here is an example using the strval
function:
$myInteger = 42; $myString = strval($myInteger); echo gettype($myString); // check the data type
Output:
string
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);
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:
$myInteger = 9223372036854775807; echo number_format($myInteger, 2, '.', ",") . "\n";
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";
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.
Tasty treats for web developers brought to you by Sentry. Get tips and tricks from Wes Bos and Scott Tolinski.
SEE EPISODESConsidered “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.
Here’s a quick look at how Sentry handles your personal information (PII).
×We collect PII about people browsing our website, users of the Sentry service, prospective customers, and people who otherwise interact with us.
What if my PII is included in data sent to Sentry by a Sentry customer (e.g., someone using Sentry to monitor their app)? In this case you have to contact the Sentry customer (e.g., the maker of the app). We do not control the data that is sent to us through the Sentry service for the purposes of application monitoring.
Am I included?We may disclose your PII to the following type of recipients:
You may have the following rights related to your PII:
If you have any questions or concerns about your privacy at Sentry, please email us at compliance@sentry.io.
If you are a California resident, see our Supplemental notice.