mcloan Posted October 21, 2006 Share Posted October 21, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/24672-operator/ Share on other sites More sharing options...
wildteen88 Posted October 21, 2006 Share Posted October 21, 2006 => 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 itemstires and oilthe 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 10If 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. Quote Link to comment https://forums.phpfreaks.com/topic/24672-operator/#findComment-112377 Share on other sites More sharing options...
mcloan Posted October 21, 2006 Author Share Posted October 21, 2006 Very helpful.Thank you so much! Quote Link to comment https://forums.phpfreaks.com/topic/24672-operator/#findComment-112400 Share on other sites More sharing options...
obsidian Posted October 21, 2006 Share Posted October 21, 2006 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]<?phpforeach ($myArray as $key => $val) { echo "$key = $val<br />\n";}?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/24672-operator/#findComment-112448 Share on other sites More sharing options...
mcloan Posted October 27, 2006 Author Share Posted October 27, 2006 [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]<?phpforeach ($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. Quote Link to comment https://forums.phpfreaks.com/topic/24672-operator/#findComment-115366 Share on other sites More sharing options...
obsidian Posted October 27, 2006 Share Posted October 27, 2006 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]<?phpforeach ($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]<?phpforeach ($_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? Quote Link to comment https://forums.phpfreaks.com/topic/24672-operator/#findComment-115376 Share on other sites More sharing options...
mcloan Posted October 27, 2006 Author Share Posted October 27, 2006 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!!!! Quote Link to comment https://forums.phpfreaks.com/topic/24672-operator/#findComment-115390 Share on other sites More sharing options...
obsidian Posted October 27, 2006 Share Posted October 27, 2006 [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? Quote Link to comment https://forums.phpfreaks.com/topic/24672-operator/#findComment-115408 Share on other sites More sharing options...
mcloan Posted October 28, 2006 Author Share Posted October 28, 2006 The book is php and msql web development by luke wellington and laura thomson. It is a darn good book so far. Quote Link to comment https://forums.phpfreaks.com/topic/24672-operator/#findComment-115674 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.