Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New Coder
    Join Date
    Aug 2005
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Is thedre a reason to use EMPTY()

    Guys,

    Is there a reason why:

    PHP Code:
    if (!empty($myId)) {
        
    // do stuff

    should be used instead of just:

    PHP Code:
    if ($myId) {
        
    // do stuff

    Thanks,
    C.

  • #2
    Senior Coder chump2877's Avatar
    Join Date
    Dec 2004
    Location
    the U.S. of freakin' A.
    Posts
    2,773
    Thanks
    19
    Thanked 155 Times in 146 Posts
    I think empty() let's you know if a form variable/field is actually empty, while the other method just let's you know if the variable is set or not...there are occasions when your variable will be set, but the form field is actually empty...In that case, your two pieces of code would evaluate differently...

    What is it that you are trying to do with your code? Detect if a variable is empty or if a variable is not set?
    Regards, R.J.

    ---------------------------------------------------------

    Help spread the word! Like my YouTube-to-Mp3 Conversion Script on Facebook !! :)
    [Related videos and tutorials are also available at my YouTube channel and on Dailymotion]
    Get free updates about new software version releases, features, and bug fixes!

  • #3
    New Coder
    Join Date
    Aug 2005
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Trying to detect if it's set or not, so I can render the page based on data available.

    It's a directory of sorts, so I test how far in they've filtered:

    PHP Code:
    if (!$categoryId && !subcategoryId && !$directoryId) {
        
    // show category list
    } else if ($categoryId && !subcategoryId && !$directoryId) {
        
    // show subcategory list based on category
    } else if ($categoryId && subcategoryId && !$directoryId) {
        
    // show all directory entries for this subcategory
    } else if ($categoryId && subcategoryId && $directoryId) {
        
    // show directory entry

    The variables get set based on the querystring, so it would build up as follows:

    page.php
    page.php?c=1
    page.php?c=1&s=2
    page.php?c=1&s=2&d=3

    Thanks,
    C.

  • #4
    Senior Coder NancyJ's Avatar
    Join Date
    Feb 2005
    Location
    Bradford, UK
    Posts
    3,172
    Thanks
    19
    Thanked 65 Times in 64 Posts
    if you want to test if a value is set use isset()

  • #5
    Senior Coder chump2877's Avatar
    Join Date
    Dec 2004
    Location
    the U.S. of freakin' A.
    Posts
    2,773
    Thanks
    19
    Thanked 155 Times in 146 Posts
    Unless they're using HTML textboxes or textareas, i think you are OK to just use either:

    PHP Code:
    if ($variable)
    {

    code;


    or

    PHP Code:
    if (isset($variable))
    {

    code;


    Regards, R.J.

    ---------------------------------------------------------

    Help spread the word! Like my YouTube-to-Mp3 Conversion Script on Facebook !! :)
    [Related videos and tutorials are also available at my YouTube channel and on Dailymotion]
    Get free updates about new software version releases, features, and bug fixes!

  • #6
    Senior Coder NancyJ's Avatar
    Join Date
    Feb 2005
    Location
    Bradford, UK
    Posts
    3,172
    Thanks
    19
    Thanked 65 Times in 64 Posts
    the top 2 lines in the handy chart show the difference between if($var) and isset($var), also has empty in there too
    http://www.deformedweb.co.uk/php_variable_tests.php
    Basically if($var) is the same as if(!empty($var)) but not the same as isset($var)
    Last edited by NancyJ; 09-14-2005 at 05:19 PM.

  • #7
    Regular Coder
    Join Date
    Jun 2005
    Posts
    804
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Using if(!$x) is not the same as using isset() or empty(). if(!$x) tests for a negative boolean value (loosely), not the existence or contents of the variable; it's equivalent to

    PHP Code:
    if($x == FALSE
    If $x is 0, NULL, or empty, if(!$x) will return true, even though $x has been set, because 0, NULL, and empty vars evaluate to FALSE in loose comparisons. You should only use if(!$x) when you know $x is a proper boolean value. Otherwise, you should use one of PHP's other variable-checking functions.

    For more chart fun, here are the PHP doc's type comparison tables.

  • #8
    New Coder
    Join Date
    Aug 2005
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Cool, thank you.

    C.


  •  

    Posting Permissions

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