String access and modification by character in PHP

Today the topic of the Italian PHP Mailing List was the following: String access and modification by character in PHP

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 like $str[42] so think of a string as an array of characters.

Note: They may also be accessed using braces like $str{42} for the same purpose. However, using square array-brackets is preferred because the {braces} style is deprecated as of PHP 6.

So Matteo B. posted this:

error_reporting(E_ALL|E_STRICT);
$var = 'Hello';
$var[1] = 'World';
var_dump($var);

The result is the following:

string(5) "HWllo"

We're all scared. EEK!

Rispondi

Il contenuto di questo campo è privato e non verrà mostrato pubblicamente.
©MMVIII Bologna Informatica - p.i. 0123456789