ProAnswers.org

Converting an integer to a string in PHP

Is there a way to convert an integer to a string in PHP?

1 Like

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;
echo “I’d like {$var} waffles”; // = "I’d like 5 waffles

$var_to_string = mysql_escape_string($var);
$query = “SELECT * FROM table WHERE id=’{$var_to_string}’”;
$query = “SELECT * FROM table WHERE id=’”.mysql_escape_string($var)."’"; // using concat

$items = (string)$var; // $items == “5”;
$items = strval($var); // $items == “5”;

Two examples:

$str = (string) $int;
$str = “$int”;