SitePoint Sponsor

User Tag List

Results 1 to 24 of 24
  1. #1
    derrrp
    Join Date
    Aug 2006
    Location
    earth
    Posts
    923
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    a difference between 0 and "" ?

    hi guys,

    this is a form question. I want the default position of a select box to be at Select One. I have this code outputting a select element.

    PHP Code:
     $options_arr= array("Y","N","S","N/A");
                 
    $values_arr = array(4,0,2,-4);
                 for (
    $i=0;$i<count($options_arr);$i++){
                 
    $selected = ($question9==$values_arr[$i])? ' selected="selected"':'';
                 echo 
    "<option value=\"$values_arr[$i]\" $selected>$options_arr[$i]</option>";
                 } 
    I need to keep the 0 value associated to N. However, when the page loads, $question9 == 0, even when I'm setting it like $question9="" at the beginning of the page.

    I tried this...
    PHP Code:
    $options_arr= array("Select One","Y","N","S","N/A");
    $values_arr = array(NULL,4,0,2,-4); 
    but it still defaults to N. How can I get it to start with "Select One"?

    thanks.
    No, I REALLY dislike having to use Joomla.

  2. #2
    SitePoint Addict miggl's Avatar
    Join Date
    Feb 2007
    Location
    Los Angeles, CA
    Posts
    286
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try this:
    PHP Code:
    $question9 null;

    $options_arr= array("Y","N","S","N/A");
    $values_arr = array(4,0,2,-4);
    for (
    $i=0;$i<count($options_arr);$i++)
    {
        
    $selected = ($question9 == $values_arr[$i]) ? ' selected="selected"' '';
        print(
    '<option value="' $values_arr[$i] . '"' $selected '>' $options_arr[$i] . '</option>';


  3. #3
    SitePoint Addict ruba's Avatar
    Join Date
    Apr 2005
    Location
    Amman -Jordan
    Posts
    339
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you can use === instead == for data type,
    coz 0 = ''

  4. #4
    derrrp
    Join Date
    Aug 2006
    Location
    earth
    Posts
    923
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks for the ideas guys.

    i tried this and it seems to work fine.

    PHP Code:
    $options_arr= array("Select One","Y","N","S","N/A");
                 
    $values_arr = array(NULL,2,"0",1,-2);
                 for (
    $i=0;$i<count($options_arr);$i++){
                 
    $selected = ($question46==$values_arr[$i])? ' selected="selected"':'';
                 echo 
    "<option value=\"$values_arr[$i]\" $selected>$options_arr[$i]</option>";
                 } 
    "0" != NULL, but 0==NULL, ""==NULL, 0==""
    Last edited by crowden; Feb 28, 2007 at 10:14. Reason: correcting my 0 statement
    No, I REALLY dislike having to use Joomla.

  5. #5
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Hmm, is 0 (or false) equivalent to NULL?

  6. #6
    SitePoint Wizard
    Join Date
    Dec 2004
    Location
    At My Desk!!
    Posts
    1,642
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Raffles View Post
    Hmm, is 0 (or false) equivalent to NULL?
    No, 0 or false is still a value, albeit a negative one, whereas NULL is no value assigned
    "Am I the only one doing ASP.NET in Delphi(Pascal)?"

  7. #7
    SitePoint Wizard dreamscape's Avatar
    Join Date
    Aug 2005
    Posts
    1,080
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by webnoob View Post
    No
    Not true. Using "=="; 0, null, false, and an empty string all evaluate to being equal.

    Code:
    php> = (0 == null) ? 'true' : 'false';
    true
    php> = (false == null) ? 'true' : 'false';
    true
    php> = ('' == null) ? 'true' : 'false';
    true
    php>
    The reason what crowden did works is because the string "0" does not evaluate to being equal to null.
    Code:
    php> = ('0' == null) ? 'true' : 'false';
    false
    php>
    See Appendix P. PHP type comparison tables, Table P.2
    <.smarter.web.development.>
    PHP Stuff: Plexus | Chocolate (BDD Framework... coming soon)
    Graphite

  8. #8
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by dreamscape View Post
    Not true.
    Uhm, actually, that's true, they are not equivalent. == is not equivalence operator, === is. There's a difference between equal and equivalent.
    Saul

  9. #9
    SitePoint Wizard
    Join Date
    Dec 2004
    Location
    At My Desk!!
    Posts
    1,642
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I take it using the === is more strict and therefore makes it null?
    "Am I the only one doing ASP.NET in Delphi(Pascal)?"

  10. #10
    SitePoint Guru Ize's Avatar
    Join Date
    Nov 2005
    Location
    The Netherlands
    Posts
    808
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If I'm not mistaken, that has to do with type conversion.
    When two (or more) values of different types are compared (e.g. (0=='')), they get converted to booleans. Every datatype has another way of converting to a boolean, every non-empty string for instance always evaluates to true.

    In other words, since NULL always gets converted to Boolean FALSE, you're comparing (false == false) in every evaluation.

  11. #11
    SitePoint Wizard dreamscape's Avatar
    Join Date
    Aug 2005
    Posts
    1,080
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by php_daemon View Post
    Uhm, actually, that's true, they are not equivalent. == is not equivalence operator, === is.
    No, === is called the "Identical" operator. == is called the "Equal" operator. It is right in the manual, if you care to read it ;-)
    <.smarter.web.development.>
    PHP Stuff: Plexus | Chocolate (BDD Framework... coming soon)
    Graphite

  12. #12
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by dreamscape View Post
    No, === is called the "Identical" operator. == is called the "Equal" operator. It is right in the manual, if you care to read it ;-)
    OK, having misunderstanding in terms here then. Isn't to say it's identical not the same as to say it's equivalent?
    Saul

  13. #13
    SitePoint Wizard dreamscape's Avatar
    Join Date
    Aug 2005
    Posts
    1,080
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by php_daemon View Post
    There's a difference between equal and equivalent.
    No there's not. Please use a dictionary or something. The term "equivalent" basically means "equal to".

    equal (adjective)
    being the same in quantity, size, degree, or value
    equivalent (adjective)
    equal in value, amount, function, meaning, etc.
    identical (adjective)
    similar in every detail; exactly alike
    <.smarter.web.development.>
    PHP Stuff: Plexus | Chocolate (BDD Framework... coming soon)
    Graphite

  14. #14
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Haha, ok then oops, sorry.
    Saul

  15. #15
    SitePoint Wizard bronze trophy Kailash Badu's Avatar
    Join Date
    Nov 2005
    Posts
    2,560
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by dreamscape
    Not true. Using "=="; 0, null, false, and an empty string all evaluate to being equal.
    They evaluate to being equal because of implicit type casting in PHP, yet they are not equal. So when WebNoob said 0 and NULL are not equal, he is correct. if a variable is NULL, it doesn't exist in PHP's symbol table (in effect, they do not exist at all), while 0 , FALSE, and empty string do.

    If you think 0 and FALSE are equivalent (because '==' shows them equal), you are looking for trouble. Certain functions would give you unexpected results if you rely on this logic.

  16. #16
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    In that case, I assume that the following are identical/equal/equivalent/the same thing:
    PHP Code:
    $var null;
    unset(
    $var); 
    Is that right?

  17. #17
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by dreamscape View Post
    The term "equivalent" basically means "equal to".
    In math, and therefore in programming, equivalence is defined as a relation that is reflexive (a = a), symmetric (a = b & b = a) and transitive (a = b & b = c & a = c). Php "equal to" operator is reflexive (10 == 10), symmetric (10 == "10" & "10" == 10), but not transitive ("00" == 0 & 0 == FALSE, but "00" != FALSE), ergo no, "equal to" is not equivalence.

  18. #18
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by stereofrog View Post
    In math, and therefore in programming, equivalence is defined as a relation that is reflexive (a = a), symmetric (a = b & b = a) and transitive (a = b & b = c & a = c). Php "equal to" operator is reflexive (10 == 10), symmetric (10 == "10" & "10" == 10), but not transitive ("00" == 0 & 0 == FALSE, but "00" != FALSE), ergo no, "equal to" is not equivalence.
    I knew I wasn't daydreaming.

    Quote Originally Posted by php_daemon View Post
    Haha, ok then oops, sorry.


    Quote Originally Posted by Raffles View Post
    In that case, I assume that the following are identical/equal/equivalent/the same thing:
    PHP Code:
    $var null;
    unset(
    $var); 
    Is that right?
    Yes
    Saul

  19. #19
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Raffles View Post
    In that case, I assume that the following are identical/equal/equivalent/the same thing:
    PHP Code:
    $var null;
    unset(
    $var); 
    Is that right?
    no

    PHP Code:
    $a null;
    $b 20;
    unset(
    $b);

    echo 
    $a// ok
    echo $b// notice: undefined variable 

  20. #20
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Ha ha, indeed.

    However, $b===null is true even if $b is not set. So virtually you can say they are identical, at least as data types.
    Saul

  21. #21
    SitePoint Wizard dreamscape's Avatar
    Join Date
    Aug 2005
    Posts
    1,080
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by stereofrog View Post
    In math, and therefore in programming, equivalence is defined as a relation that is reflexive (a = a), symmetric (a = b & b = a) and transitive (a = b & b = c & a = c). Php "equal to" operator is reflexive (10 == 10), symmetric (10 == "10" & "10" == 10), but not transitive ("00" == 0 & 0 == FALSE, but "00" != FALSE), ergo no, "equal to" is not equivalence.
    Woa buddy let's put it in context. I'm sure you learned about "context" in school... god I hope so, but it seems maybe not.

    are 0, false, and null equivalent?

    (0 == false and false == null and 0 == null).

    YES, they're equivalent, so stop all the ******** already
    <.smarter.web.development.>
    PHP Stuff: Plexus | Chocolate (BDD Framework... coming soon)
    Graphite

  22. #22
    SitePoint Wizard
    Join Date
    Dec 2004
    Location
    At My Desk!!
    Posts
    1,642
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by dreamscape View Post
    Woa buddy let's put it in context. I'm sure you learned about "context" in school... god I hope so, but it seems maybe not.

    are 0, false, and null equivalent?

    (0 == false and false == null and 0 == null).

    YES, they're equivalent, so stop all the ******** already
    Dude! Show some respect, everybody has their right to say what they feel is correct, hence why the forum is as big as it is and why questions get answered as quick as they do. Maybe you need refreshing on the forum rules..here is the link for you CLICK
    I suggest you read the part specifically on "Don't attack each other" about half way down the page.
    "Am I the only one doing ASP.NET in Delphi(Pascal)?"

  23. #23
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,834
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by php_daemon View Post
    Ha ha, indeed.

    However, $b===null is true even if $b is not set. So virtually you can say they are identical, at least as data types.
    That statement itself is enough to actually create $b and set it to null since that is the effect of referencing $b in the code for the first time anyway. By the time it gets to the comparison $b does exiist and is equal to null and therefore the condition is true when the test takes place even though it wan't before that statement was reached.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  24. #24
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by felgall View Post
    That statement itself is enough to actually create $b and set it to null since that is the effect of referencing $b in the code for the first time anyway.
    Stephen, let me correct you there.

    Implicit variable creation only takes place if variable is on the left side of the assignment, or is passed by reference. Comparison operators never create variables, so "$undef === null" will still raise a notice.


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
  •