Jump to content

while loop - joining characters to create word


murfy

Recommended Posts

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?

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 != ";")

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.