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 9 of 9
  1. #1
    Regular Coder Coastal Web's Avatar
    Join Date
    Oct 2004
    Posts
    225
    Thanks
    12
    Thanked 3 Times in 3 Posts

    easy question - checking for empty vars

    Hi guys... I have a question is:
    Code:
    if(trim($someVar) == '')
    the same as:
    Code:
    if(empty($someVar))
    Usually l check for empty vars with:
    PHP Code:
    if($someVar != '' && !empty($someVar)){
    //do something

    But l was just wonderingif there's a more standard way of doing this.

  • #2
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by Coastal Web View Post
    Hi guys... I have a question is:
    Code:
    if(trim($someVar) == '')
    the same as:
    Code:
    if(empty($someVar))
    Usually l check for empty vars with:
    PHP Code:
    if($someVar != '' && !empty($someVar)){
    //do something

    But l was just wonderingif there's a more standard way of doing this.
    standard is to not check,
    I don't think it is something standard because depend of what type is $someVar.
    In your example you assume there is a string, this is the usual case, but could be a number, array, object or else depend of coding and context.
    Since you know what type would be the best things is to check if exists first, what type is or expect next, to avoid previous errors, and then to check acording to that type.

    http://www.php.net/manual/en/ref.var.php

    is_xxx, intval, isset, empty are few I use.

    best regards

  • #3
    Regular Coder Coastal Web's Avatar
    Join Date
    Oct 2004
    Posts
    225
    Thanks
    12
    Thanked 3 Times in 3 Posts
    Quote Originally Posted by oesxyl View Post
    standard is to not check,
    I don't think it is something standard because depend of what type is $someVar.
    In your example you assume there is a string, this is the usual case, but could be a number, array, object or else depend of coding and context.
    Since you know what type would be the best things is to check if exists first, what type is or expect next, to avoid previous errors, and then to check acording to that type.

    http://www.php.net/manual/en/ref.var.php

    is_xxx, intval, isset, empty are few I use.

    best regards
    Hi oesxyl,

    Yes l'm talking specifically about strings...
    So what is the best method of checking for empty strings?

  • #4
    Senior Coder Len Whistler's Avatar
    Join Date
    Jul 2002
    Location
    Vancouver, BC Canada
    Posts
    1,323
    Thanks
    26
    Thanked 100 Times in 100 Posts
    This is the code snippet I use to check strings. In the example it has to contain 10 characters or more.

    PHP Code:
    if (strlen($comments) > 10) {
    // do this

    Leonard Whistler

  • #5
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    PHP Code:
    if(empty(trim($someVar))){
      
    // ...

    this include the case when $someVar is filled with white spaces, without trim in rest but usualy is better to use trim, .

    or using strlen like Len Whistler
    PHP Code:
    if(strlen(trim($someVar)) !== 0){
      
    // ...

    best regards
    Last edited by oesxyl; 01-15-2010 at 08:56 AM.

  • #6
    Regular Coder
    Join Date
    Dec 2009
    Location
    UK
    Posts
    495
    Thanks
    0
    Thanked 58 Times in 58 Posts
    If you know for sure the variable is always being set then use the empty method that oesxyl has above. It's rare that you will get a value in a variable that you don't know what it is by the time it's assigned to the variable so programming carfully will mean that this won't happen in the first case. I like to do the following if I have to
    PHP Code:
    if(isset($var) && !empty(trim($var))) {
        ... 
    code here for non empty string
    }else{
        ... empty 
    string code here

    That checks that the var has been assigned a value to begin with too
    My site: JayGilford.com
    Resources:
    PHP Pagination Class | Getting all page links | Handling PHP Errors properly
    If you like a users help, show your appreciation with the rep and thanks buttons :)

  • #7
    Regular Coder Coastal Web's Avatar
    Join Date
    Oct 2004
    Posts
    225
    Thanks
    12
    Thanked 3 Times in 3 Posts
    You can't empty(trim($var))... PHP won't let you call trim() inside of the empty() function, so it would have to be: trim($var) == ''

  • #8
    Regular Coder
    Join Date
    Dec 2009
    Location
    UK
    Posts
    495
    Thanks
    0
    Thanked 58 Times in 58 Posts
    Good point. I can't say I've ever seen that before...shows how often I use the empty function
    My site: JayGilford.com
    Resources:
    PHP Pagination Class | Getting all page links | Handling PHP Errors properly
    If you like a users help, show your appreciation with the rep and thanks buttons :)

  • #9
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by Coastal Web View Post
    You can't empty(trim($var))... PHP won't let you call trim() inside of the empty() function, so it would have to be: trim($var) == ''
    yes, it's my fault, trim is a variable function and is a note to empty about this. That's why is better to test,

    best regards


  •  

    Posting Permissions

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