www.webdeveloper.com
Results 1 to 5 of 5

Thread: isset vs !empty

  1. #1
    Join Date
    May 2012
    Posts
    1

    isset vs !empty

    Hello,

    I am fairly new to PHP and I am creating a basic registration from. I was trying to use isset to check all the variables but without any luck. It seemed all I needed to go was use !empty instead of isset.

    Any reason why isset would not work?

    if ( (!empty($username)) && (!empty($email)) && (!empty($email2)) && (!empty($pass)) && (!empty($pass2)) )
    {
    // seems to work
    // rest of code to validate
    }

    else
    {
    echo 'please fill in all fields';
    }

    Cheers.

  2. #2
    Join Date
    Feb 2012
    Location
    Bokaro Steel City (Jharkhand), India!
    Posts
    194
    PHP Code:
    if(isset($username,$email,$email2,$pass,$pass2)) {
        
    // do stuff here
    } else {
        
    // do stuff here

    "It will never rain roses: when we want to have more roses, we must plant more roses." - George Eliot

  3. #3
    Join Date
    Feb 2012
    Location
    Bokaro Steel City (Jharkhand), India!
    Posts
    194
    isset($variable [, ...$variable...])
    Accepts multiple $variables separated by commas, but will only return TRUE if all
    variables are set!
    Last edited by TheAliveWinner; 05-28-2012 at 02:06 AM. Reason: It is too hot in India and I am sick! :)

  4. #4
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,326
    isset() returns false if the variable is not set at all or is set to NULL. In addition to those two cases, !empty() will return false if the variable is set but has a value of FALSE, 0, 0.0, or "" (empty string). Thus, for example, you need to be careful about using !empty() when testing for a numeric input where zero is a valid value.
    PHP Code:
    <pre><?php
    $notEmpty 
    = (!empty($foo));
    $isset    = (isset($foo));
    echo 
    "not set:\nnotEmpty:";
    var_dump($notEmpty);
    echo 
    "isset:";
    var_dump($isset);
    echo 
    "\n";

    $foo null;
    $notEmpty = (!empty($foo));
    $isset    = (isset($foo));
    echo 
    "null:\nnotEmpty:";
    var_dump($notEmpty);
    echo 
    "isset:";
    var_dump($isset);
    echo 
    "\n";

    $foo false;
    $notEmpty = (!empty($foo));
    $isset    = (isset($foo));
    echo 
    "false:\nnotEmpty:";
    var_dump($notEmpty);
    echo 
    "isset:";
    var_dump($isset);
    echo 
    "\n";

    $foo 0;
    $notEmpty = (!empty($foo));
    $isset    = (isset($foo));
    echo 
    "0 (zero):\nnotEmpty:";
    var_dump($notEmpty);
    echo 
    "isset:";
    var_dump($isset);
    echo 
    "\n";

    $foo 1;
    $notEmpty = (!empty($foo));
    $isset    = (isset($foo));
    echo 
    "non-zero number:\nnotEmpty:";
    var_dump($notEmpty);
    echo 
    "isset:";
    var_dump($isset);
    echo 
    "\n";

    $foo '';
    $notEmpty = (!empty($foo));
    $isset    = (isset($foo));
    echo 
    "empty string:\nnotEmpty:";
    var_dump($notEmpty);
    echo 
    "isset:";
    var_dump($isset);
    echo 
    "\n";

    $foo 'string';
    $notEmpty = (!empty($foo));
    $isset    = (isset($foo));
    echo 
    "string:\nnotEmpty:";
    var_dump($notEmpty);
    echo 
    "isset:";
    var_dump($isset);
    echo 
    "\n";
    ?></pre>
    Output:
    Code:
    not set:
    notEmpty:bool(false)
    isset:bool(false)
    
    null:
    notEmpty:bool(false)
    isset:bool(false)
    
    false:
    notEmpty:bool(false)
    isset:bool(true)
    
    0 (zero):
    notEmpty:bool(false)
    isset:bool(true)
    
    non-zero number:
    notEmpty:bool(true)
    isset:bool(true)
    
    empty string:
    notEmpty:bool(false)
    isset:bool(true)
    
    string:
    notEmpty:bool(true)
    isset:bool(true)
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  5. #5
    Join Date
    May 2012
    Posts
    42
    When you submit a form, say VIA POST, the form sets the value of the form field into the $_POST variable. So if the form field (say, name="username") is left empty, $_POST['username'] will be set, but just to an empty value ("").

    When you're validating the form input on the PHP side, using isset($_POST['username']) will make sure that the form field is there, while empty($_POST['username']) will tell you whether or not the user has put anything into that field.

    In my personal opinion, it's important to check both when validating form fields as users can alter the form input data using various methods. You can do that by calling each function for each field, or if you're going to be doing that many times, you can create a function.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles