PHP only supports a 256-character set, and hence does not offer native Unicode support.
String access and modification by character
<?php // Get the first character of a string $str = 'This is a test.'; $first = $str; // Get the third character of a string $third = $str; // 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.
- 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.
- String: Syntax
- Syntax: Single quoted
- Syntax: Double quoted
- Syntax: Heredoc syntax
- String: Variable parsing
- Variable parsing: Simple syntax
- Variable parsing: Complex (curly) syntax
- String: String access and modification by character
- String: Converting to string
- array: Key casts
- Variables in PHP
- JS Data Types
- str.split and arr.join