Problem performing addition on strings.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
swudev
Forum Newbie
Posts: 2
Joined: Thu Jul 30, 2009 12:41 pm

Problem performing addition on strings.

Post 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
straightman
Forum Commoner
Posts: 48
Joined: Sun Apr 19, 2009 5:20 am

Re: Problem performing addition on strings.

Post 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
swudev
Forum Newbie
Posts: 2
Joined: Thu Jul 30, 2009 12:41 pm

Re: Problem performing addition on strings.

Post 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?
ronnietherocket
Forum Newbie
Posts: 5
Joined: Mon Jul 20, 2009 11:38 am

Re: Problem performing addition on strings.

Post by ronnietherocket »

have you tried echo on each of the four things that you read to make sure they are all numbers?
straightman
Forum Commoner
Posts: 48
Joined: Sun Apr 19, 2009 5:20 am

Re: Problem performing addition on strings.

Post 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
straightman
Forum Commoner
Posts: 48
Joined: Sun Apr 19, 2009 5:20 am

Re: Problem performing addition on strings.

Post 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]
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: Problem performing addition on strings.

Post by jackpf »

Turn on error reporting, is the simple answer.
straightman
Forum Commoner
Posts: 48
Joined: Sun Apr 19, 2009 5:20 am

Re: Problem performing addition on strings.

Post 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.
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: Problem performing addition on strings.

Post 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.
Post Reply