> ## 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.

# PHP string

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.

```php theme={"dark"}
$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.

```php theme={"dark"}
$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.

```php theme={"dark"}
$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.

```php theme={"dark"}
$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.

```php theme={"dark"}
$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.

```php theme={"dark"}
$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 (`[]`).

```php theme={"dark"}
$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.

```php theme={"dark"}
$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).

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor PHP Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
