limitphp Posted August 27, 2009 Share Posted August 27, 2009 I have this notice: Notice: Undefined index: currentpage in C:\wamp\www\index.php on line 172 Here's the code for that line: // get the current page or set a default $currentpage = is_numeric($_GET['currentpage']) ? (int) $_GET['currentpage'] : 1; I wrote this a while back....i forgot what the question mark and colon do. How can I fix this notice and keep $currentpage set to the right value? thanks Quote Link to comment https://forums.phpfreaks.com/topic/172149-solved-help-with-undefined-index/ Share on other sites More sharing options...
Mardoxx Posted August 27, 2009 Share Posted August 27, 2009 http://en.wikipedia.org/wiki/Ternary_operation#PHP Quote Link to comment https://forums.phpfreaks.com/topic/172149-solved-help-with-undefined-index/#findComment-907703 Share on other sites More sharing options...
akitchin Posted August 27, 2009 Share Posted August 27, 2009 you can fix the notice by checking if the item is set before checking if it is numeric. the reason it is giving an undefined index is because the index doesn't exist in the GET array (obviously the var wasn't passed with the URL): $currentpage = (isset($_GET['currentpage']) && is_numeric($_GET['page'])) ? (int)$_GET['currentpage'] : 1; the question and colon are part of the ternary form for if() statements - it's in the manual. briefly: (condition) ? statement if true : statement if false; note that although the notices do help point out spots where your code could (and should) be better written and places where potential problems may occur, they will not halt your script. you should try to set the error reporting at a lower level when you move this script to production. EDIT: beaten to the punch on the ternary front. Quote Link to comment https://forums.phpfreaks.com/topic/172149-solved-help-with-undefined-index/#findComment-907704 Share on other sites More sharing options...
limitphp Posted August 27, 2009 Author Share Posted August 27, 2009 thank you guys for all the info. So, for production turn off notices and e reporting.....but in the testing phase it best to try and get rid of these notices.... thanks again! Quote Link to comment https://forums.phpfreaks.com/topic/172149-solved-help-with-undefined-index/#findComment-907714 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.