# Strange hidden string to array conversion

#### Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

•  Subject
• Author
• Posted on
Hello,

any of You ever faced this?

\$s = '';     // \$s is a string
\$s[4] = 'A'; // after this, surprisingly \$s is an ARRAY = ([4] => 'A')

but, if You take non-empty string:

\$s = 'x';    // the only difference is that we take non-empty string
\$s[4] = 'A'  // \$s is still a string, as supposed to be (== 'x   A')

It seams, that conversion from string to array is done after accessing
by index only EMPTY string by index. Anybody know, why, and how to avoid it?

ScriptKid

## Re: Strange hidden string to array conversion

If \$s[4] is now accessing the 4th character now? If so this reminds me
of the old atari 800 basic and how it handled string dimensions :)

Bill H

## Re: Strange hidden string to array conversion

Bill H pisze:

No, in fact it's 5th character, as arrays in PHP are 0-biased. Anybody

ScriptKid

## Re: Strange hidden string to array conversion

But why do you even want to do that ? :)
Before using arrays you should always declare them first:

\$s = array();
\$s[4] = 'A';

Kind regards,
Mathew

## Re: Strange hidden string to array conversion

Mathieu, that's the problem: I do not want to use ARRAYS!

I just want my empty STRING to remain being STRING, instead of being
magically converted to that ARRAY, after adding some characters! :(

## Re: Strange hidden string to array conversion

ScriptKid wrote:

So what are you trying to achieve with \$s[4] = 'A'?

AFAIK, doing that will create an array, and set the fifth element to 'A'.

## Re: Strange hidden string to array conversion

Hugh Oxford pisze:

No, I'm trying to achieve

\$s == '   A';

by writing:

\$s = '';
\$s [4] = 'A';

which should be possible according to documentation, but as Curtis wrote
(thanks, Curtis!) array conversion is chosen over string padding.

## Re: Strange hidden string to array conversion

ScriptKid wrote:

I don't see where the documentation says you can do that.  The doc
specifies "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, as in \$str[42]. ".

So you can access characters in the string, but it says nothing about
being able to access characters which don't exist.

If you want it equal to '   A', then set it to '   A'.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

## Re: Strange hidden string to array conversion

Jerry Stuckle pisze:

Well Jerry,

If You just read next two small paragraphs You will find there:

"Writing to an out of range offset pads the string with spaces."

according to this:

\$s = '';
\$s [4] = 'A";

\$s SHOULD be: '    A', but is not.

This was just an example. In real code, I need empty string
initialization in one place, and assigning character to this string to
offset out of range hundreds lines of code away, in the derived class,
so simple \$s = '    A' doesn't work there.

But thanks anyway. I just wanted to point out strange, not-so-evident
type conversion appeared here.

ScriptKid

## Re: Strange hidden string to array conversion

On Sat, 03 Jan 2009 01:35:59 +0100, no@spam.pl wrote:

PHP is dynamically and weakly typed, so the empty string is
implicitly equal to an empty array.  In this case, associative array
assignment is always assumed.

Try:

<?php
\$s = str_repeat(' ', 4) . 'A';
?>

--
Curtis
\$email = str_replace('sig.invalid', 'gmail.com', \$from);

## Re: Strange hidden string to array conversion

ScriptKid escribió:

The manual says: "The behaviour of an automatic conversion to array is
currently undefined". See the first note here:

http://es.php.net/manual/en/language.types.type-juggling.php

Since the behaviour is not defined it might change randomly in future
PHP versions. I'd say it's better to pad the string manually: