$str[0] vs substr(): When to Use Array Syntax on Strings
You aren't alone in noticing that $str[0] works for strings, even though it looks like array syntax. In PHP, this isn't a bug or an undocumented feature; it's a deliberate design choice rooted in how the language handles data types at a fundamental level. However, whether you should use it depends on what you value: brevity and convenience, or explicitness and clarity.
The Mechanics of Mixed Syntax
PHP is dynamically typed and loosely typed, which means it doesn't enforce strict boundaries between data structures until runtime. When you write $str[0], the engine checks if $str is an array or a string. If it's a string, PHP treats the integer index as an offset into the character sequence.
This behavior was introduced in PHP 4 and has persisted ever since. Under the hood, accessing a string via an integer index invokes the internal C function zend_string_fetch_char. This returns a single-byte character. If you attempt to access an out-of-bounds index, PHP throws a ValueError (in newer versions) or triggers a warning in older versions, effectively preventing silent errors that might occur with arrays if you aren't careful.
The syntax $str{0} is also valid but follows a different path. Curly braces denote curly-bracket access, which is technically reserved for accessing properties of objects. However, PHP allows this syntax on strings as well, and it resolves to the same internal function call. The difference is mostly syntactic sugar; there is no performance penalty or functional difference between square brackets and curly braces in this specific context.
Performance Implications
One of the most common debates surrounding $str[0] vs substr() is performance. In strict high-performance scenarios, every millisecond counts. Here, substr() is often cited as being faster.
When you use $str[0], PHP performs a bounds check and then fetches the character. When you use substr($str, 0, 1), PHP creates a new string object containing exactly one character. The overhead of creating that new string object adds to the execution time. If you are processing millions of strings in a tight loop where memory allocation is expensive, substr() might actually be slower due to the garbage collection pressure of creating new string instances for every iteration. Conversely, if you need the result as a standalone string variable later, substr() ensures type consistency, whereas $str[0] returns a single character which, while effectively a string, is technically fetched differently.
Readability and Intent
The strongest argument against using $str[0] lies in code readability. For a developer scanning your codebase, seeing $str[0] immediately signals "this is an array." It forces them to pause and mentally check: "Is this an array or a string?" If it's a string, they might wonder why you aren't using substr(), which explicitly communicates the intent to extract a substring.
Using substr($str, 0, 1) leaves no room for ambiguity. It clearly states that you are operating on text and manipulating a portion of it. This aligns with the principle of explicit over implicit. If your codebase relies heavily on conventions, introducing array-style indexing on strings can create friction for team members who expect strict type semantics.
There is a specific edge case to consider: multi-byte character sets like UTF-8. The $str[0] syntax and substr() both operate on bytes, not characters. If your string contains an emoji or a Chinese character that spans two bytes, $str[0] will return the first byte of that character, which is often an invalid character on its own. In such cases, you should probably be using mb_substr() instead of either method.
When to Choose What
If you are writing a micro-optimization for a tight loop where memory allocation is the primary bottleneck and you only need the first byte, $str[0] is acceptable. It is concise and widely supported. However, if you are building public APIs or working with strings that might contain multi-byte characters, substr() (or better yet, mb_substr()) is the safer bet.
Ultimately, the choice comes down to your project's style guide. If you prioritize explicitness and want your code to speak clearly about its intent, stick with substr(). If you are comfortable with PHP's loose typing and value brevity in internal scripts, $str[0] remains a valid tool in your arsenal.