Code snippet help please

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
paulala
Forum Newbie
Posts: 2
Joined: Fri Dec 09, 2011 3:52 am

Code snippet help please

Post 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
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Code snippet help please

Post 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.
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
phphelpme
Forum Contributor
Posts: 261
Joined: Sun Nov 21, 2010 3:32 pm

Re: Code snippet help please

Post 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. :)
paulala
Forum Newbie
Posts: 2
Joined: Fri Dec 09, 2011 3:52 am

Re: Code snippet help please

Post by paulala »

Thanks guys youre a pair of stars :) ....

I think Im gonna like this forum!

Paula
phphelpme
Forum Contributor
Posts: 261
Joined: Sun Nov 21, 2010 3:32 pm

Re: Code snippet help please

Post by phphelpme »

Your very welcome any more help just bob back on forum :)
Post Reply