Converting an integer to a string in PHP
Solution 1
You can use the strval()
function to convert a number to a string.
From a maintenance perspective its obvious what you are trying to do rather than some of the other more esoteric answers. Of course, it depends on your context.
$var = 5;
// Inline variable parsing
echo "I'd like {$var} waffles"; // = I'd like 5 waffles
// String concatenation
echo "I'd like ".$var." waffles"; // I'd like 5 waffles
// The two examples above have the same end value...
// ... And so do the two below
// Explicit cast
$items = (string)$var; // $items === "5";
// Function call
$items = strval($var); // $items === "5";
Solution 2
There's many ways to do this.
Two examples:
$str = (string) $int;
$str = "$int";
See the PHP Manual on Types Juggling for more.
Solution 3
$foo = 5;
$foo = $foo . "";
Now $foo
is a string.
But, you may want to get used to casting. As casting is the proper way to accomplish something of that sort:
$foo = 5;
$foo = (string)$foo;
Another way is to encapsulate in quotes:
$foo = 5;
$foo = "$foo"
Solution 4
There are a number of ways to "convert" an integer to a string in PHP.
The traditional computer science way would be to cast the variable as a string:
$int = 5;
$int_as_string = (string) $int;
echo $int . ' is a '. gettype($int) . "\n";
echo $int_as_string . ' is a ' . gettype($int_as_string) . "\n";
You could also take advantage of PHP's implicit type conversion and string interpolation:
$int = 5;
echo $int . ' is a '. gettype($int) . "\n";
$int_as_string = "$int";
echo $int_as_string . ' is a ' . gettype($int_as_string) . "\n";
$string_int = $int.'';
echo $int_as_string . ' is a ' . gettype($int_as_string) . "\n";
Finally, similar to the above, any function that accepts and returns a string could be used to convert and integer. Consider the following:
$int = 5;
echo $int . ' is a '. gettype($int) . "\n";
$int_as_string = trim($int);
echo $int_as_string . ' is a ' . gettype($int_as_string) . "\n";
I wouldn't recommend the final option, but I've seen code in the wild that relied on this behavior, so thought I'd pass it along.
Solution 5
Warning: the below answer is based on the wrong premise. Casting 0 number to string always returns string "0", making the code provided redundant.
All these answers are great, but they all return you an empty string if the value is zero.
Try the following:
$v = 0;
$s = (string)$v ? (string)$v : "0";
kman99
Updated on July 13, 2022Comments
-
kman99 almost 2 years
Is there a way to convert an integer to a string in PHP?
-
karim79 about 15 yearsSo incredibly strange that we both picked 5. Also, + is java.
-
jason about 15 yearsI think you're mixing up your concatenation syntax between languages there.
-
Frank Farmer about 15 yearsI'd lean toward casting, simply because it makes your intentions abundantly clear. Casting has one and only one purpose: to change types. The other examples may almost look like mistakes, in some contexts.
-
Chris Thompson about 15 yearsGood point. I threw in some mysql_escape_string functions in there to clean it up.
-
Admin over 13 years$foo = 5; $foo = "$foo" extremely waistfull for memory in PHP use ''.
-
Kzqai almost 13 yearsedited since mysql_escape_string is deprecated since it ignores charset.
-
Yash Kumar about 12 yearsHere is a live version - link
-
freaky over 11 yearsif we have those values, then will it be 55 or 10?
-
Chris Thompson over 11 years@freaky: $var + $var = 10, $var . $var = "55". "." is the string concatenation operator while "+" is the addition operator.
-
chacham15 over 9 years@Kzqai for anyone who looks at this example to try and avoid injection attacks, I'd say abandon your attempts at escaping strings and use prepared statements
-
Chris Thompson over 9 yearsAfter 5 years I finally removed the SQL example as it was unnecessary to answer the question and introduced confusion.
-
Kim Hogeling over 9 yearsHere is a benchmark, for the examples in the answer: leifw.wickland.net/2009/08/…
-
Admin about 9 yearsNote that the question specifically says 'converting an integer', not a float. :)
-
itsjavi about 8 years"in most cases PHP will decide whats good for you" amazing :,)
-
Elzo Valugi almost 8 yearsI don't think that PHP will coerce an integer to string all the time. It will do it for
strlen(12345);
but not for$x = 12345; echo $x[2];
All these casting functions are quite useful and lots of programmers are checking their types more and more. -
mindsupport over 7 yearsHow is about 0 as string ? $var =0;
-
danielson317 over 5 yearsWarning!! If you pass in certain numbers php will round them when converting to string:
strval(0.999999997)
returns1
. -
nintyfan over 5 yearsCorrect the mistake, please. The correct form should be: $s = (string)$v ? (string)$v : "0";
-
Peter Mortensen almost 5 yearsRe "string": literally or the type? Or something else? Can you make it more clear (by editing your answer)?
-
Kaushik shrimali almost 5 yearsThanks for the comment@ Peter Mortensen That is the return type "STRING" means your value should be converted as a string in a new variable. so that's why I edited string
-
Marwan Salim over 3 yearsThe
trim()
function removes whitespace and other predefined characters from both sides of a string Although it returns string it's a bad practice. It's better to usestrval()
ornumber_format()
perhaps. -
Lacek about 3 years
var_dump((string)0);
printsstring(1) "0"
on PHP 5.5.9 (Released: 6 Feb 2014) for me. Which version of PHP did you get "an empty string if the value is zero"? -
JEX over 2 yearsWhat do you mean by wasteful? ``` $foo = 5; $foo = "$foo"; ``` This code is just overwriting int value with string, of course, it's not the answer but what's wasteful?
-
ADJenks over 2 yearsCan anyone provide a reason to choose a cast over a function call or vice versa?
-
Admin over 2 yearsAs it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
-
Your Common Sense about 2 yearsIt doesn't seem like an answer but rather a question.