Jump to content


Photo

while loop - joining characters to create word

while

  • Please log in to reply
2 replies to this topic

#1 murfy

murfy

    Advanced Member

  • Members
  • PipPipPip
  • 48 posts

Posted 12 December 2013 - 02:10 PM

I read data from binary file which contains names of domains separated by semicolon. I have saved one word "localhost;" into the position.

 

I tried more versions, the main difference is with the use of last condition of the while loop:

$word=$byte="";
fseek( $fh  , $n );echo "n is $n;";                                                                                               
while ( $byte=fread($fh,1) && bindec($byte)!=59) //  semicolon is ord 59 ... && ord($byte)!=0
 {
 echo "<b>".$byte."</b>";
 $word.=$byte;
 }
echo ":$word==".$_SERVER["HTTP_HOST"]."<br>".ord($byte);
if ( $word==$_SERVER["HTTP_HOST"] )
   return false; // this domain is blocked

First of all I tried this:

while ( $byte=fread($fh,1) && $byte!=";")

none works:

while ( $byte=fread($fh,1) && chr($byte)!=";")

also no success:

while ( $byte=fread($fh,1) && ord($byte)!=";")

It should print localhost==localhost ... however I am getting number one instead (repeated ).

If I will skip the last condition, the localhost is printed commonly with the rest of the file (but I need to break it).

 

Any idea what I do wrong?



#2 kicken

kicken

    Wiser? Not exactly.

  • Gurus
  • 2,775 posts
  • LocationBonita, FL

Posted 12 December 2013 - 02:40 PM

while ( $byte=fread($fh,1) && $byte!=";")
That condition is being evaluated as: $byte = (fread($fh,1) && $byte != ";"), in other words $byte is being assigned the result of the && operation, which is a boolean true/false. This is because && has a higher precedence = and is evaluated first.

You need to change the precedence using parenthesis so that the assignment happens first:
while (($byte=fread($fh,1)) && $byte != ";")

Recycle your old CD's, don't trash them!
Did I help you out?  Feeling generous? I accept tips via Paypal or Bitcoin @ 14mDxaob8Jgdg52scDbvf3uaeR61tB2yC7

#3 murfy

murfy

    Advanced Member

  • Members
  • PipPipPip
  • 48 posts

Posted 12 December 2013 - 02:49 PM

Thank you. That's great!






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com