Convert an integer or a float to a string in PHP
This article demonstrates how to convert an integer or a float to a string in PHP.
1. Using Variable Interpolation
You can use variable interpolation to get the string representation of an integer or a float. PHP will parse the interpolated variables inside a double-quoted string, and replace them with their corresponding values. You can interpolate variables within a string literal with or without curly braces. Note that interpolating a variable into a single-quoted string will not work.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<?php $values = [ 0, 0.0, 1, 1.0, -1, -1.0, 1.5, -1.5, 1.50, -1.50 ]; foreach ($values as $value) { var_export($value); echo " => "; $result = "$value"; var_dump($result); } ?> |
This results in below output:
0 => string(1) "0"
0.0 => string(1) "0"
1 => string(1) "1"
1.0 => string(1) "1"
-1 => string(2) "-1"
-1.0 => string(2) "-1"
1.5 => string(3) "1.5"
-1.5 => string(4) "-1.5"
1.5 => string(3) "1.5"
-1.5 => string(4) "-1.5"
You may also use the Heredoc syntax, which behaves like a double-quoted string but without double quotes. In PHP, this looks like:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<?php $values = [ 0, 0.0, 1, 1.0, -1, -1.0, 1.5, -1.5, 1.50, -1.50 ]; foreach ($values as $value) { var_export($value); echo " => "; $result= <<<END $value END; var_dump($result); } ?> |
2. Using strval() function
PHP has a built-in function strval() which converts a value to a string. This function returns the string value of the specified variable. It can be used to convert a scalar type to a string, as follows:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<?php $values = [ 0, 0.0, 1, 1.0, -1, -1.0, 1.5, -1.5, 1.50, -1.50 ]; foreach ($values as $value) { var_export($value); echo " => "; $result = strval($value); var_dump($result); } ?> |
A more traditional way to convert a value to a string type is to explicitly cast it with the (string) cast. This works similarly to the strval() function. For example,
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<?php $values = [ 0, 0.0, 1, 1.0, -1, -1.0, 1.5, -1.5, 1.50, -1.50 ]; foreach ($values as $value) { var_export($value); echo " => "; $result = (string)$value; var_dump($result); } ?> |
3. Using Concatenation operator
Finally, you can force a variable to be evaluated as a string type using the concatenation operator (.). It performs the concatenation of its right and left operands. The following example shows how to concatenate a numeric value with an empty string ("").
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<?php $values = [ 0, 0.0, 1, 1.0, -1, -1.0, 1.5, -1.5, 1.50, -1.50 ]; foreach ($values as $value) { var_export($value); echo " => "; $result = "" . $value; var_dump($result); } ?> |
That’s all about converting an integer or a float to a string in PHP.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)