if and array explanation

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
User avatar
gkwhitworth
Forum Commoner
Posts: 85
Joined: Tue Sep 05, 2006 8:28 pm
Location: Wasilla, Alaska

if and array explanation

Post 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
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: if and array explanation

Post 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.
Last edited by Christopher on Tue Sep 12, 2006 1:12 am, edited 1 time in total.
(#10850)
User avatar
dibyendrah
Forum Contributor
Posts: 491
Joined: Wed Oct 19, 2005 5:14 am
Location: Nepal
Contact:

Post 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.
User avatar
gkwhitworth
Forum Commoner
Posts: 85
Joined: Tue Sep 05, 2006 8:28 pm
Location: Wasilla, Alaska

Post 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.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

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