Jump to content

!isset vs empty


monkeytooth

Recommended Posts

I have a search index I am building up. It can be accessed one of 2 ways first way being your customary way going to the main index of the search page and typing a term and hitting a button to search, then next would be via URL through variables contained within the URL.

 

What I want to do is figure out what the best way to handle an instance where both the _GET and _POST values are empty, null, not supplied, however you want to spin it. And display a message saying "Please enter your search term".

 

My debate is what is the best way to handle that? I don't know what would be the best way I guess is the best way to put it

 

should I do something like

if((!isset($_POST[var1])) AND (!isset($_GET[var1]))){/*show default message*/}

or

if((empty($_POST[var1])) AND (empty($_GET[var1]))){/*show default message*/}

or

if((!$_POST[var1]) AND (!$_GET[var1])){/*show default message*/}

 

is there a better way to approach this idea?

 

Link to comment
Share on other sites

I prefer to expect variable either as POST or GET and try to avoid the possibility for both, if you however expect both you could use REQUEST

<?php

if(empty($_REQUEST['var']))

?>

as empty returns false also if 'var' is not set, so it checks both if it is set and that its not empty

Link to comment
Share on other sites

Well as you know, isset() literally just checks if the variable has been set, it has no kind of input validation on it. For example you would use it to check to see if a session variable had been set when a user logs in, the value may not be important.

 

However, in a user-submitted form the value is important, i'd prefer to use the empty() function to check against form element inputs. But on the other hand, I would use isset() to check for checkboxes, and radio buttons.

Link to comment
Share on other sites

Well I do use empty typically when it comes to user input, example I check to see if the form submit button is part of the posted inputs.. if its not empty then I double check it if its not empty with the isset and if isset does it match the value I want it to match.

 

But thats not fully the case here, I need to check both get and post and verify if they are null or set

Link to comment
Share on other sites

Well I seemed to skip over that response while scrolling down initially. All variables expected to trigger an event are named differently ie $_POST['var1'] $_GET['var2'].. however with either triggering an event accordingly I want to catch it if both are not there. independently I can catch them fine, trigger a message if its not there, run script if it is.

 

But in the past when playing with $_GET $_POST data, ive had to use !$_POST, or !isset() or empty() and occasionally ran into an issue with each one what would work on one page/server (i have multiple clients and servers to work with) All the servers to my knowing are set up similar, same php builds an all else different companies in some cases but generally all the same stuff. Either way !$_POST would work on one where !isset() would work on the other.. or only isset() would work but not !isset()

 

So I was trying to find out a better more universal approach one that is in good practice so to speak.

 

Link to comment
Share on other sites

Well, I still don't entirely understand what you're trying to achieve, but this is my best guess:

 

<?php
$validate = array('var1', 'var2');
  
foreach ($validate as $key) {
  if (!isset($_REQUEST[$key]) || empty($_REQUEST[$key])) {
    // empty or not set
  }
  else {
    // They're fine
}
?>

   

Link to comment
Share on other sites

Well... yeah... there's no point checking isset($_POST) byt checking isset($_POST['variable']) is something quite another.

 

Scratch all that... something must have changed since I checked it last time. Checking empty on unset variable does not raise E_NOTICE in PHP 5.3 or 5.2. Have nowhere to check previous versions.

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.