1

i have a script what give is working via $ErrorCode = 0x1 or 0x2 or 0x4 or 0x8 ...

this code is working is give me the right status msg:

<div class="left_content">Submit:</div>
<div class="right_content">
    <?php echo ($errorCode & 0x1) != 0x1 
            ? "<span class='green'>Ok</span>" 
            : "<span class='red'>Fail</span>"; ?>
</div>

but i try to change the code with this, but this code all time the status msg is "Ok" even if is fail

if(($errorCode & 0x1) != 0x1 ) 
{ 
    $Error_0x1 = "<span class='green'>Ok</span>";  
} else 
{ 
    $Error_0x1 = "<span class='red'>Fail</span>"; 
}

what im doing wrong ?

randak
  • 1,941
  • 1
  • 12
  • 22
Matei Zoc
  • 3,739
  • 3
  • 16
  • 18
  • Why are you comparing it to `0x1`? do you mean the string `'0x1'`? – Jad Joubran Dec 14 '13 at 08:17
  • the first code is not made by me...i just want to convert the code... – Matei Zoc Dec 14 '13 at 08:23
  • The 2 codes are correct, meaning the second is the "long" version of the ternary operation above. What is the context of this code? Did you keep it or are you trying to use it in another context/system? – Damien Pirsy Dec 14 '13 at 08:38
  • are you getting errors ? – nl-x Dec 14 '13 at 08:43
  • with the same $errorCode they are more 0x1 and 0x2 and 0x4 and 0x8 ... the total value of $errorCode is 163, so if 0x1 is on $errorCode to show true or fail ... on the class like this is added the error: $this->error+=0x80; – Matei Zoc Dec 14 '13 at 08:45
  • Show us more code! It looks okay if the previous code was already working. You probably have a mistake elsewhere. – nl-x Dec 14 '13 at 09:11

2 Answers2

1

Looking at your code, all I miss is the actual echo $Error_0x1; and the surrounding HTML tags.

So this should be your entire code:

<?php
    if(($errorCode & 0x1) != 0x1 )
    {
        $Error_0x1 = "<span class='green'>Ok</span>";
    } else
    {
        $Error_0x1 = "<span class='red'>Fail</span>";
    }
?>
<div class="left_content">Submit:</div>
<div class="right_content">
    <?php echo $Error_0x1; ?>
</div>

By the way ... 0x1 just means 1 in a binary format.

What $errorCode & 0x1 actually does is look at which bits of $errorCode overlap with the bits of 0x1 (binary 1). In this case it would return a 1 in case of odd numbers and a 0 in case of even numbers.

163: 10100011
0x1: 00000001 &
---------------
     00000001

So if $errorCode is 163, your code now states if( (1) != 1). So it should read FAIL. If it doesn't, look at if you placed your rewritten code at a wrong place, or the variables are not yet set or something...

Community
  • 1
  • 1
nl-x
  • 11,762
  • 7
  • 33
  • 61
  • the value of $errorCode is not 0x1, the value of $errorCode = 163 so need something like if((163 & 0x1) != 0x1 ) but this is not working – Matei Zoc Dec 14 '13 at 08:54
  • @MateiZoc I extended my answer with more detail. All I did first is compare the code you say is working (ternary) against the code you say is not working (normal if-else). They should do the same. – nl-x Dec 14 '13 at 09:04
-1
if(($errorCode != '0x1' ) 
{ 
    $Error_0x1 = "<span class='green'>Ok</span>";  
} else 
{ 
    $Error_0x1 = "<span class='red'>Fail</span>"; 
}

this should do the trick. In case it didn't work, try replaceing '0x1' with 0x1

Jad Joubran
  • 2,511
  • 3
  • 31
  • 57
  • You're comparing to the string '0x1', this is different from the original code – Damien Pirsy Dec 14 '13 at 08:28
  • yeah but the first code is not correct, I guess it's working for him by mistake (he's not trying the proper scenarios) – Jad Joubran Dec 14 '13 at 08:30
  • 1
    I don't see it wrong...I usually check errors in a different way not using bit and exadecimals, but who knows. Comparing to the string '0x1' si pretty different. I would compare with a boolean, if it were me – Damien Pirsy Dec 14 '13 at 08:35
  • i try the code but is not working $errorCode is not 0x1, with the same $errorCode they are more 0x1 and 0x2 and 0x4 and 0x8 ... the total value of $errorCode is 163, so if 0x1 is on $errorCode to show true or fail ... on the class like this is added the error: $this->error+=0x80; – Matei Zoc Dec 14 '13 at 08:41