Jump to content

Archived

This topic is now archived and is closed to further replies.

mcloan

=> Operator

Recommended Posts

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.

Share this post


Link to post
Share on other sites
=> sets the key for an item in an array.

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

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.

Share this post


Link to post
Share on other sites
in addition, it allows you to [b]pull[/b] 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.
[code]
<?php
foreach ($myArray as $key => $val) {
  echo "$key = $val<br />\n";
}
?>
[/code]

Share this post


Link to post
Share on other sites
[quote author=obsidian link=topic=112256.msg455582#msg455582 date=1161463474]
in addition, it allows you to [b]pull[/b] 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.
[code]
<?php
foreach ($myArray as $key => $val) {
  echo "$key = $val<br />\n";
}
?>
[/code]
[/quote]

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.

Share this post


Link to post
Share on other sites
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:
[code]
<?php
$array = array(
  'key1' => 'value1',
  'key2' => 'value2',
  'key3' => 'value3
);
?>
[/code]

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:
[code]
<?php
foreach ($array as $key => $val) {
  echo "$key is $val<br />\n";
}

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

Once you have that understanding, you can apply it to the $_POST array knowing that the [b]keys[/b] of the post array are the [b]names[/b] of your form fields and the [b]values[/b] of the array are the [b]values[/b] 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:
[code]
<?php
foreach ($_POST as $key => $val) {
  if (($key == 'name' || $key == 'email') && empty($val)) {
    // either the name or the email address field is empty!
  }
}
?>
[/code]

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?

Share this post


Link to post
Share on other sites
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!!!!

Share this post


Link to post
Share on other sites
[quote author=mcloan link=topic=112256.msg458590#msg458590 date=1161960595]
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.
[/quote]

Excellent. That's what we're here for ;) ... Out of curiosity, what book are you working through?

Share this post


Link to post
Share on other sites
The book is php and msql web development by luke wellington and laura thomson.  It is a darn good book so far.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.