murfy Posted December 12, 2013 Share Posted December 12, 2013 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? Quote Link to comment Share on other sites More sharing options...
kicken Posted December 12, 2013 Share Posted December 12, 2013 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 != ";") Quote Link to comment Share on other sites More sharing options...
murfy Posted December 12, 2013 Author Share Posted December 12, 2013 Thank you. That's great! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.