Case Concepts -7 What is undefined index error in PHP ? Level - - TopicsExpress



          

Case Concepts -7 What is undefined index error in PHP ? Level - Intermediate - Time to Grasp - 25 Minutes . Basically , many programmers will get this error of undefined index and start wondering what went wrong .Lets understand this error in depth so that once you understand why this happens , you will then have a indepth understanding of the subject and be able to appreciate it ! What happens is thaat when we define a array variable , we can store many values inside it . However this values get stored in something called indexes . When i write name=(amlan , Prashant ,dev ); What i have essentially done is written a array variable which has 3 values inside it.However , also understand that the value Amlan is now occupied in zero index , prashant is occupying the 1st index , and dev is occupying the 3rd index , Say someone calls the array name(2) , it will pop out dev , if someone calls name(0) , it will pop out amlan But if someone calls name(7), we have not defined any such index inside the array name ...so the programme throws a undefined index error In associative errays , the concept is similar , but here we have key- value pairs like, name=(name1->amlan,name 2->prashant ,name3->dev); Here the indexes are synonymous to keys ...so zero index now is name 1 and its value is amlan ,so the way to call it is now by name(name1) and not name(0); Lets now attack the subject , in HTML we make use of forms very often and define method for collecting form content as POST and GET without realising what they are ...they are methods of collecting form data no doubt but stored as what ...aha , you are getting my point ...storing them as array values They are basically associative arrays wherein the indexes are based on the form variable name...lets clear this with example ....here the variable name is city . ....here the variable name is age . ....here the variable name is gender . if ( isset( $_POST[city] ) && isset($_POST[age]) && isset($_POST[gender]) ) { $city = $_POST[city]; $age = $_POST[age]; $gender = $_POST[gender]; echo $city ; echo $age; echo$gender } ?> When we click on submit , all the form variables and the user input data get stored in the $_POST associative array where the variable name forms the key (Index) and the user input forms the value Here , it would be like $_POST ( city -> user input 1 , age - >user input , gender - > user input ) So , say Ram is in the front end and puts his city as Mumbai and age as 30 and gender as Amlan , then the $_POST would be a array variable containing ( city -> Mumbai , age - >30 , gender - > Amlan ) when we call such form elements using PHP as shown above , if the form names i.e thereby the associative indexes - are nor defined properly , then like explained above , it is pretty simple to understand that we shall get undefined index error...implying , if we call $_POST [HOBBY]....since there is no such index (Variable name ) in our form , obviously calling the post array will result in undefined index Cheers, Amlan Dutta
Posted on: Thu, 17 Oct 2013 18:19:32 +0000

Trending Topics



Recently Viewed Topics




© 2015