Closed Thread Icon

Topic awaiting preservation: PHP,Arrays Pages that link to <a href="https://ozoneasylum.com/backlink?for=12554" title="Pages that link to Topic awaiting preservation: PHP,Arrays" rel="nofollow" >Topic awaiting preservation: PHP,Arrays\

 
Author Thread
Trigger
Paranoid (IV) Inmate

From:
Insane since: Jun 2002

posted posted 12-14-2002 22:37

Why is it.
if you have a array called
say
$animals and you
echo or print $animals you get the word Array in your brwoser?
surley logicaly it would print out the contents of the"Array" not tell you it's an array
anyway. my second question is, is there any "simple" way to
display the complete contents of an array? in 2 lines or less?
Thanks
Trigger

DL-44
Maniac (V) Inmate

From: under the bed
Insane since: Feb 2000

posted posted 12-14-2002 22:47

answer 1 - because you declare it as $animals = array

so if you echo $animals...you get 'array'

answer 2 - print_r() is what you need.

print_r($animals);

would print out the contents of $animals.



[This message has been edited by DL-44 (edited 12-14-2002).]

butcher
Paranoid (IV) Inmate

From: New Jersey, USA
Insane since: Oct 2000

posted posted 12-14-2002 23:02

Cool!!

Thanks DL, I can't believe I haven't come across that one before. That will be real handy for debugging instead of writting a quick loop to output everything.

-Butcher-

Perfect Thunder
Paranoid (IV) Inmate

From: Milwaukee
Insane since: Oct 2001

posted posted 12-15-2002 00:46

Bear in mind that if you typed

$animals = array;

Then you just created a normal variable with the value "array".

$animals = array();

calls the function array(), which initializes $animals as an empty array. Do that, and "echo $animals" ought to display the value at the current array pointer -- in this case, nothing.

DL-44
Maniac (V) Inmate

From: under the bed
Insane since: Feb 2000

posted posted 12-15-2002 02:06

PT - setting an array (an actual array) and then echoing the name will in fact output the word "Array".



[This message has been edited by DL-44 (edited 12-15-2002).]

Perfect Thunder
Paranoid (IV) Inmate

From: Milwaukee
Insane since: Oct 2001

posted posted 12-15-2002 02:56

Really? Weird. Just goes to show that if you've got an array that might be empty, check for content before outputting it. Thanks for the tip.

DL-44
Maniac (V) Inmate

From: under the bed
Insane since: Feb 2000

posted posted 12-15-2002 03:28

Yep, I was quite perterbed the first couple times I saw "Array" all by it's lonesome on my page =)

Being quite new to all this, I was unsure how to check certain things - print_r was a happy find, as was 'array_keys()'

=)



Perfect Thunder
Paranoid (IV) Inmate

From: Milwaukee
Insane since: Oct 2001

posted posted 12-15-2002 03:59

Well, I can't imagine a situation where I'd want to try echoing an array directly... I always loop through the elements with a for, while, or foreach. And yeah, if I suspect the array might be empty, I check with something like "if (count($variable) > 0)".

DL-44
Maniac (V) Inmate

From: under the bed
Insane since: Feb 2000

posted posted 12-15-2002 05:54

at that time I was just looking for a simple error check...not knowing what I was doing I gave the seemingly simplest method a try 1st

Then I went to RTFM =)

{edit - spelling}




[This message has been edited by DL-44 (edited 12-15-2002).]

Trigger
Paranoid (IV) Inmate

From:
Insane since: Jun 2002

posted posted 12-15-2002 12:18

ahh ok.. well the deal is im writeing a
PHP for idiots guide.. as I learn php i add to the tutorial what I've learnt in the simplet
terms possible,
because well once i've finshed i'll be able to read it and complety understand it all
it helps me learning things
its located at http://www.the-bronze.net/trig/php.txt
anyway thanks DL-44


so theres no way to just print out
dog cat fish hamster
if the array was
$animals = array(dog,cat,fish,hamster); ???

Thanks
Trigger

[EDIT: and why is it people seem to use $animals / $animal as a array example??]


[This message has been edited by Trigger (edited 12-15-2002).]

DL-44
Maniac (V) Inmate

From: under the bed
Insane since: Feb 2000

posted posted 12-15-2002 16:04

Trigger - my 1st reply gives you the function to do just that - print_r()

« BackwardsOnwards »

Show Forum Drop Down Menu