Softkites Posted March 5, 2009 Share Posted March 5, 2009 Hi all, I am sort of a newbie at this and I have been trying to find the answer through online tutorials but have not been able to find the stuff I want. That is why I came to the professionals and hope that one of you can help me. I just want to make a simple addition. I have a page counter set up that plants the outcome to a txt file. The txt file has nothing but a number in it. Now I know how to print out that number by using either "include" or "require", but I want to use that number from the txt file in an addition. I have placed a simple code below to give you an idea to what I am after. I want to add last months total to what is going on right know, so the number in the txt file is variable but last months number of course isn't so we can just write that down. Can anybody help me solve this? <?php $last_month = 243; $index_file = "inc/index.txt"; //this is where the fault is but how?? $a = $last_month; $b = $index_file; $c = $a+$b; echo 'Last month we had ';echo $a ;echo '<br>'; echo 'So far this month we have '; echo $b ;echo '<br>'; echo 'That is a temporary total of '; echo $c ; ?> Steady Breezes all Softkites Link to comment https://forums.phpfreaks.com/topic/148087-solved-simple-addition/ Share on other sites More sharing options...
revraz Posted March 5, 2009 Share Posted March 5, 2009 What your code is trying to do is add 243 to the actual file name of the file and not the contents of that file. You will need to fopen that file to read the contents. Link to comment https://forums.phpfreaks.com/topic/148087-solved-simple-addition/#findComment-777327 Share on other sites More sharing options...
rhodesa Posted March 5, 2009 Share Posted March 5, 2009 <?php $last_month = 243; $index_file = "inc/index.txt"; $a = $last_month; $b = (int)file_get_contents($index_file); $c = $a+$b; echo 'Last month we had ';echo $a ;echo '<br>'; echo 'So far this month we have '; echo $b ;echo '<br>'; echo 'That is a temporary total of '; echo $c ; ?> Link to comment https://forums.phpfreaks.com/topic/148087-solved-simple-addition/#findComment-777328 Share on other sites More sharing options...
Softkites Posted March 5, 2009 Author Share Posted March 5, 2009 <?php $last_month = 243; $index_file = "inc/index.txt"; $a = $last_month; $b = (int)file_get_contents($index_file); $c = $a+$b; echo 'Last month we had ';echo $a ;echo '<br>'; echo 'So far this month we have '; echo $b ;echo '<br>'; echo 'That is a temporary total of '; echo $c ; ?> Boy, thanks for the fast reaction, That gave me this error: Fatal error: Call to undefined function: file_get_contents() in c:\phpdev\www\....\totalplus.php on line 246 Sorry, it did work. Thank you so much, boy I should have come here yesterday. Take care, Link to comment https://forums.phpfreaks.com/topic/148087-solved-simple-addition/#findComment-777341 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.