Professional Web Applications Themes

Whats the diffecence between '&' and '&&' operators? - PHP Development

Both '&' and '&&' are 'AND' right? Or what about 'ORs' '|' and '||'? Whats the difference? I not grok.......

  1. #1

    Default Whats the diffecence between '&' and '&&' operators?

    Both '&' and '&&' are 'AND' right? Or what about 'ORs' '|' and '||'?

    Whats the difference? I not grok....
    Jam Guest

  2. #2

    Default Re: Whats the diffecence between '&' and '&&' operators?

    "Jam Pa" wrote:
     

    Visit <http://php.net/operators> and take a look at the sections on "Bitwise
    operators" and "Logical operators"

    --
    phil [dot] ronan virgin [dot] net
    http://vzone.virgin.net/phil.ronan/


    Philip Guest

  3. #3

    Default Re: Whats the diffecence between '&' and '&&' operators?

    "Jam Pa" <org> a écrit dans le message de news:
    243.153.2... 

    Hi,
    It takes me 1/4 seconds on Google :
    http://www.php.net/manual/en/language.operators.bitwise.php
    Bye,


    \(¯`·..Yttrium Guest

  4. #4

    Default Re: Whats the diffecence between '&' and '&&' operators?

    > "Jam Pa" <org> a écrit dans le message de news: [/ref]

    "\(¯`·..Yttrium ...·´¯\)" <fr> wrote in
    news:429494a3$0$10517$free.fr: 

    Gee, you are quick. Good job, einstein.

    Philip Ronan replied: 

    The thing is, I did already read the manual.

    Does anyone have actual clearheaded answer instead of links and snappy
    comments?

    JP
    Jam Guest

  5. #5

    Default Re: Whats the diffecence between '&' and '&&' operators?

    Jam Pa <org> wrote in
    news:243.153.2:
     

    Here is an example test for those interested.

    <?php
    $a = "nil"; $b = "nil";
    if (($a=true) | ($b=false)) { echo " * 1: true, a: $a , b: $b <br>"; }
    $a = "nil"; $b = "nil";
    if (($a=false) | ($b=true)) { echo " * 2: true, a: $a , b: $b <br>"; }
    $a = "nil"; $b = "nil";
    if (($a=true) || ($b=false)) { echo " * 3: true, a: $a , b: $b <br>"; }
    $a = "nil"; $b = "nil";
    if (($a=false) || ($b=true)) { echo " * 4: true, a: $a , b: $b <br>"; }
    $a = "nil"; $b = "nil";
    ?>

    output with comments
    * 1: true, a: 1 , b:

    * 2: true, a: , b: 1
    - both evaluated above

    * 3: true, a: 1 , b: nil
    - only first one evaluated (it was true)
    )
    * 4: true, a: , b: 1
    - both

    Hmm, not sure if I'm really any wiser...
    Jam Guest

  6. #6

    Default Re: Whats the diffecence between '&' and '&&' operators?

    "Jam Pa" <org> kirjoitti
    viestissä:243.153.2... 

    & and | are bitwise arithmetic operators while && and || are boolean
    operators. What this maeans is that & and | handle single bits in and
    integer, but && and || evaluate the entire integer as true or false.

    For example we have and integer 4. when it is splitted in bits it looks like
    0100
    ( = 0*(2^3) + 1*(2^2) + 0*(2^1) + 0*(2^0) )
    And we have integer 8 that's 1000
    ( = 1*(2^3) + 0*(2^2) + 0*(2^1) + 0*(2^0) )

    Now, comparing them (8 && 4) this returns TRUE, since both are none-zero
    (there is at least one bit other than zero in both of them.

    Bitwise operation on the other hand: 8 & 4 will return zero. This is because
    it compares each single bit:
    8 & 4 = 0
    -----------
    1 and 0 = 0
    0 and 1 = 0
    0 and 0 = 0
    0 and 0 = 0

    Now OR then. 4 || 8 returns TRUE, since within the two operators there is at
    least one none-zero bit. Bitwise operation 8 | 4 returns 12. Let's see that
    again in bitwise:

    8 | 4 = 12
    -----------
    1 or 0 = 1
    0 or 1 = 1
    0 or 0 = 0
    0 or 0 = 0

    because 1100 is the binary representation of 12. It's not the same as 8 + 4,
    that was just a coincidence. 8 | 8 returns 8. :)

    8 | 8 = 8
    -----------
    1 or 1 = 1
    0 or 0 = 0
    0 or 0 = 0
    0 or 0 = 0

    See the difference? When using && and || we just look if the integer is zero
    or non-zero, but using & and | (and xor ^) we perform bitwise arithmetic.

    AND and OR are not the same as && and ||, but they are closer to them, than
    to & and |. I never understood completely the difference, but there is a
    section in the manual that trys to explain it, but I just can't find it now.
    All I found was this: "The reason for the two different variations of "and"
    and "or" operators is that they operate at different precedences. " (PHP
    Manual). Operator precendence is all about in what order the expression is
    evaluated.

    --
    "I am pro death penalty. That way people learn
    their lesson for the next time." -- Britney Spears

    com


    Kimmo Guest

  7. #7

    Default Re: Whats the diffecence between '&' and '&&' operators?

    "Kimmo Laine" <com> wrote in
    news:d727g3$9e$kolumbus.fi: 
    [a LOT deleted]

    Thank juu Kimmo! Jotain tollaista mun kokeilutkin indikoi! :) Salatiedettä!

    So, what should one use when comparing in if clauses or ternary ? : ops
    when evaluating the validity of user input, for instance?

    Is this better?
    if ($gee && $whiz) { //do shiz)

    or perhaps
    if ($gee AND $whiz) { //do... }

    Jp
    Jam Guest

  8. #8

    Default Re: Whats the diffecence between '&' and '&&' operators?

    "Jam Pa" <org> kirjoitti
    viestissä:243.153.2... 
    > [a LOT deleted]
    >
    > Thank juu Kimmo! Jotain tollaista mun kokeilutkin indikoi! :)
    > Salatiedettä![/ref]

    Eipä kestä...
     

    Seems to me that in that particular case there is no difference. Reading the
    manual I found an example that sort of demonstrates the effect of OR.

    $var0 = "";
    $var1 = "bunny";
    $default = "default";

    $target = $var0 || $var1 || $default;
    echo $target; //outputs 1
    $target = $var0 OR $var1 OR $default;
    echo $target; //outputs bunny

    Why "bunny"? Seems that using or the operand is assigned into $target, and
    if it's true, it halts. It is evaluated like this:

    (($target = $var0) || $target = $var1) || $target = $default;

    $target = $var0 is false, since $var0 is empty, but $target = $var1 is true
    ("bunny" is true) so it halts, since "true or something" is always true, it
    "won't bother" to check $default.

    --
    "I am pro death penalty. That way people learn
    their lesson for the next time." -- Britney Spears

    com


    Kimmo Guest

  9. #9

    Default Re: Whats the diffecence between '&' and '&&' operators?

    Kimmo Laine wrote: 
    <...SNIP...> 

    No it doesn't. The result is an empty string.
     

    No it's not. The line is evaluated as:

    ($target = $var0) || $var1 || $default;



    --
    Oli
    Oli Guest

  10. #10

    Default Re: Whats the diffecence between '&' and '&&' operators?

    "Oli Filth" <co.uk> kirjoitti
    viestissä:7R2le.34$ntli.net... 
    > <...SNIP...> 
    >
    > No it doesn't. The result is an empty string.

    >
    > No it's not. The line is evaluated as:
    >
    > ($target = $var0) || $var1 || $default;
    >[/ref]


    Ah... So it does, I had to test it. Well in that case the user comment at
    php.net was incorrect... or I understood it wrong... :(

    --
    "I am pro death penalty. That way people learn
    their lesson for the next time." -- Britney Spears

    com


    Kimmo Guest

Similar Threads

  1. Operators
    By sai1 in forum Macromedia Dynamic HTML
    Replies: 2
    Last Post: January 2nd, 08:00 PM
  2. = <> SQL operators in components
    By cf_matt in forum Coldfusion - Advanced Techniques
    Replies: 4
    Last Post: May 18th, 12:38 PM
  3. [PHP-DEV] regex operators
    By David Sklar in forum PHP Development
    Replies: 30
    Last Post: October 16th, 04:58 PM
  4. What are the !== and === operators for?
    By vbAlex in forum PHP Development
    Replies: 4
    Last Post: September 12th, 10:02 AM

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
  •  

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