Jump to content


Photo

=> Operator


  • Please log in to reply
8 replies to this topic

#1 mcloan

mcloan
  • Members
  • PipPipPip
  • Advanced Member
  • 48 posts

Posted 21 October 2006 - 05:58 PM

I am in the process of learning php.  In the book I am reading it has an array as $prices = array('tires=>'100', 'oil'=>10);

Can someone tell me what the => means?  Is this different than just =

Thank you.

#2 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 21 October 2006 - 06:20 PM

=> sets the key for an item in an array.

For example the following code:
$prices = array('tires=>'100', 'oil'=>10);

Sets up a variable called prices which stores an array which has two items

tires and oil

the string to the left of => operator setups up the key. The stuff after the => operators sets the value for the key.

The key is what you use in the square brakets, eg:
$prices['tires'] $proces['oil']

So tires holds the value of 100 and oil holds the value of 10

If you didnt have the => operator PHP will use a numeric key instead starting at zero for the first item, 1 for the secound, 2 for the third etc.

Hope that helps.

#3 mcloan

mcloan
  • Members
  • PipPipPip
  • Advanced Member
  • 48 posts

Posted 21 October 2006 - 07:04 PM

Very helpful.

Thank you so much!


#4 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 21 October 2006 - 08:44 PM

in addition, it allows you to pull the key out of an array when looping in a foreach. it has other more obscure functions as well, but i'll let someone a little more familiar with it cover those.
<?php
foreach ($myArray as $key => $val) {
  echo "$key = $val<br />\n";
}
?>

You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#5 mcloan

mcloan
  • Members
  • PipPipPip
  • Advanced Member
  • 48 posts

Posted 27 October 2006 - 02:22 PM

in addition, it allows you to pull the key out of an array when looping in a foreach. it has other more obscure functions as well, but i'll let someone a little more familiar with it cover those.

<?php
foreach ($myArray as $key => $val) {
  echo "$key = $val<br />\n";
}
?>


Thank you actually this use is what invoked my question, but I am still a little confused on this application of it.  For instance.  You may invoke the above code by setting it up as a function and passing $myArray as a variable which will probably be $_Post.

So if the above was function was named fillout, it would do something like the below:

fillout($_Post)

function fillout($myArray)
{
foreach ($myArray as $key => $val) {
  if (!isset($key) || ($val== ' '))
      return false;
}
      return true;
}

What I do not understand is how the value ($val) is getting set?  The key will be the form name but what about the value?  I think it may be in $_Post but I do not have a clear understanding of exactly what $_Post does.  It is setting up an associtive array with $key as field name => value of field name for each field on a form?

Your help is much appreciated.

Thank you.

#6 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 27 October 2006 - 02:33 PM

OK, as you stated, $_POST is an associative array. An associative array is an array made up of keys for the placeholders and values (what is stored at each key location). So, if I were to create an associative array, I would do the following:
<?php
$array = array(
  'key1' => 'value1',
  'key2' => 'value2',
  'key3' => 'value3
);
?>

Now, if I were wanting to use the foreach() like I showed above on this array I just created, it would simply return all the keys and values of the array:
<?php
foreach ($array as $key => $val) {
  echo "$key is $val<br />\n";
}

// ouputs:
// key1 is value1
// key2 is value2
// key3 is value3
?>

Once you have that understanding, you can apply it to the $_POST array knowing that the keys of the post array are the names of your form fields and the values of the array are the values of your form fields. So, if you were simply checking to make sure that your name and email address fields were not left empty, you could use the following:
<?php
foreach ($_POST as $key => $val) {
  if (($key == 'name' || $key == 'email') && empty($val)) {
    // either the name or the email address field is empty!
  }
}
?>

On a side note, you really never need to use isset() on the keys when you are looping through the $_POST variable. Since foreach() only cycles through those variables that are in the array, every key returned is set already.

Does this help some?
You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#7 mcloan

mcloan
  • Members
  • PipPipPip
  • Advanced Member
  • 48 posts

Posted 27 October 2006 - 02:49 PM

obsidian -

Perfect, your example is exactly what I needed to understand.  I really appreciate the thorough example.  Now I finally have the concept down. This will help me so much as I move forward in my book.

Thank you again!!!!

#8 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 27 October 2006 - 03:14 PM

Perfect, your example is exactly what I needed to understand.  I really appreciate the thorough example.  Now I finally have the concept down. This will help me so much as I move forward in my book.


Excellent. That's what we're here for ;) ... Out of curiosity, what book are you working through?
You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#9 mcloan

mcloan
  • Members
  • PipPipPip
  • Advanced Member
  • 48 posts

Posted 28 October 2006 - 02:00 AM

The book is php and msql web development by luke wellington and laura thomson.  It is a darn good book so far.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users