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 10 of 10
  1. #1
    Regular Coder
    Join Date
    Aug 2006
    Posts
    133
    Thanks
    0
    Thanked 0 Times in 0 Posts

    checking whether a variable contains value or not

    hi,


    how to check whether a variable contains some value or not.if the variable contains some value then print.example
    <?php

    $a="abcd";

    echo "the value of a is".$a;

    $b;

    echo "the value of b is".$b;
    ?>
    here the $b contains no value.so i want to hide the echo statement.how i can check that the $b contains some value or not.any of your help will be appreciated

    thanks
    mrjameer

  • #2
    Regular Coder the-dream's Avatar
    Join Date
    Mar 2007
    Location
    Northamptonshire, UK
    Posts
    477
    Thanks
    8
    Thanked 4 Times in 4 Posts
    try something like:

    PHP Code:
    <?php

    $a 
    "abcd";

    if (
    $a == "abcd") {
        echo 
    "The Statement Is True!";
    }

    else {
        echo 
    "The Statement Is False!";
    }

    ?>

  • #3
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    Use isset().

  • #4
    Regular Coder
    Join Date
    Aug 2006
    Posts
    133
    Thanks
    0
    Thanked 0 Times in 0 Posts
    hi,

    i have tested the isset function.working good.
    if i have 100 variables how i can test it.i have to write 100 isset function..

    thanks
    mrjameer

  • #5
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    Sounds like you could benefit from the powers of the Array (and looping) in that situation.

  • #6
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    The function empty() is more effective for this.

  • #7
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    empty() has the distinct disadvantage of returning TRUE if the value is 0 or "0" or FALSE, which can burn you if you use it to check to see if a variable exists (especially booleans and numeric types).

  • #8
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    The question asked was.

    how to check whether a variable contains some value or not
    For which empty() is the best function to use. isset() will return true even when a variable is empty.

  • #9
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    I'm not sure if you're trying to "win", or help the guy out-- empty() has a major caveat that in the OP's case will possibly burn him down the road, which is worth mentioning.

    I never said "don't use empty()".

    (I've got 5 bucks that says aedrin is going to argue some more with me about how his post is right and everyone else is wrong )

  • #10
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,642
    Thanks
    2
    Thanked 405 Times in 397 Posts
    There isn't even an argument. Unless the "value" is a zero, then empty() is the right function to use. As aedrin said isset() will return true if the variable is set, even if the value is blank. ie. $var = "";


  •  

    Posting Permissions

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