PHP – Convert Integer to String
To convert an int value to a string value in PHP, you can use Type Casting technique or PHP built-in function strval().
In this tutorial, we will go through each of these methods and learn how to convert an integer value to a string value.
Method 1: Type Casting
To convert integer to string using Type Casting, provide the literal (string)
along with parenthesis before the integer value. This expression returns a string value. We can store it in a variable or use as a value in another expression.
The syntax to type cast integer to string is
$string_value = (string) $int_value;
In the following program, we take an integer value in $n
, convert this integer value to string value using type casting and store the result in $x
.
PHP Program
<?php
$n = 268;
$x = (string) $n;
echo "x : " . $x;
?>
Output
Method 2: strval()
To convert integer value to string value using PHP built-in function strval(), pass the integer value as argument to the function. The function returns a string value created from the given argument.
The syntax to use strval() to convert int to string is
$string_value = intval( $integer_value );
In the following program, we take an integer value in $n, and convert it to string using strval() function.
PHP Program
<?php
$n = 268;
$x = strval($n);
echo "x : " . $x;
?>
Output
Conclusion
In this PHP Tutorial, we learned how to convert an int value to a string value using type-casting or strval() function.