PHP Program to Find Simple Interest
Last Updated :
27 Feb, 2024
Simple interest is the method to calculate the interest where we only take the principal amount each time without changing it for the interest earned in the previous cycle.
The formula to calculate Simple Interest is -
SI = P * R * T / 100
Where -
- I is the interest.
- P is the principal amount.
- R is the rate of interest per annum.
- T is the time in years.
In this article, we will explore how to calculate simple interest in PHP using different approaches.
Find Simple Interest using Inline Calculation
If you only need to perform the conversion once or in a specific context, you can do the calculation inline without defining a separate function.
Example: Illustration of program to find Simple Interest in PHP using inline calculations.
PHP
<?php
// Given Values
$principal = 1000;
$rate = 9;
$time = 5;
// Calculating the SI
$interest = ($principal * $rate * $time) / 100;
echo "Simple Interest: $interest";
?>
OutputSimple Interest: 450
Time Complexity: O(1)
Auxiliary Space: O(1)
Find Simple Interest using a Function
Defining a function to calculate simple interest is a reusable and modular way to perform the calculation. The simpleInterest( )
function takes the principal, rate, and time as arguments and returns the simple interest.
Example: Illustration of program to find Simple Interest in PHP using a function.
PHP
<?php
// Function to calculate SI
function simpleInterest($principal, $rate, $time) {
$interest = ($principal * $rate * $time) / 100;
return $interest;
}
$principal = 1000;
$rate = 9;
$time = 5;
$interest = simpleInterest($principal, $rate, $time);
echo "Simple Interest: $interest";
?>
OutputSimple Interest: 450
Time Complexity: O(1)
Auxiliary Space: O(1)
In a more interactive scenario, you might want to calculate simple interest based on user input. This can be done using an HTML form to get the principal, rate and time as input and PHP to process the input.
Example: Illustration of program to find Simple Interest in PHP through User Input using HTML.
HTML
<!DOCTYPE html>
<html>
<head>
<title>Simple Interest Calculator</title>
</head>
<body>
<form action="" method="post">
<table>
<tr>
<td><label for="principal">Principal:</label></td>
<td><input type="text" id="principal" name="principal"></td>
</tr>
<tr>
<td><label for="rate">Rate (%):</label></td>
<td><input type="text" id="rate" name="rate"></td>
</tr>
<tr>
<td><label for="time">Time (years):</label></td>
<td><input type="text" id="time" name="time"></td>
</tr>
<tr>
<td><input type="submit" value="Calculate"></td>
</tr>
</table>
</form>
<?php
if (isset($_POST['principal'])
&& isset($_POST['rate'])
&& isset($_POST['time'])) {
$principal = $_POST['principal'];
$rate = $_POST['rate'];
$time = $_POST['time'];
$interest = ($principal * $rate * $time) / 100;
echo "<p>Simple Interest: $interest</p>";
}
?>
</body>
</html>
Output

Time Complexity: O(1)
Auxiliary Space: O(1)
Similar Reads
R Program to Calculate Simple Interest Basically, Simple Interest is a quick method to calculate the interest charge of a loan. It is determined by multiplying the principal amount by the daily interest rate by the number of days which elapse between the payments. It gets estimated day to day-with the help of mathematical terms.we will c
2 min read
PHP program to find the length of the last word in string We are given a string. We are required to write a program in PHP to find the length of the last word in the string using inbuilt functions. We have already discussed approach of solving this problem here. This article discusses the PHP solution to the problem. Examples: Input : "php exercises" Outpu
2 min read
Output of PHP programs | Set 3 Predict the output of below PHP programs: Question 1 PHP <?php $number = array(0, 1, one, two, three, 5); $num = preg_grep("/[0-5]/", $number); print_r($num); ?> Options: Array([0]=>0 [1]=>1 [2]=>one [3]=>two [4]=>three [5]=>5) Array([2]=>one [3]=>two [4]=>
3 min read
Simple Interest Simple Interest (SI) is a method of calculating the interest charged or earned on a principal amount over a fixed period. It is calculated based solely on the principal amount, which remains unchanged throughout the calculation.Simple Interest is widely used across industries such as banking, financ
9 min read
PHP Program to Find the Number Occurring Odd Number of Times Write a PHP program for a given array of positive integers. All numbers occur an even number of times except one number which occurs an odd number of times. Find the number in O(n) time & constant space. Examples : Input: arr = {1, 2, 3, 2, 3, 1, 3}Output : 3 Input: arr = {5, 7, 2, 7, 5, 2, 5}Ou
3 min read