#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    60
    Rep Power
    3

    What's the difference between "!isset($value)" and "$value == false", please?


    Okay, I was writing this code where a While Loop go through each fetched record from a database table and see if each record contains any value under the column named "URL" on a database table. I wrote 2 different codes and one shows that the while loop successfully finds out a record contains no value whereas the other one doesn't seem like it can determine that the record doesn't hold any value under the column "URL".
    What I did for the failed code was leaving out the line "||$row['URL']== false" from the code.
    Is the line "!isset($row['URL'])" not enough by itself to determine if the $row['URL'] contains no value?
    What's the difference between "!isset($row['URL'])" and "$row['URL']== false"??
    The two codes are down below.
    Someone please help me.


    ----- Failed Code -----
    Code:
    if(!isset($row['URL'])){$URL='no-image.jpg';}else{$URL=$row['URL'];}

    ----- Right Code -----
    Code:
    if(!isset($row['URL'])||$row['URL']== false){$URL='no-image.jpg';}else{$URL=$row['URL'];}
  2. #2
  3. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,250
    Rep Power
    9400
    isset() only checks if the value (a) exists or (b) === null.
  4. #3
  5. No Profile Picture
    Lost in code
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2004
    Posts
    8,316
    Rep Power
    7170
    empty($var) will do almost the same thing as (!isset($var) || $var == false);
    PHP FAQ

    Originally Posted by Spad
    Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around
  6. #4
  7. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,908
    Rep Power
    6352
    You use isset() to check to see if a variable is set before you attempt to use it. It's to avoid errors and warnings (and bad logic in your code).

    The construct:

    if ( isset($a) && $a == 'something' )

    Is almost always used when $a is a variable which may or may not exist (like an element of $_POST, or a variable which is conditionally created, like error codes).

    If you didn't use the isset(), your code will throw a warning, which is incorrect code and a performance bottleneck on high-performing systems.
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    60
    Rep Power
    3
    Originally Posted by requinix
    isset() only checks if the value (a) exists or (b) === null.

    Thank you for your reply.

    If there is no value under the column, that means null right? no??
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    60
    Rep Power
    3
    Originally Posted by ManiacDan
    You use isset() to check to see if a variable is set before you attempt to use it. It's to avoid errors and warnings (and bad logic in your code).

    The construct:

    if ( isset($a) && $a == 'something' )

    Is almost always used when $a is a variable which may or may not exist (like an element of $_POST, or a variable which is conditionally created, like error codes).

    If you didn't use the isset(), your code will throw a warning, which is incorrect code and a performance bottleneck on high-performing systems.

    Okay. Thanks for your reply. I'm actually not clear about what "a variable is set" mean to begin with.

    Does the isset() not determine that variable contains some values, right? I was thinking if there is no value under a column in a database table, that means there is nothing to be stored in the variable thus the isset() determines that variable isn't set and the code will be processed according to the result that there is no value inside the variable.

    What is setting variable means really, please??
  12. #7
  13. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,250
    Rep Power
    9400
    That's actually kinda true.

    "To be set" means that a variable was assigned a value and that value is not null. So
    PHP Code:
    isset($foo// false: no value yet
    $foo 123;
    isset(
    $foo// true: has a value
    $foo "";
    isset(
    $foo// still true: has a value
    $foo null;
    isset(
    $foo// false: has the value null 
    When a value from a database is NULL then the variable will likely be null too and thus !isset().
  14. #8
  15. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,908
    Rep Power
    6352
    Verified, basically, with this:

    PHP Code:
    $row = array("id" => 123"name" => "bob""age" => null);
    var_dump(isset($row['age']));
    //bool(false)
    print_r($row);
    /*Array
    (
        [id] => 123
        [name] => bob
        [age] => 
    )*/ 
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  16. #9
  17. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,250
    Rep Power
    9400
    Yep. That there is basically the number one reason why array_key_exists is around.
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    60
    Rep Power
    3
    Originally Posted by requinix
    That's actually kinda true.

    "To be set" means that a variable was assigned a value and that value is not null. So
    PHP Code:
    isset($foo// false: no value yet
    $foo 123;
    isset(
    $foo// true: has a value
    $foo "";
    isset(
    $foo// still true: has a value
    $foo null;
    isset(
    $foo// false: has the value null 
    When a value from a database is NULL then the variable will likely be null too and thus !isset().

    Okay, so the reason why the condition for !isset($_POST['URL']) isn't met when nothing is inside the corresponding cell is that when there is no value under URL column on a fetched row, the value will be "" and this will still be regarded as TRUE(as it is still character "nothing").
    Then what is null?? I will have to look into it.
  20. #11
  21. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Originally Posted by Ihatephp
    Okay, so the reason why the condition for !isset($_POST['URL']) isn't met when nothing is inside the corresponding cell is that when there is no value under URL column on a fetched row, the value will be ""
    Are you talking about database tables or request data?

    For request data, yes, empty input fields arrive as empty strings -- which makes a lot of sense, because request data is always a string.

    For database tables, you have to distinguish between NULL and the empty string. If the table attribute does in fact have no value (which is represented by NULL), you will get null in PHP, not an empty string. If, however, the attribute is an empty string (which is a value), then you'll get back an empty string.



    Originally Posted by Ihatephp
    Then what is null??
    A special value representing the absence of an actual value -- just like NULL in database systems.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".

IMN logo majestic logo threadwatch logo seochat tools logo