Lassie Posted September 25, 2007 Share Posted September 25, 2007 Although I have been programming with php for a little while I am still failing to understand what causes errors such as Undefined index:user_id or Undefined variable: ven: I thought that variables did not have to be predefined and where defined when you created them. Could some guide me please as I seem to misunderstand a basic regarding php. Quote Link to comment https://forums.phpfreaks.com/topic/70623-elementary-question-undefined-indexvariable/ Share on other sites More sharing options...
jaymc Posted September 25, 2007 Share Posted September 25, 2007 Thats just a warning.. If I had error displaying on I would have all kinds all over my pages Turn off error reporting or define variables if they are going to be called without validation Quote Link to comment https://forums.phpfreaks.com/topic/70623-elementary-question-undefined-indexvariable/#findComment-354890 Share on other sites More sharing options...
rarebit Posted September 25, 2007 Share Posted September 25, 2007 Before you call a variable you will need to define / initialise it Quote Link to comment https://forums.phpfreaks.com/topic/70623-elementary-question-undefined-indexvariable/#findComment-354892 Share on other sites More sharing options...
Lassie Posted September 25, 2007 Author Share Posted September 25, 2007 Thanks. By define do you mean eg $var=NULL; Quote Link to comment https://forums.phpfreaks.com/topic/70623-elementary-question-undefined-indexvariable/#findComment-354913 Share on other sites More sharing options...
wildteen88 Posted September 25, 2007 Share Posted September 25, 2007 The undefined index notices are normally caused by not checking whether a user defined variable exists or not, such as $_GET, $_POST and $_COOKIE vars. Consider this code: <?php $name = $_POST['name']; if($name) { echo 'Your Name is: '. $name; } ?> <form action="#" method="post"> Name: <input type="text" name="name"><br> <input type="submit" value="Post my name"> </form> That code is fine to use. However if you run the script without submitting the form you'll get an Undefined index 'name' notice message displayed. This is because PHP is assigning the $name variable to the value of $_POST['name']. But we haven't submitted the form yet! So $_POST['name'] wont exist. If you call any variable within your code PHP is expecting that variable exists. What you should do to prevent notices is to first verify that variable exists before using it. PHP has handy function for this called isset. In stead of doing the following: $name = $_POST['name']; if($name) You should do: if(isset($_POST['name']) { $name = $_POST['name']; Now if you run the script again without submitting the form there will be no notice message. Quote Link to comment https://forums.phpfreaks.com/topic/70623-elementary-question-undefined-indexvariable/#findComment-355111 Share on other sites More sharing options...
Daniel0 Posted September 25, 2007 Share Posted September 25, 2007 Thats just a warning.. If I had error displaying on I would have all kinds all over my pages Turn off error reporting or define variables if they are going to be called without validation Well, no... it's a notice (warnings are different) and if you get them then something is wrong with your code. It's not critical however, but it should still be fixed. Quote Link to comment https://forums.phpfreaks.com/topic/70623-elementary-question-undefined-indexvariable/#findComment-355149 Share on other sites More sharing options...
Lassie Posted September 26, 2007 Author Share Posted September 26, 2007 Thanks guys Quote Link to comment https://forums.phpfreaks.com/topic/70623-elementary-question-undefined-indexvariable/#findComment-355635 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.