Page 1 of 1

php array problem

Posted: Fri Aug 20, 2004 3:54 pm
by tmgs2002
I've been having problems with php arrays for a while. Unfortunately it seems to happen randomly so I don't have a clue what's causing it.

It goes something like this:

$category[0]="name1";
$category[1]="name2";

Now, if I do this:

echo $category[0];

It will simply print "n" (the first character of "name1")

If I change the array name to $category2 it will work fine!

As I said, it happens randomly, sometimes on my local version of PHP 4.2.3 running under Windows XP, sometimes on my remote webserver (Linux running PHP 4).

Also, the array name doesn't have to be $category. It has happened using other names aswell.

Any help would be much appreciated!

Posted: Fri Aug 20, 2004 4:03 pm
by Joe
When you say:

If I change the array name to $category2 it will work fine!

Then

Also, the array name doesn't have to be $category. It has happened using other names aswell.

How does that work out?. Are you sure it works using category2...

Posted: Fri Aug 20, 2004 4:17 pm
by markl999
The only way echo $category[0]; would display 'n' is if $category = 'name1'; (or some other string beginning with n), ie $category is a string and not an array. You could use var_dump() or is_array() to check what 'type' $category is.

Posted: Fri Aug 20, 2004 9:43 pm
by d3ad1ysp0rk
mark is right, if you declare:

Code: Select all

$catagory = "";
or something like it, it may cause this.

Try this exact code:

Code: Select all

<?php
$catagory[0] = "name1";
$catagory[1] = "name2";
echo $catagory[0];
?>