Viewed   93 times

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

 Answers

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";
Friday, September 23, 2022
4

CStr(45) is all you need (the Convert String function)

Sunday, August 7, 2022
 
2

Type casting is treating a value (block of memory) referenced by a variable as being of a different type than the type the variable is declared as.

Type conversion is actually performing a conversion of that value.

In many languages, some casts (usually numeric ones) do result in conversions (this will vary quite a bit by language), but mostly it's just "treat this X as a Y".

Like most aspects of human language, unfortunately the terms are used slightly differently in different communities, mostly along language lines. For instance, see James' comment below about C++ — the word "cast" there has a much broader meaning than the above definition, which is more in the C or Java mold. And just to make things fun, the Java Language Spec actually gets into various kinds of casts, including casting conversions. But the above is a good rule of thumb.

But to take a simple case:

In Java, prior to generics it wasn't unusual to have to do a lot of typecasting when dealing with maps:

Map m = new HashMap();
m.put("one", "uno");

// This would give a compiler error, because although we know
// we'll get a String back, all the compiler knows is that it's
// an Object
String italian = m.get("one");

// This works, we're telling the compiler "trust me, it's a String"
String italian = (String)m.get("one");

Fortunately, the addition of generics addressed this, as casting in this way tends to be a fragile process with maintenance issues.

In contrast, you'd convert if you had a String of digits:

String s = "1234";

...and needed to know what number those digits represented in decimal:

// Wrong (cast)
int n = (int)s;

// Right (conversion)
int n = Integer.parseInt(s, 10);
Friday, October 21, 2022
1

The following is probably not the neatest way, but it works:

1> lists:flatten(io_lib:format("~p", [35365])).
"35365"

EDIT: I've found that the following function comes in useful:

%% string_format/2
%% Like io:format except it returns the evaluated string rather than write
%% it to standard output.
%% Parameters:
%%   1. format string similar to that used by io:format.
%%   2. list of values to supply to format string.
%% Returns:
%%   Formatted string.
string_format(Pattern, Values) ->
    lists:flatten(io_lib:format(Pattern, Values)).

EDIT 2 (in response to comments): the above function came from a small program I wrote a while back to learn Erlang. I was looking for a string-formatting function and found the behaviour of io_lib:format/2 within erl counter-intuitive, for example:

1> io_lib:format("2 + 2 = ~p", [2+2]).
[50,32,43,32,50,32,61,32,"4"]

At the time, I was unaware of the 'auto-flattening' behaviour of output devices mentioned by @archaelus and so concluded that the above behaviour wasn't what I wanted.

This evening, I went back to this program and replaced calls to the string_format function above with io_lib:format. The only problems this caused were a few EUnit tests that failed because they were expecting a flattened string. These were easily fixed.

I agree with @gleber and @womble that using this function is overkill for converting an integer to a string. If that's all you need, use integer_to_list/1. KISS!

Monday, September 5, 2022
 
2
var_dump(0 == "string");

is doing a numeric (integer) comparison

0 is an integer, so "string" is converted to an integer to do the comparison, and equates to an integer value of 0, so 0 == 0 is true

Se the comparison with various types table in the PHP documentation for details

Sunday, September 11, 2022
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :