Signup/Sign In
Ask Question
Not satisfied by the Answer? Still looking for a better solution?

Converting an integer to a string in PHP

Is there an approach to change an integer over to a string in PHP?
by

2 Answers

akshay1995
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";
sandhya6gczb
Here is an example of converting integer to string.

$str = (string) $int;
$str = "$int";

Login / Signup to Answer the Question.