How do i find the average of 3 numbers in php?

In this example, we are going to show you how to calculate an “average number” using PHP.

Although this does require some basic math, PHP’s inbuilt functions do make it a little easier.

For example, let’s say that we have a PHP array that contains various numbers:

//Our array, which contains a set of numbers.
$array = array[1, 7, 9, 3, 20, 12, 2, 9];

//Calculate the average.
$average = array_sum[$array] / count[$array];

//Print out the average.
echo $average;

If you run the code above, you will see that the answer is 7.875.

To round that number up, we can use the ceil function like so:

//Our average is 7.875
$average = 7.875;

//Round it up.
$averageRounded = ceil[$average];

A full example:

//Our array, which contains a set of numbers.
$array = array[1, 7, 9, 3, 20, 12, 2, 9];

//Calculate the average and round it up.
$average = ceil[ array_sum[$array] / count[$array] ];

//Print out the average.
echo $average;

As you can see, it’s actually pretty simple.

An explanation of the code above:

  1. We have an array that contains a set of numbers.
  2. We add those numbers together using the array_sum function.
  3. After that, we count how many numbers are in our “set” by using the count function.
  4. Finally, we divide the sum by the count and then round the result up using the ceil function.

This is by far the best way to calculate an average number in PHP. By putting our numbers into an array, we can count the size of that array instead of manually specifying the size of the set.

If you had to manually calculate the average, it would look something like this:

//Our numbers.
$num1 = 1;
$num2 = 43;
$num3 = 23;

//How many numbers are in our set.
$numbersInSet = 3;

//Get the sum of those numbers.
$sum = $num1 + $num2 + $num3;

//Calculate the average by dividing $sum by the
//amount of numbers that are in our set.
$average = $sum / $numbersInSet;

As you can, this example is far more complicated as you have to do the sum and then manually specify how many numbers are in the set.

Facebook

邮箱或手机号 密码

忘记帐户?

注册

无法处理你的请求

此请求遇到了问题。我们会尽快将它修复。

  • 返回首页

  • 中文[简体]
  • English [US]
  • 日本語
  • 한국어
  • Français [France]
  • Bahasa Indonesia
  • Polski
  • Español
  • Português [Brasil]
  • Deutsch
  • Italiano

  • 注册
  • 登录
  • Messenger
  • Facebook Lite
  • Watch
  • 地点
  • 游戏
  • Marketplace
  • Meta Pay
  • Oculus
  • Portal
  • Instagram
  • Bulletin
  • 本地
  • 筹款活动
  • 服务
  • 选民信息中心
  • 小组
  • 关于
  • 创建广告
  • 创建公共主页
  • 开发者
  • 招聘信息
  • 隐私权政策
  • Cookie
  • Ad Choices
  • 条款
  • 帮助中心
  • 联系人上传和非用户
  • 设置
  • 动态记录

Meta © 2022

  1. HowTo
  2. PHP Howtos
  3. Calculate Average of Numbers in PHP

Created: June-27, 2022

  1. Use array_sum[] and count[] for Specific Number Set
  2. Use while Loop for Continuous Number Set

When we code in PHP, we encounter different mathematical operations that we need to do. Addition, subtraction, multiplication, and division are the native ones.

A typical one is average, also called mean. In this article, we consider using PHP built-in functions to calculate the average of a known set of numbers and a continuous set of numbers.

Use array_sum[] and count[] for Specific Number Set

The simple formula of average is the sum of the numbers divided by the frequency [count] of the numbers. Therefore, to find the average of 1, 3, 4, 5, and 6, we will add 1+3+4+5+6, which gives 19 and divide 19 by the frequency of numbers which is 5, and the average will be 3.8.

Code - PHP:


However, this is rather simple when we can easily count the frequency of numbers. Therefore, we need functions such as array_sum[] and count[].

With these two built-in functions, you can easily calculate the sum of numbers within an array because arrays are a better data structure to store sequences of elements such as integers and floats.

Let’s store the same numbers within an array and calculate the average.

Code:


Output:

3.8

Let’s make the a little more advanced where we auto-generate some random numbers.


Output:

49.331606217617

Your output will differ from ours as the numbers within the arrays are randomly generated.

Use while Loop for Continuous Number Set

The previous example covers having a list of known numbers. However, there are cases you might want to calculate the average as you go, like in a PHP application for an education dashboard.

Suppose you want to calculate the average weekly for as long as the student is on that path. We can use the while loop to continuously ask for the numbers and calculate the number at every interval.

In this example, we use the readline[] function and the typical array_sum[] and count[] functions. In addition, we will make use of the interactive PHP shell via the following shell statement.

php main.php

Code - main.php:


The $temp array will hold the numbers that the users will enter. The while loop allows us to continuously ask for a new number forever unless the break condition is met, which is zero [0].

The code below makes the users’ input via the readline[] function and ensures it is a float received.

$a = [float]readline["Enter a number: "];

The following command will receive an integer.

$a = [int]readline["Enter a number: "];

We add the user’s number to the $temp array using the array_push[] function. Afterwards, we calculate the average using the array_sum[] and count[] function like earlier.

When we are done, we can type 0 to end the program, which initiates the break statement. Finally, we print the average of all the users’ inputted numbers.

$frequency = count[$temp];

echo "\nAverage of all the numbers [$frequency] is $average.";

Interactive Shell Output:

> php main.php

Adding numbers repeatedly to get the average at each interval
If you want to terminate the program, type 0

Enter a number: 11

Current average is 1


Enter a number: 3

Current average is 2


Enter a number: 6

Current average is 3.3333333333333


Enter a number: 88

Current average is 4.5


Enter a number: 1010

Current average is 5.6


Enter a number: 0

Average of all the numbers [5] is 5.6.

How do you calculate average in PHP?

Use array_sum[] and count[] for Specific Number Set The simple formula of average is the sum of the numbers divided by the frequency [count] of the numbers.

What is the formula of average of three numbers?

Average This is the arithmetic mean, and is calculated by adding a group of numbers and then dividing by the count of those numbers. For example, the average of 2, 3, 3, 5, 7, and 10 is 30 divided by 6, which is 5.

How do you find the average value in an array in PHP?

You can either do $count = count[$a]; or just do if[$a] .

Chủ Đề