Page 1 of 1

Problem performing addition on strings.

Posted: Thu Jul 30, 2009 12:46 pm
by swudev
I am having problems adding four string values that I'm reading from a text file.

It pulls the values successfully.

And I can output those.

But I keep getting an error performing the addition.

Thanks in advance.

Code: Select all

     <?php 
 
     $myFile = "count01.txt";
     $fy = fopen($myFile, 'r');
     $Data01 = fread($fy, filesize($myFile));
     fclose($fy);
 
     $myFile = "count02.txt";
     $fn = fopen($myFile, 'r');
     $Data02 = fread($fn, filesize($myFile));
     fclose($fn);
     
     $myFile = "count03.txt";
     $fn = fopen($myFile, 'r');
     $Data03 = fread($fn, filesize($myFile));
     fclose($fn);
     
     $myFile = "count04.txt";
     $fn = fopen($myFile, 'r');
     $Data04 = fread($fn, filesize($myFile));
     fclose($fn);
 
$DataTotal = (int)$Data01 + (int) $Data02 + (int) $Data03 + (int) $Data04
 
echo"
     1:&nbsp;$Data01<br>
     2:&nbsp;$Data02 <br>
     3:&nbsp;$Data03 <br>
     4:&nbsp;$Data04 
;
?>
Thanks again,

Scott

Re: Problem performing addition on strings.

Posted: Thu Jul 30, 2009 12:51 pm
by straightman
are you closing your double quotes allright?


==========================================================
swudev wrote:I am having problems adding four string values that I'm reading from a text file.

It pulls the values successfully.

And I can output those.

But I keep getting an error performing the addition.

Thanks in advance.

Code: Select all

     <?php 
 
     $myFile = "count01.txt";
     $fy = fopen($myFile, 'r');
     $Data01 = fread($fy, filesize($myFile));
     fclose($fy);
 
     $myFile = "count02.txt";
     $fn = fopen($myFile, 'r');
     $Data02 = fread($fn, filesize($myFile));
     fclose($fn);
     
     $myFile = "count03.txt";
     $fn = fopen($myFile, 'r');
     $Data03 = fread($fn, filesize($myFile));
     fclose($fn);
     
     $myFile = "count04.txt";
     $fn = fopen($myFile, 'r');
     $Data04 = fread($fn, filesize($myFile));
     fclose($fn);
 
$DataTotal = (int)$Data01 + (int) $Data02 + (int) $Data03 + (int) $Data04
 
echo"
     1:&nbsp;$Data01<br>
     2:&nbsp;$Data02 <br>
     3:&nbsp;$Data03 <br>
     4:&nbsp;$Data04 
;
?>
Thanks again,

Scott

Re: Problem performing addition on strings.

Posted: Thu Jul 30, 2009 1:51 pm
by swudev
Thanks for the reply.

Yes, my quotes do close, I have some additional code that I clipped this out of.

It functions fine until I add the line performing the addition.

Any thoughts?

Re: Problem performing addition on strings.

Posted: Thu Jul 30, 2009 1:59 pm
by ronnietherocket
have you tried echo on each of the four things that you read to make sure they are all numbers?

Re: Problem performing addition on strings.

Posted: Thu Jul 30, 2009 2:04 pm
by straightman
I am not seeing that you have the ; statement finishing your equation of the addition.


=======================================================================
straightman wrote:are you closing your double quotes allright?


==========================================================
swudev wrote:I am having problems adding four string values that I'm reading from a text file.

It pulls the values successfully.

And I can output those.

But I keep getting an error performing the addition.

Thanks in advance.

Code: Select all

     <?php 
 
     $myFile = "count01.txt";
     $fy = fopen($myFile, 'r');
     $Data01 = fread($fy, filesize($myFile));
     fclose($fy);
 
     $myFile = "count02.txt";
     $fn = fopen($myFile, 'r');
     $Data02 = fread($fn, filesize($myFile));
     fclose($fn);
     
     $myFile = "count03.txt";
     $fn = fopen($myFile, 'r');
     $Data03 = fread($fn, filesize($myFile));
     fclose($fn);
     
     $myFile = "count04.txt";
     $fn = fopen($myFile, 'r');
     $Data04 = fread($fn, filesize($myFile));
     fclose($fn);
 
$DataTotal = (int)$Data01 + (int) $Data02 + (int) $Data03 + (int) $Data04
 
echo"
     1:&nbsp;$Data01<br>
     2:&nbsp;$Data02 <br>
     3:&nbsp;$Data03 <br>
     4:&nbsp;$Data04 
;
?>
Thanks again,

Scott

Re: Problem performing addition on strings.

Posted: Thu Jul 30, 2009 2:05 pm
by straightman
I am not seeing that you have the ; statement finishing your equation of the addition.


===================================================================================


  
$DataTotal = (int)$Data01 + (int) $Data02 + (int) $Data03 + (int) $Data04
 
echo"
     1:&nbsp;$Data01<br>
     2:&nbsp;$Data02 <br>
     3:&nbsp;$Data03 <br>
     4:&nbsp;$Data04
;
?>

Thanks again,

Scott[/quote][/quote][/quote]

Re: Problem performing addition on strings.

Posted: Thu Jul 30, 2009 2:12 pm
by jackpf
Turn on error reporting, is the simple answer.

Re: Problem performing addition on strings.

Posted: Thu Jul 30, 2009 2:18 pm
by straightman
yes Jackpf, but dont you think that it is because he has left the ; out? i dont see it in his equation of addition

===================================================================================

jackpf wrote:Turn on error reporting, is the simple answer.

Re: Problem performing addition on strings.

Posted: Thu Jul 30, 2009 2:26 pm
by jackpf
I'm pretty sure it is because he's missing a semi-colon. It's probably also because he's missing a quotation mark.

But, if he turns on error reporting, it'll show him that he has errors on those particular lines.

But yes, you are right about what's causing the errors :) I'm just trying to show how he should go about debugging these errors now, and in the future.