SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Guru
    Join Date
    Sep 2008
    Location
    Dubai
    Posts
    971
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    if(isset($_POST['mail'])) and if($_POST['mail']) different ?

    Hi,

    Are those two statement the same ?

  2. #2
    SitePoint Zealot zalucius's Avatar
    Join Date
    Jul 2007
    Location
    Denmark
    Posts
    162
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No, they are not the same.

    the isset return true or false, depending on whether the variable exists or not.

    The latter return false if the variable contains false or does not exist, but true in any other cases.

    Perhaps the result looks to be the same, but it is not.
    Why?

    What if the $_POST['mail'] contains the value false? then the last "if" would not be executed.
    zalucius

  3. #3
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    isset() returns true if the variable exists and has a value other than type null. http://www.php.net/manual/en/language.types.null.php

    doing if ($var) returns true if the value contained in the variable evaluates to boolean true. To see which values evaluate to true or false, read http://www.php.net/manual/en/languag...oolean.casting

    Also, isset() will prevent php from throwing an E_NOTICE error in the event that the variable is undefined, or the array indice doesnt exist.

    Judging by the name 'email', either way would return true for values you would want to accept. But if for example, asking someone for some info, where zero could be a valid value they might submit, doing if ($var) wouldn't work well. isset() combined with strlen() or another content validating routine would be the way to go.


Bookmarks

Posting Permissions

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