Ask a Question related to PHP Development, Design and Development.

  1. #1

    Default if (!0) means what?

    Recently I made this mistake:

    if (!0){
    echo "I am true";
    }
    else {
    echo "I am false";
    }

    It always evaluated to "true". What does (!0) equate to in plain language?
    Why is it always true.

    Al

    Adams-Blake Company Guest

  2. Similar Questions and Discussions

    1. SWC,SWF Means
      What is the abbrevation of .SWC , .SWF formats. please any one give me reply
    2. what does (gray#) means
      hi there, from a client we received some grayscale images (just black and white scannings I think), that when opened in Photoshop 7 inside the...
    3. chkconfig: - 60 20 ; what does means?
      I found following script line in /etc/rc.d/init.d/nfs chkconfig: - 60 20 someone tell me what does above line means? I think start priority 60...
    4. Can you tell me what this coding means?
      One of our employees received an email (spam) that contained some code that didn't make sense to us. In the source code you will see the words...
    5. What means Protected WithEvents ?
      The keyword 'protected' is an access modifier. You can get started understanding what is going on by opening your MSDN Library, look for the term...
  3. #2

    Default Re: if (!0) means what?


    "Adams-Blake Company" <atakeoutcanton@adams-blaketakeout.com> wrote in
    message news:vsvu58f0s0hq9c@news20.forteinc.com...
    > Recently I made this mistake:
    >
    > if (!0){
    > echo "I am true";
    > }
    > else {
    > echo "I am false";
    > }
    >
    > It always evaluated to "true". What does (!0) equate to in plain language?
    > Why is it always true.
    >
    > Al
    >

    Ones & Zeros, On or Off, True or False

    !0 equates to not zero - which must then be 1 - or true

    so, if (true) - is always true




    Guest

  4. #3

    Default Re: if (!0) means what?

    Adams-Blake Company wrote:
    > Recently I made this mistake:
    >
    > if (!0){
    > echo "I am true";
    > }
    > else {
    > echo "I am false";
    > }
    >
    > It always evaluated to "true". What does (!0) equate to in plain language?
    > Why is it always true.
    >
    > Al
    >
    Have a look at: [url]http://www.blueshoes.org/en/developer/php_cheat_sheet/[/url]

    Looks like '0' always evaluates to false, so essentially you're just
    returning 'not false' which is always true ;)



    J0hn Sm1th Guest

  5. #4

    Default Re: if (!0) means what?

    <xyzzy> wrote:
    >
    > "Adams-Blake Company" <atakeoutcanton@adams-blaketakeout.com> wrote in
    > message news:vsvu58f0s0hq9c@news20.forteinc.com...
    >> Recently I made this mistake:
    >>
    >> if (!0){
    >> echo "I am true";
    >> }
    >> else {
    >> echo "I am false";
    >> }
    >>
    >> It always evaluated to "true". What does (!0) equate to in plain
    >> language? Why is it always true.
    >>
    >> Al
    >>
    >
    >
    > Ones & Zeros, On or Off, True or False
    >
    > !0 equates to not zero - which must then be 1 - or true
    >
    > so, if (true) - is always true
    I had not thought of it in that way.

    Thanks. And thanks to the person who posted the URL to the Cheat Sheet. Very
    helpful.

    Al

    Adams-Blake Company Guest

  6. #5

    Default Re: if (!0) means what?

    Not quite true. Boolean is a distinct type in PHP. ! is a boolean operator,
    so 0 first get convert into boolean (to false), then the expression is
    evaluated. The difference between 0 and false is illustrated by the
    following examples:

    // Example A
    if (!"Bush") {
    echo "I am true";
    }
    else {
    echo "I am false";
    }

    // Example B
    if ("Bush" == 0) {
    echo "I am zero";
    }
    else {
    echo "I am not zero";
    }

    Example A prints "I am false", because a non-empty string converts to true.
    Example B on the other hand, prints "I am zero" because "Bush" converts to
    0. Thus, we get the following logic:

    false == 0
    0 == "Bush"
    false != "Bush"

    In linear algebra I think you would say "PHP variables do not form a vector
    space."

    Uzytkownik <xyzzy> napisal w wiadomosci
    news:NaadnYu0CvoVZ1KiRVn-vw@comcast.com...
    >
    > "Adams-Blake Company" <atakeoutcanton@adams-blaketakeout.com> wrote in
    > message news:vsvu58f0s0hq9c@news20.forteinc.com...
    > > Recently I made this mistake:
    > >
    > > if (!0){
    > > echo "I am true";
    > > }
    > > else {
    > > echo "I am false";
    > > }
    > >
    > > It always evaluated to "true". What does (!0) equate to in plain
    language?
    > > Why is it always true.
    > >
    > > Al
    > >
    >
    >
    > Ones & Zeros, On or Off, True or False
    >
    > !0 equates to not zero - which must then be 1 - or true
    >
    > so, if (true) - is always true
    >
    >
    >
    >

    Chung Leong Guest

  7. #6

    Default Re: if (!0) means what?


    "Chung Leong" <chernyshevsky@hotmail.com> wrote in message
    news:CsidnY5G26w9hE2iRVn-sQ@comcast.com...
    > Not quite true. Boolean is a distinct type in PHP. ! is a boolean
    operator,
    > so 0 first get convert into boolean (to false), then the expression is
    > evaluated. The difference between 0 and false is illustrated by the
    > following examples:
    >
    > // Example A
    > if (!"Bush") {
    > echo "I am true";
    > }
    > else {
    > echo "I am false";
    > }
    >
    > // Example B
    > if ("Bush" == 0) {
    > echo "I am zero";
    > }
    > else {
    > echo "I am not zero";
    > }
    >
    > Example A prints "I am false", because a non-empty string converts to
    true.
    > Example B on the other hand, prints "I am zero" because "Bush" converts to
    > 0. Thus, we get the following logic:
    >
    > false == 0
    > 0 == "Bush"
    > false != "Bush"
    >
    > In linear algebra I think you would say "PHP variables do not form a
    vector
    > space."
    >
    > Uzytkownik <xyzzy> napisal w wiadomosci
    > news:NaadnYu0CvoVZ1KiRVn-vw@comcast.com...
    > >
    > > "Adams-Blake Company" <atakeoutcanton@adams-blaketakeout.com> wrote in
    > > message news:vsvu58f0s0hq9c@news20.forteinc.com...
    > > > Recently I made this mistake:
    > > >
    > > > if (!0){
    > > > echo "I am true";
    > > > }
    > > > else {
    > > > echo "I am false";
    > > > }
    > > >
    > > > It always evaluated to "true". What does (!0) equate to in plain
    > language?
    > > > Why is it always true.
    > > >
    > > > Al
    > > >
    > >
    > >
    > > Ones & Zeros, On or Off, True or False
    > >
    > > !0 equates to not zero - which must then be 1 - or true
    > >
    > > so, if (true) - is always true
    > >
    I disagree.
    Yes, strings can evaluate to true and false if they are null or !null
    However, zero is numeric until you convert it to Boolean by using the !

    <?
    $a = 0;

    echo "a = $a\n";
    echo "!a = " . !$a . "\n";
    echo "is_bool(a) ".($a ? "true":"false") . "\n";
    echo "is_bool(!a) ".(!$a ? "true":"false") . "\n";
    ?>

    *** produces ***

    a = 0
    !a = 1
    is_bool(a) =false
    is_bool(!a) =true









    Guest

Posting Permissions

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139