String: Converting to string

Converting to string

  • A value can be converted to a string using the (string) cast or the strval() function.
  • String conversion is automatically done in the scope of an expression where a string is needed.
  • A boolean TRUE value is converted to the string "1".
  • Boolean FALSE is converted to "" (the empty string).
  • An integer or float is converted to a string representing the number textually (including the exponent part for floats).
  • Floating point numbers can be converted using exponential notation (4.1E+6).
  • Arrays are always converted to the string "Array" because of this, echo and print can not by themselves show the contents of an array.
  • In order to convert objects to string magic method __toString must be used.
  • Resources are always converted to strings with the structure "Resource id #1", where 1 is the resource number assigned to the resource by PHP at runtime.
  • NULL is always converted to an empty string.
  • Most PHP values can also be converted to strings for permanent storage. This method is called serialization, and is performed by the serialize() function.

String: Converting to string — Structure map

Clickable & Draggable!

String: Converting to string — Related pages: