Basic Statistics Class

Basic Statistics Class

Released 7 years ago , Last update 6 years ago

All the basics of statistics in one simple class.

This PHP Statistics class allows you to calculate the basic statistical formulas for a given array of scores(values):


  • Scores (X values)
  • Frequency (F values)
  • FX (F * X)
  • XminA (X - (X mean))
  • XminAsqr (X - (X mean) squared)
  • Relative Frequency (RF)
  • Relative Frequency Percentages (RF * 100)
  • Cumulative Frequency (CF)
  • Population Size (N)
  • Sample Size (N - 1)

Measures of Central Location

  • Mean
  • Median
  • Mode

Measures of Dispersion

  • Sample Variance
  • Population Variance
  • Sample Standard Deviation
  • Population Standard Deviation
  • Range
  • Interquartile Range
  • Quartile 1, 2 and 3
  • Highest Value
  • Lowest Value
  • 5 2
  • 4 1
  • 3 0
  • 2 0
  • 1 0
3 Reviews Read Reviews


14 day 14-day money-back guarantee


Personal License

  • Perpetual license

  • 1 site, unlimited servers

  • No distribution (hosted use only)

  • Commercial use


Package Contents

The package contains the following files:

  • statistics.class.php
  • example.php

Including for Use

Simply upload the statistics.class.php file to a directory of your choice and include it in your website like so:



Using the Class

First you need an array of numerical values to feed into the class, they can either be whole numbers or decimal numbers:

$scores = array(4,7,3,8,5,2,7); // Whole Numbers (acceptable)
$scores = array(4.2,7.1,3.3,8.9,2.0,7.1); // Decimal Numbers (acceptable)
$scores = array(4,7.1,3.3,8,5,2,7.1); // Combination of both (acceptable)

Then you simple call the class, feeding the array into it as an argument:

$stats = new Statistics($scores);

By default the class rounds all values to two decimal places. You can make it round to however many decimal places as you like by feeding it in as the second argument:

$stats = new Statistics($scores,5); // all values rounded to five decimal places

Before you can get your results, you must first produce them by the calling the methods you require. For example; if you want the mean of your values you must first call the method which calculates the mean:

$stats->mean = $stats->Find_Mean();

Once you have done that, you can then select the mean.

Calculation Methods


  • Calculate_F (Frequency)
  • Calculate_FX (F * X)
  • Calculate_XminAvg(square root: true/false | yes/no) (X - (X Mean) squared) or (X - (X Mean))
  • Calculate_RF (Relative Frequency)
  • Calculate_RFP (Relative Frequency Percentages)
  • Calculate_CF (Cumulative Frequency)

Measures of Central Location

  • Find_Mean
  • Find_Median
  • Find_Mode

Measures of Dispersion

  • Find_Range
  • Find_IQR (Interquartile Range)
  • Find_V(Population or Sample: 'p' or 's') (Sample/Population Variance)
  • Find_SD(Population or Sample: 'p' or 's') (Sample/Population Standard Deviation)
  • Find_Q (Quartiles 1, 2 and 3)
  • Find_Max (Highest Value)
  • Find_Min (Lowest Value)

Selecting Stats

There is two methods you can use to select the results of the statistics class:

  1. using $stats->var
  2. using the Get methods within the class

1. Selecting Stats with $stats->var

You can simply select the stats via the property:

echo $stats->mean // displays the mean

The following is all the stats property names:


  • scores
  • frequency
  • fx (F * X)
  • XminA (X - (X mean))
  • XminAsqr (X - (X Mean) squared)
  • rf (Relative Frequency)
  • rfp (Relative Frequency Percentages)
  • cf (Cumulative Frequency)
  • pn (Population Size)
  • sn (Sample Size (Population Size - 1))

Measures of Central Location

  • mean
  • median
  • mode

Measures of Dispersion

  • range
  • iqr (Interquartile Range)
  • pv (Population Variance)
  • sv (Sample Variance)
  • psd (Population Standard Deviation)
  • ssd (Sample Standard Deviation)
  • q[1] (Quartile 1, lower quartile)
  • q[2] (Quartile 2)
  • q[3] (Quartile 3, upper quartile)
  • max (Highest Value)
  • min (Lowest Value)

2. Using the Get methods within the class

You can select the stats via the Get methods in the class:

echo $stats->Get_Mean();

The following is all the stats Get method names:


  • Get_Scores
  • Get_Frequency
  • Get_FX (F * X)
  • Get_XminAvg (X - (X mean))
  • Get_XminAvgsqr (X - (X Mean) squared)
  • Get_RF (Relative Frequency)
  • Get_RFP (Relative Frequency Percentages)
  • Get_CF (Cumulative Frequency)
  • Get_PN (Population Size)
  • Get_SN (Sample Size)

Measures of Central Location

  • Get_Mean
  • Get_Median
  • Get_Mode

Measures of Dispersion

  • Get_Range
  • Get_IQR (Interquartile Range)
  • Get_PV (Population Variance)
  • Get_SV (Sample Variance)
  • Get_PSD (Population Standard Deviation)
  • Get_SSD (Sample Standard Deviation)
  • Get_Q (Quartiles 1, 2 and 3)
  • Get_Max (Highest Value)
  • Get_Min (Lowest Value)
2 licenses, starting from From » FREE View Licenses

Get A Quote

What do you need?
  • Custom development
  • Integration
  • Customization / Reskinning
  • Consultation
When do you need it?
  • Soon
  • Next week
  • Next month
  • Anytime

Thanks for getting in touch!

Your quote details have been received and we'll get back to you soon.

Or enter your name and Email
  • A Andrea 4 years ago
    Hello, I have tried your class Basic Statistics Class and it is great but it seems doesen't work with not-numerical values, for example with the function Find_Mode. How can I do? Thank you in advance
  • T tibaredha 5 years ago
    good good thanks