Convert Integer to String in PHP
5 Methods with Examples
In PHP, converting an integer to a string is often required when you are working with different types of data like numbers and strings, and you want to convert the data between the types. This tutorial walks you through various ways to perform this conversion — from simple to advanced — so that you're not just memorizing methods, but truly understanding how and why they work.
Converting Integer to String
Before diving into methods, let’s understand the why. You may want to convert an integer to a string in PHP when:
- You need to concatenate an integer with other strings.
- You’re preparing data for JSON or API responses.
- You're performing type-specific operations where string format is required.
Method 1: Using strval()
Function
The strval()
function is the most straightforward way to convert an integer into a string in PHP.
<?php
$number = 123;
$string = strval($number);
echo gettype($string);
echo "\n";
echo $string;
?>
string
123
Explanation
strval()
directly converts the integer to a string. gettype()
confirms the type after conversion. This method is clean, readable, and preferred for general use.
Method 2: Type Casting (Explicit Conversion)
PHP also supports manual or explicit type casting. You can force a type conversion using (string)
.
<?php
$number = 456;
$string = (string)$number;
echo gettype($string);
echo "\n";
echo $string;
?>
string
456
Explanation
This method is handy when you want complete control over type conversion. The syntax may seem technical but it's widely used by experienced PHP developers.
Method 3: String Concatenation
You can force PHP to treat an integer as a string by appending it to an empty string using the dot operator (.
).
<?php
$number = 789;
$string = $number . "";
echo gettype($string);
echo "\n";
echo $string;
?>
string
789
Explanation
This is a creative and idiomatic way of converting an integer to a string. While it may look like a hack, it's perfectly valid and commonly used in templating and display logic.
Method 4: Using sprintf()
Function
sprintf()
offers formatted output, and can be used to convert integers into strings with specific formatting.
<?php
$number = 100;
$string = sprintf("%d", $number);
echo gettype($string);
echo "\n";
echo $string;
?>
string
100
Explanation
sprintf()
is especially useful when you need to format numbers before converting them. You can control padding, decimal places, and other format rules.
Method 5: Using json_encode()
Though it’s not the primary use-case, json_encode()
can convert integers to strings in a JSON-safe way.
<?php
$number = 321;
$string = json_encode($number);
echo gettype($string);
echo "\n";
echo $string;
?>
string
321
Explanation
While json_encode()
is typically used for arrays and objects, it does convert individual values too. This approach is useful when dealing with data interchange formats like JSON.
Best Practices
- Use
strval()
or type casting for readability and simplicity. - Use
sprintf()
when formatting is required. - Prefer concatenation only when used inline or in templates.
Advanced Tip: Check if a Variable is an Integer Before Converting
<?php
$input = 200;
if (is_int($input)) {
$string = strval($input);
echo "Converted: " . $string;
} else {
echo "Input is not an integer.";
}
?>
Converted: 200
Conclusion
Whether you need clean code, formatting control, or inline tricks — PHP gives you multiple ways to convert integers to strings.