Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Finding average using PHP
Old 04-24-2006, 06:26 PM Finding average using PHP
DJA
DJA's Avatar
Skilled Talker

Posts: 87
Trades: 0
Let say I read data from a file into a variable like so:
$f='31,84,18,135,86'; and I want to find the sverage number in the variable $f how would I do this, also if I can read each line in a text file into a different varable like:
$a='31';
$b='84';
etc
then I would do it that way, or if I could read it into an array I could do that but I dont know how to do any of these things, so I have a file that looks like this:
1
8
4
9
10
3
6
etc
it only has numbers 1 to 10 and I want to read it and find the average and display the average, how would I do this, I prefer code, but if you can give me the functions that works also.
__________________

Please login or register to view this content. Registration is FREE
DJA is offline
Reply With Quote
View Public Profile Visit DJA's homepage!
 
 
Register now for full access!
Old 04-24-2006, 07:53 PM Re: Finding average using PHP
ChancesAre's Avatar
Skilled Talker

Posts: 84
Trades: 0
while is not end of file
read file
total += curnumber
loop

ave = total / lines

This should work
ChancesAre is offline
Reply With Quote
View Public Profile
 
Old 04-24-2006, 08:17 PM Re: Finding average using PHP
DJA
DJA's Avatar
Skilled Talker

Posts: 87
Trades: 0
huh? What do you mean by "while is not end of file" and "loop" I'm not really that great with PHP, and I dont know how to "loop" through a file.
__________________

Please login or register to view this content. Registration is FREE
DJA is offline
Reply With Quote
View Public Profile Visit DJA's homepage!
 
Old 04-24-2006, 08:24 PM Re: Finding average using PHP
ChancesAre's Avatar
Skilled Talker

Posts: 84
Trades: 0
Code:
if( $file =fopen( "file.txt", "r" ) ){
   while( ( $cline = fgets( $file ) ) ){
      $total += $cline;
      $line++;
   }
}

$ave = $total / $lines;
ChancesAre is offline
Reply With Quote
View Public Profile
 
Old 04-24-2006, 08:32 PM Re: Finding average using PHP
DJA
DJA's Avatar
Skilled Talker

Posts: 87
Trades: 0
Thank you! I found the error that you made when you put ave = total / lines whn it was supposed to be line, thank you very much. Talkuptation for you
__________________

Please login or register to view this content. Registration is FREE
DJA is offline
Reply With Quote
View Public Profile Visit DJA's homepage!
 
Reply     « Reply to Finding average using PHP
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.55073 seconds with 12 queries