parameter error

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jaylin
Forum Commoner
Posts: 68
Joined: Fri Nov 18, 2005 4:44 am

parameter error

Post 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,
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

isset() funtion in the if statement
User avatar
php3ch0
Forum Contributor
Posts: 212
Joined: Sun Nov 13, 2005 7:35 am
Location: Folkestone, Kent, UK

Post by php3ch0 »

Code: Select all

if (!isset($_GET['page'])) 
{ 
    $page = 1; 
} ELSE 
{ 
    $page = $_GET['page']; 
}
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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;
Post Reply