2

Possible Duplicate:
Converting an integer to a string in PHP

$variable = '2011'
$temp = tostring($variable);

How can I convert integer to string? Is any tostring() method available?

Community
  • 1
  • 1
starter
  • 21
  • 1
  • 4
  • http://stackoverflow.com/questions/1035634/converting-an-integer-to-a-string-in-php – Kaken Bok Jul 30 '11 at 22:56
  • `$variable` is *already* a string in your code since you declared it using single quotes (`'2011'`). There's no need to convert it. If instead you *were* using an int (i.e. no quotes), then this is a duplicate. – Rob Hruska Jul 30 '11 at 22:56
  • Do you mean converting 2011 to the string, "two thousand and eleven"? – Peter Ajtai Jul 30 '11 at 23:00

3 Answers3

4

PHP is dynamically typed, so your var is both a string and an integer

However, you can do type casting like so

$intval = (int) $string;
$string = (string) $intval;

or simply by saying

$string = "$intval";

But it shouldn't matter, because the string you make will still work as an integer

Flambino
  • 18,507
  • 2
  • 39
  • 58
2

Cast it to a string:

$temp=(string)$variable;

...but why do you need it? Much of PHP doesn't care about types and implicitly converts it whenever needed.

icktoofay
  • 126,289
  • 21
  • 250
  • 231
  • because i can't concat like that , $start_date = $get_year + ' ' + $get_month + ' ' + $date_01; – starter Jul 30 '11 at 23:01
  • The concatenation operator in PHP is `.`, not `+`. – Casey Chu Jul 30 '11 at 23:07
  • other language use + for concat , why does php use a dot(.) ? – starter Jul 30 '11 at 23:23
  • It was just a design decision. `+` was already taken for adding, and they chose to use rather weak typing, so they couldn't use `+` for concatenation. – icktoofay Jul 31 '11 at 00:00
  • can i change this design ? which file do i have to edit to change this design ? – starter Jul 31 '11 at 00:10
  • I suppose if you really wanted to dig into the PHP source you could change it, but then it would be so different from the normal PHP that it just wouldn't be right to call your modified version PHP. – icktoofay Jul 31 '11 at 01:54
1

As I've read your comments on recent answers, I can add the following:

$variable = '2011'; is already a string, but you need to concat it like this:

$start_date = $get_year . $get_month . $date_01;

Otherwise php will treat the "Stringed" digits as numbers, and just add up the values using the +-operator. Hope this solves your problem!

karllindmark
  • 6,031
  • 1
  • 26
  • 41
  • other language use + for concat , why does php use a dot(.) ? – starter Jul 30 '11 at 23:23
  • 1
    those other languages aren't dynamically typed, so they don't need to differentiate between . and + - if you *know* it's a string, you can use + to concatenate because it's typed as string. You don't know that in PHP. – Nick B Jul 30 '11 at 23:26
  • Pretty much what Nick wrote above. – karllindmark Jul 30 '11 at 23:36
  • The reason for PHP using . as the concat operator doesn't really matter. Just accept that it does and your question is answered. – vascowhite Jul 31 '11 at 00:09