Jump to content

[SOLVED] Help with undefined index


limitphp

Recommended Posts

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.