Page 1 of 1

if and array explanation

Posted: Tue Sep 12, 2006 12:53 am
by gkwhitworth
I just really would like to know something:

Code: Select all

<?
						$package = $_REQUEST['package'];
						$prices = array (
								'basic' => 1500,
								'professionalLite' => 2300,
								'premiumProfessional' => 3500
								);
								
						if (isset ($_REQUEST['package'])) {
							$answer = $prices[$package];
							}
							
						?>
I want to know what the following snippet of code is achieving, mainly this line because I get the rest:

Code: Select all

$answer = $prices[$package];
							}

Thanks

Re: if and array explanation

Posted: Tue Sep 12, 2006 1:12 am
by Christopher
gkwhitworth wrote:I want to know what the following snippet of code is achieving, mainly this line because I get the rest:

Code: Select all

$answer = $prices[$package];
The code $prices[$package] is looking up the element with the name in $package in the array $prices. So if $package == 'basic' then $prices[$package] == 1500, and if $package == 'professionalLite' then $prices[$package] == 2300, and so on.

Posted: Tue Sep 12, 2006 1:12 am
by dibyendrah
It's just simple.

it's taking the query string variable called package.
Suppose if you pass the variable package from the page

Code: Select all

price.php?package=basic
, $answer wil have 1500 in value. It is just taking the value from the array index.

Posted: Tue Sep 12, 2006 1:28 am
by gkwhitworth
Thank you for the replies, I knew it was doing something of that nature...I just wanted to know exactly what it was doing so I wasn't just copying someone's code and not learning actually how it works, just in case run into the same thing later.

Posted: Tue Sep 12, 2006 2:10 am
by volka
Then by now you should know what the line

Code: Select all

if ( isset($price[$package]) ) {
of the "original" code does.
Why did you remove it?