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?

Link to comment
Share on other sites

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 != ";")
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.