String — Is series of characters, where a character is the same as a byte.

PHP only supports a 256-character set, and hence does not offer native Unicode support.

The string in PHP is implemented as an array of bytes and an integer indicating the length of the buffer.

String access and modification by character

// Get the first character of a string
$str = 'This is a test.';
$first = $str[0];

// Get the third character of a string
$third = $str[2];

// Get the last character of a string.
$str = 'This is still a test.';
$last = $str[strlen($str)-1];

// Modify the last character of a string
$str = 'Look at the sea';
$str[strlen($str)-1] = 'e';

  • Characters within strings may be accessed and modified by specifying the zero-based offset of the desired character after the string using square array brackets, as in $str[42].
  • Think of a string as an array of characters for this purpose.
  • As of PHP 7.1.0, negative string offsets are also supported. These specify the offset from the end of the 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.


A string literal can be specified in four different ways.

Variable parsing

When a string is specified in double quotes or with heredoc, variables are parsed within it.

There are two types of syntax: a simple one and a complex one.

String — Structure map

Clickable & Draggable!

String — Related pages: