Page 1 of 1

Code snippet help please

Posted: Fri Dec 09, 2011 4:19 am
by paulala
Hi guys,

Im trying to learn PHP as I go. (The hardest way)

I have usually managed to work everything out as I encountered it but Im having some difficulty with this:

<?php echo $product['product_name'] . $product['product_attributes'] ?>

Can somebody please give me a clue as to what this means when a variable is declared with square brackets after it like this.

Im thinking it has something to do with integration with javascript (Mootools). Somehow, this code snippet produces a URL and Im tearing my hair out to understand how it does so.

All help greatly appreciated.

thanks in advance,

Paula

Re: Code snippet help please

Posted: Fri Dec 09, 2011 4:24 am
by social_experiment

Code: Select all

<?php echo $product['product_name'] . $product['product_attributes'] ?>
$product is an array and using square brackets means you are accessing a specific element within the array. The fullstop in the middle concatenates the two values (adds them together). To test this use the following:

Code: Select all

<?php
 print_r($product);
It should display the contents of $product and show whether it is an array.

Re: Code snippet help please

Posted: Fri Dec 09, 2011 6:25 am
by phphelpme
You can set an array manually by giving it key values or have the array key values set automatically. Many things loaded from a database are placed in an array.

Code: Select all

$array[key] = value;
Check out the link for PHP.NET site explaining about Arrays and the use of them.

http://php.net/manual/en/language.types.array.php

http://www.tizag.com/phpT/arrays.php

http://www.w3schools.com/php/php_arrays.asp

Hope the links help you even further. :)

Re: Code snippet help please

Posted: Fri Dec 09, 2011 6:55 am
by paulala
Thanks guys youre a pair of stars :) ....

I think Im gonna like this forum!

Paula

Re: Code snippet help please

Posted: Fri Dec 09, 2011 7:18 am
by phphelpme
Your very welcome any more help just bob back on forum :)