Current Location: Home> Latest Articles> PHP String Conversion: How to Convert Integer (int) to String

PHP String Conversion: How to Convert Integer (int) to String

M66 2025-07-13

PHP String Handling: How to Convert Integer (int) to String

PHP, as a popular web development language, provides powerful string handling features. During development, developers often need to convert between different data types. This article focuses on explaining how to convert an integer (int) to a string in PHP, along with practical code examples.

Type Casting

In PHP, one of the most common ways to convert an int to a string is by using type casting. You can either use (string) or the strval() function. Here’s an example:

$intNum = 123;

$strNum = (string)$intNum;

echo $strNum;

You can also use the strval() function for the same result:

$intNum = 456;

$strNum = strval($intNum);

echo $strNum;

String Concatenation

Another common way to convert an int to a string is by using string concatenation. The approach is to add an empty string to the integer. Here’s an example:

$intNum = 789;

$strNum = $intNum . '';

echo $strNum;

Using the sprintf() Function

The sprintf() function in PHP can format a string and can also be used to convert an integer to a string. Here’s an example:

$intNum = 101112;

$strNum = sprintf('%d', $intNum);

echo $strNum;

Conclusion

In PHP, converting an integer (int) to a string is a simple task. Depending on your needs, you can choose different methods such as type casting, the strval() function, string concatenation, or the sprintf() function. Each method provides an efficient way to perform the conversion and helps developers improve their coding efficiency.

We hope this article helps you understand and implement these basic type conversion techniques in PHP, making you more proficient in your development work.