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
jaylin
Forum Commoner
Posts: 68 Joined: Fri Nov 18, 2005 4:44 am
Post
by jaylin » Sat Nov 19, 2005 3:35 am
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,
m3mn0n
PHP Evangelist
Posts: 3548 Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada
Post
by m3mn0n » Sat Nov 19, 2005 3:38 am
isset() funtion in the if statement
php3ch0
Forum Contributor
Posts: 212 Joined: Sun Nov 13, 2005 7:35 am
Location: Folkestone, Kent, UK
Post
by php3ch0 » Sat Nov 19, 2005 4:54 am
Code: Select all
if (!isset($_GET['page']))
{
$page = 1;
} ELSE
{
$page = $_GET['page'];
}
twigletmac
Her Royal Site Adminness
Posts: 5371 Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK
Post
by twigletmac » Sat Nov 19, 2005 6:44 am
You can even shorten the code a bit by using the ternary operator:
Code: Select all
$page = (isset($_GET['page'])) ? $_GET['page'] : 1;
Mac
John Cartwright
Site Admin
Posts: 11470 Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:
Post
by John Cartwright » Sat Nov 19, 2005 4:44 pm
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;