How to Convert an Integer to a String in PHP?

Coverting an number to a string in PHP

You can convert an integer to a string in PHP, in the following ways:

Casting to String

You can simply cast a integer to a string using the (string) cast, for example, like so:

$num = 12345;
$numericStr = (string) $num;

echo $numericStr; // '12345'
echo gettype($numericStr); // 'string'

If the variable holding the integer value can potentially be null, then it is converted to an empty string.

Using the strval() Function

The strval() can be used to return the string value of scalar types (such as an int):

$num = 12345;
$numericStr = strval($num);

echo $numericStr; // '12345'
echo gettype($numericStr); // 'string'

If the variable holding the integer value can potentially be null, then it is converted to an empty string.

You should consider casting to a string instead of using strval() as it is much faster and more readable.

Using String Interpolation

When you use double quotes or the heredoc syntax, variables are interpolated. This would automatically convert an integer to a string. For example:

$num = 12345;
$numericStr = "$num";

echo $numericStr; // '12345'
echo gettype($numericStr); // 'string'
$num = -12345;
$numericStr =<<<STR
$num
STR;

echo $numericStr; // '-12345'
echo gettype($numericStr); // 'string'

If the variable holding the integer value can potentially be null, then it is converted to an empty string.

Using string interpolation solely for the purpose of converting integers to strings might not be the best idea as it does not make the intention immediately clear. Perhaps, in that case, you could instead consider casting to a string instead.

Concatenating the Integer to a String

When an integer literal is used in a string concatenation, the integer value is automatically converted to a string:

$str = 'Hi, I am ' . 21 . ' years old';

echo $str; // 'Hi, I am 21 years old'
echo gettype($str); // 'string'

This means that you can use an empty string in a concatenation to convert an integer to a string:

$numericStr = 12345 . '';

echo $numericStr; // '12345'
echo gettype($numericStr); // 'string'
$numericStr = -12345 . '';

echo $numericStr; // '-12345'
echo gettype($numericStr); // 'string'

The empty string can be placed before or after the integer; the order of it does not matter:

$numericStr = '' . 12345;

echo $numericStr; // '12345'
echo gettype($numericStr); // 'string'
$numericStr = '' . -12345;

echo $numericStr; // '-12345'
echo gettype($numericStr); // 'string'

If the variable holding the integer value can potentially be null, then it is converted to an empty string.

In practice you should avoid using this method if you're using string concatenation solely for converting an integer to a string. This is because it makes the code not very readable and the intention (of integer to string conversion) is not immediately clear. Perhaps, in that case, you could instead consider casting to a string instead.


Hope you found this post useful. It was published . Please show your love and support by sharing this post.