$s = 'abc';
$s[0] = '';
if ($s[0] !== '') {
echo 'emmm';
}
Why $string[0] = '' is not resulting $string[0] === ''?
seems like $string[0] === "\0", but more confusing..
$s = 'abc';
$s[0] = '';
if ($s[0] !== '') {
echo 'emmm';
}
Why $string[0] = '' is not resulting $string[0] === ''?
seems like $string[0] === "\0", but more confusing..
$s[0]
gives access to a single character of the string. ''
is not a character, it's a string, so the assignment fails. You get the exact same behavior if you try to assign FALSE
to $s[0]
. It looks like PHP does its best to convert whatever value you assign into a single character. If you assign a non-empty string, it will use the first character. If you assign a number, it will use the first digit. If you assign the value TRUE
, it will use 1
.