Icon
Icon
Icon
Icon
Icon
Icon
12:42 AM
0 comments


String offset syntax
Background
Sometimes, it is desirable to access a specific character from within a string.  The Zend
Engine 1.0 has no special notation for referring to such string offsets.  Instead, strings can
be referred to as arrays, and using array offset notation, one can access a specific
character within a string.
Need
There are three main reasons why specialized string offsets syntax would be a good
addition to the Zend Engine:
a)  There is an ambiguity in today’s sharing of array offsets syntax for both strings
and arrays. In code such as $str[0] = ‘a’; where $str is an empty string (due to
earlier PHP 4 and PHP 3 behavior) $str is treated as an array and therefore the
above statement would result in $str[0] being converted to an array and having its
offset 0 being set to the string “a”. However, some people would expect the result
to be $str as the string value “a”.
b) By introducing a specialized syntax for string offsets it will be possible to
somewhat optimize the run-time processing, as we will know at compile-time that
the user specifically means to use a string offset.
c)  Language wise it is much better if developers will be able to tell if the author
meant to use array offsets or string offsets in a code snippet.
Overview
The Zend Engine will feature a new syntax for accessing string offsets. Using the array
offsets syntax for string offsets will be deprecated initially by a run-time warning
(possibly E_NOTICE or maybe a possible E_STRICT). Functionality
The currently suggested syntax is as follows:

$str{2} = ‘a’;

An example of the new functionality:
$str1 = $str2 = “”;
$str1{0} = ‘a’;
$str2[0] = ‘a’;
The result will be $str1 being the string “a” and $str2 being array(0 => “a”).
Compatibility notes
Using the array offset syntax for string offsets will print a warning in order to allow for
people to migrate their scripts. Due to backwards compatibility problems it is doubtful if
we will ever be able to change the array offset syntax to raise an error if used in a string
context.
Dependencies of feature
No dependencies

If You Enjoyed This Post Please Take a Second To Share It.

You Might Also Like

Stay Connected With Free Updates

Subscribe via Email

teaser