guybrown Posted May 30, 2013 Share Posted May 30, 2013 Hi all I am doing a video tutorial on GET but I get a 'notice' that the tutor doesn't: Notice: Undefined index: test in C:\xampp\htdocs\database\vartest2.php on line 2 Can I switch notifications like this off? Here is the php code: <?php $var = $_GET["test"]; $len = strlen($var); if ($len > 0) { echo $len; } else { echo "error: no input"; } ?> <form action="vartest2.php" method="GET"> <input type="text" name="test"> <input type="submit" value="Submit"> </form> This is what the tutor and I are entering into our browsers: http://localhost/database/vartest2.php (If I put ?test=anything at the end of the above, I don't get the notification but can't you just not specify ?test=... at the end?) Displayed in my browser: Notice: Undefined index: test in C:\xampp\htdocs\database\vartest2.php on line 2error: no input Displayed on the tutor's browser: error: no input I'm using Google Chrome Version 27.0.1453.94 m Any help appreciated. Regards, Guy Brown Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted May 30, 2013 Share Posted May 30, 2013 you can turn notices off, but you shouldn't, especially when learning php as you will also miss things like typo's and using wrong variable names. you will end up wasting hours of your time trying to find simple mistakes in your code if you hide any of the types of errors. the correct coding for a variable that might not exist is to test if it isset() before using the value. using the short form of an if(){}else{} statement the code would be - $var = isset($_GET["test"]) ? $_GET["test"] : ''; Quote Link to comment Share on other sites More sharing options...
guybrown Posted May 30, 2013 Author Share Posted May 30, 2013 you can turn notices off, but you shouldn't, especially when learning php as you will also miss things like typo's and using wrong variable names. you will end up wasting hours of your time trying to find simple mistakes in your code if you hide any of the types of errors. the correct coding for a variable that might not exist is to test if it isset() before using the value. using the short form of an if(){}else{} statement the code would be - $var = isset($_GET["test"]) ? $_GET["test"] : ''; Thanks Mac_gyver! I'll try this. Quote Link to comment Share on other sites More sharing options...
Solution guybrown Posted May 30, 2013 Author Solution Share Posted May 30, 2013 Works great. Now I can spend the evening understanding $var = isset($_GET["test"]) ? $_GET["test"] : ''; Much appreciated! Quote Link to comment 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.