Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

A string in PHP is implemented as an array of bytes coupled with an integer indicating the length of the buffer. Because PHP does not dictate a specific character encoding at the core language level, a “character” in a standard PHP string is strictly equivalent to a single byte. Consequently, native string functions operate on byte values rather than Unicode code points. PHP provides four syntactical methods for defining string literals: 1. Single Quoted Single quotes create literal strings. Escape sequences (other than \' and \\) and variables are not evaluated or parsed.
$literal = 'This is a literal string.\n Variables like $foo are ignored.';
2. Double Quoted Double quotes instruct the PHP engine to parse the string for escape sequences (e.g., \n, \t, \x0F) and interpolate variables.
$foo = 'bar';
$parsed = "This string evaluates escape sequences\n and variables: $foo";
3. Heredoc Heredoc syntax behaves identically to double-quoted strings but is designed for multiline declarations without requiring escaping for internal quotes. It is initialized using <<< followed by an identifier.
$foo = 'bar';
$heredoc = <<<IDENTIFIER
This is a multiline string.
It parses variables like $foo and does not require escaping "quotes".
IDENTIFIER;
4. Nowdoc Nowdoc syntax behaves identically to single-quoted strings. It is initialized using <<< followed by an identifier enclosed in single quotes. No parsing or interpolation occurs.
$nowdoc = <<<'IDENTIFIER'
This is a literal multiline string.
Variables like $foo are not parsed.
IDENTIFIER;

Variable Interpolation

When using double quotes or Heredoc syntax, PHP supports two forms of variable interpolation:
  • Simple Syntax: The parser greedily matches a dollar sign ($) followed by a valid variable name.
  • Complex (Curly Brace) Syntax: Enclosing the variable in curly braces {$...} explicitly defines the boundary of the variable name, allowing for disambiguation and the evaluation of complex expressions like array indices or object properties.
$noun = 'apple';
$array = ['key' => 'data'];
$object = new stdClass();
$object->property = 'value';

// Simple syntax
$simple = "I ate an $noun.";

// Complex syntax (required to separate the variable from the literal 's')
$complex = "I ate two {$noun}s.";

// Complex syntax for arrays and objects
$dynamic = "Value: {$array['key']} and {$object->property}";

String Concatenation

PHP utilizes the dot (.) operator for string concatenation. The concatenating assignment operator (.=) appends the right-hand argument to the existing left-hand string variable, mutating it in place.
$prefix = "Byte";
$suffix = "Buffer";

// Concatenation operator
$combined = $prefix . $suffix; // "ByteBuffer"

// Concatenating assignment operator
$combined .= " Length"; // "ByteBuffer Length"

Byte Access and Mutation

Because PHP strings are zero-indexed byte arrays, individual bytes can be accessed and mutated using array offset syntax ([]).
$string = "PHP";
$firstByte = $string[0]; // 'P'

// Mutating a specific byte
$string[2] = 'X'; // $string is now "PHX"
Note: Modifying a string via byte offsets does not account for multibyte character encodings (like UTF-8). Mutating a byte within a multibyte character will result in a corrupted character payload.

Memory and Length Mechanics

The length of a string is determined by its byte count, not its character count. The native strlen() function returns the size of the internal byte array.
$ascii = "Hello";
echo strlen($ascii); // Outputs: 5

$utf8 = "Héllö";
echo strlen($utf8); // Outputs: 7 (due to multibyte characters)
To accurately measure or manipulate strings containing multibyte characters, developers must utilize the mbstring extension (e.g., mb_strlen(), mb_substr()), which parses the byte array according to a specified character encoding. On 64-bit builds of PHP, the maximum length of a string is bounded only by the system’s available memory and the memory_limit directive in php.ini. On 32-bit builds, the maximum string length is strictly 2GB (2,147,483,647 bytes).
Master PHP with Deep Grasping Methodology!Learn More