php array problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
tmgs2002
Forum Newbie
Posts: 4
Joined: Fri Aug 20, 2004 3:54 pm

php array problem

Post 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!
User avatar
Joe
Forum Regular
Posts: 939
Joined: Sun Feb 29, 2004 1:26 pm
Location: UK - Glasgow

Post 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...
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post 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.
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Post 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];
?>
Post Reply