Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting an integer to a string in PHP

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

like image 584
kman99 Avatar asked Jun 23 '09 22:06

kman99


People also ask

What is parseInt in PHP?

Definition and Usage The parseInt method parses a value as a string and returns the first integer. A radix parameter specifies the number system to use: 2 = binary, 8 = octal, 10 = decimal, 16 = hexadecimal. If radix is omitted, JavaScript assumes radix 10.

Can we convert array to string in PHP?

In PHP, the implode() function is a built-in function that takes an array and converts it to a string. implode() doesn't modify the original array. It doesn't matter whether the array is an indexed or associative array. Once you pass in the array to implode() , it joins all the values to a string.

What is Intval?

The intval() function returns the integer value of a variable.

What is the function Strval () supposed to do in a program?

The strval() function returns the string value of a variable.


1 Answers

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"; 
like image 84
Chris Thompson Avatar answered Oct 14 '22 18:10

Chris Thompson