Page 1 of 1

Question about a php variable ?

Posted: Thu Jan 26, 2006 9:12 am
by pmzq
Hello all,

I'm just starting with PHP.

I've got a source code of an online mass multyplayer web game.
I'm studying te php code when I came along this:

if (mysql_num_rows($result) == 1) {
$myrow = mysql_fetch_row($result);

if (($myrow[1] & 0x0F) == 4 )

So what I don't understand were stands the 0x0F for ?

And if I read it the right way this tells me that if mysql_num_rows($rsult) = 1 the $myrow = 1 right ? or ALL wrong ?

If that is so then in the next line $myrow[1] = 1 and after that I'm clueless what does the "&" mean and the 0x0F ???

Hope you can help me

thnx
pMzQ,[/b]

Posted: Thu Jan 26, 2006 9:39 am
by feyd
0x0F is the hex code for 15 (decimal)


$myrow[1] is being compared on the bit level if it has commonality with the number 15 or 00001111 in bits. The results of this bitmask are compared against 4 or 00000100 in bits. Basically, it's assessing if the lower nibble of the second element from $myrow is 4.

And if I read it the right way this tells me that if mysql_num_rows($rsult) = 1 the $myrow = 1 right ? or ALL wrong ?
it's setting $myrow to the resultant record if there is only one row in the result set. i.e. if mysql_num_rows() returns 1, set $myrow to the first (and only) record in the result set.

Posted: Thu Jan 26, 2006 10:01 am
by pmzq
thnx for your respons.

So the statement

if (($myrow[1] & 0x0F) == 4 )

Isn't true then? right because 0x0F is 15 so no 4 and means this statement isn't true ?

thnx
pMzQ,

Posted: Thu Jan 26, 2006 11:06 am
by feyd
it actually can be true. The & operator does a bitmask. The bits matching between the two variables are returned. Here's an example

Code: Select all

00001111 = 15 (0x0F)
& 00000100 =  4 (0x04)
----------
  00000100 =  4 (0x04)

  01010101 = 85 (0x55)
& 00001111 = 15 (0x0F)
----------
  00000101 =  5 (0x05) != 4

Posted: Thu Jan 26, 2006 6:18 pm
by raghavan20
pmzq wrote:thnx for your respons.

So the statement

if (($myrow[1] & 0x0F) == 4 )

Isn't true then? right because 0x0F is 15 so no 4 and means this statement isn't true ?

thnx
pMzQ,
As feyd told you, all characters within 0-9, A-F which starts with 0x as prefix are hexadecimals.
The if condition might yield a Boolean TRUE if $myrow[1] when it makes an AND operation against 0x0F (15) return 0x04(4). It all depends on the value taken by the $myrow[1]

Code: Select all

for simplicity, i am considering values to be only of four digits or just one hexadecimal character
1111- 15
0111 - 7
0101 - 5
0100 - 4
  AND
0100 - 4
 WILL RETURN 0100
you know AND, only 1 and 1 yields 1 all others are zero. so , if you AND any of the four numbers I have given above with 0100 it will yield TRUE all other combinations of four digits yield FALSE.