Converting an integer to a string in PHP The Quick Way

A common PHP developer question is, how do I convert an integer to a string in PHP? There are many ways this can be achieved, but first, let's explore the quickest way.

The quickest way to convert an (int) integer into a string is using PHP's ability to allow for type coercion. Type casting (type coercion) in PHP is a way data can change data type throughout its lifetime, i.e. start as an integer, and end as a string. That's because PHP (unlike other programming languages) does not require the type definition to be set when variables are declared.

In the following example, the variable "$number" starts as an integer, but if we require this variable's type to change during runtime to a string, in PHP we can concatenate a literal string to the start (or end) of the variable. When added, PHP will handle this for us, and convert our variable from an integer to a string automatically.

$number = 1;
var_dump("".$number);

# Outputs
string(1) "1"

However, although fast to perform, this isn't the only way nor the best way to convert strings or integers into other data types. Firstly, the readability of this isn't always clear, especially to new programmers. It might appear odd to append or prepend a blank string to a number, but luckily there's another (better) way, type casting.

Converting an integer to a string in PHP using Type Casting

In PHP, type casting allows us to convert the value of a given type using typed parentheses at the start of the value. Because we want to convert our integer into a string, we use "(string)". This will cast the number 1 which was an integer, to a string. This is much cleaner and easier to read than the previous example.

$number = 1;
var_dump((string) $number);

# Outputs
string(1) "1"

Alternative ways to convert an integer to a string in PHP

Type casting and string concatenation are two of many ways programmers convert integers to strings in PHP. Let's take our original example and check the type before converting it, using PHP's built-in function, "gettype". This will print the variable type to the screen. Taking a similar approach to our first example, instead of concatenating a string to the start or end of the variable, we could instead use the complex (curly) syntax (variable parsing). Complex curly syntax is a type of interpolation syntax in PHP, where a PHP variable is placed in curly braces. Because it's wrapped in a string, PHP will convert this variable for us.

$number = 1;
echo gettype($number);

# Outputs
integer

var_dump("{$number}");

# Outputs
string(1) "1"

Convert integers to string with strval function

Taking our same example, another way we can convert our integer to a string is using strval() function in PHP. This is a method of getting PHP to give us the string value of a variable, by attempting the conversion. It works because this function accepts any scalar type that implements the __toString() method.

var_dump(strval($number));

# Outputs
string(1) "1"

Formatting integers as strings

If you're looking to format integers as strings but not convert them per se, you can do so by using PHP's powerful "sprintf" function. The sprintf function returns a string based on the formatting string that was provided. The function accepts a lot of different types of specifiers, but you can see in our example below that our integer comes out as a string once past through sprintf.

$number = 1000000;
var_dump(sprintf("%d", $number));

# Outputs
string(7) "1000000"

The format of the number above can be converted another way by using the number_format function. Using this function is a great way to express large numbers to your end-users by making it easier to read as well as coming out as a string.

$number = 1000000;
var_dump(number_format($number));

# Outputs
string(9) "1,000,000"

When converting very large numbers (and very small ones) this way, it may lead to unexpected results due to how integers are signed on 32-bit and 64-bit computers. PHP automatically converts integers to floating-point numbers (doubles) when reaching this limit because the integer is outside the capable range. This means the inputting integer might not match the outputted string.

Here the type changes from an integer to a double once hitting the limit.

$number = 999999999;
echo gettype($number);

# Outputs
integer

$number = 99999999999999;
echo gettype($number);

# Outputs
double

Here the decimal point value has changed once parsed through the number_format function.

$number = 999999999999999.17543;
var_dump(number_format($number,2));

# Outputs
string(22) "999,999,999,999,999.12"

Senior PHP developer with near two decades of PHP experience. Author of Dev Lateral guides and tools. The complete place for PHP programmers. Available to hire to help you build or maintain your PHP application.

Looking for industry-leading PHP web development?

API development WordPress Hosting ★ and more 🐘

We use cookies to enhance your browsing experience and analyse website traffic in accordance with our Privacy and Cookie Policy. Our cookies, including those provided by third parties, collect anonymous information about website usage and may be used for targeted advertising purposes. By clicking "Reject non-essential" you can opt out of non-essential cookies. By clicking "Accept all" you agree to the use of all cookies.


Reject non-essential Accept all