Reputation: 3
<?php
$test = 'text1';
$test["02"] = "Hello";
var_dump($test);
Output is:: string(5) "teHt1"
Upvotes: 0
Views: 207
Reputation: 36
in the first line $test stored the value as 'text1' start giving offset value for the string from left to right from 0 to (stringlength-1) and each character byte size in string will occupies one byte of memory.
ie,
...
$test[0] =>'t';
$test[1] =>'e';
$test[2]=>'x';
$test[3]=>'t';
$test[4]=> '1';
...
so, $test[2] current value as 'x' but we are tried to override in the line $test["02"] = "Hello"; but its memory size just one byte so it stored only 'H' from the string in 'Hello'
that's why when you dump the variable $test value is now "teHt1";
Upvotes: 2
Reputation: 89
Strings are not arrays even you can use bracket notation.you are trying to replace second byte offset of the $test variable.So it takes the first byte offset from "Hello" and replaces x with H.
Upvotes: 0