Page 1 of 1

parameter error

Posted: Sat Nov 19, 2005 3:35 am
by jaylin
i m very new to php. now i test about parameter. This is my code.

Code: Select all

if (!$_GET['page'])
{
    $page = 1;
} ELSE
{
    $page = $_GET['page'];
}
for the first (no paramert) i got the error :
Notice: Undefined index: page in c:\Inetpub\wwwroot\test.php on line 27

this works fine if there is paramerter (page).

How can i solve that?
regards,

Posted: Sat Nov 19, 2005 3:38 am
by m3mn0n
isset() funtion in the if statement

Posted: Sat Nov 19, 2005 4:54 am
by php3ch0

Code: Select all

if (!isset($_GET['page'])) 
{ 
    $page = 1; 
} ELSE 
{ 
    $page = $_GET['page']; 
}

Posted: Sat Nov 19, 2005 6:44 am
by twigletmac
You can even shorten the code a bit by using the ternary operator:

Code: Select all

$page = (isset($_GET['page'])) ? $_GET['page'] : 1;
Mac

Posted: Sat Nov 19, 2005 4:44 pm
by John Cartwright
Might want to check if anything exists in the variable, not if the variable exists.. as well as checking if it is numeric or not, which it should be

Code: Select all

$page = (!empty($_GET['page']) && is_numeric($_GET['page'])) ? $_GET['page'] : 1;