php, how to catch if a variable is never sent (post or get)

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
RICKYJ
Forum Newbie
Posts: 15
Joined: Sun Nov 05, 2006 9:10 am

php, how to catch if a variable is never sent (post or get)

Post by RICKYJ »

Hi,

I'm running into something that seems quite trivial. I'm primarily a java programmer, but am creating a web site that has a bit of php code.

The problem is, I have created a search page in php that querries a db, and returns to self with the results.

One of the problems im having is that if a user goes directly to search php, then there is an error thrown because there is no variable sent to this page.

I need a way of "catching" this error without having to define.

I have tried something like:

Code: Select all

if(  $_POST['myVar'] == NULL ){
$variableDefinedfromMyVar = "no search submited";
}
// problem is "$_POST['myVar']" thows an error since myVar is undefined

I also need to catch other nonsubmitted variables but cant see how ???? any suggestions :)
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

isset().
RICKYJ
Forum Newbie
Posts: 15
Joined: Sun Nov 05, 2006 9:10 am

Post by RICKYJ »

wow you're fast, looking into it on the php manual now, thanks
RICKYJ
Forum Newbie
Posts: 15
Joined: Sun Nov 05, 2006 9:10 am

Post by RICKYJ »

ok so it looks like I can just use:

Code: Select all

If ( isset($_POST['myVar']) == TRUE) {
// var has been initialised
} else {// not initialised yet}
Cheers ears
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Nope, you can get away with just isset()

Code: Select all

<?php
if (isset($_POST['myvar']))
{
    // Execute if set
}
?>
Post Reply