Added some code for browser usage
This commit is contained in:
parent
45bfa311a1
commit
8c989afa29
14
README.md
14
README.md
@ -4,6 +4,9 @@ It's nothing fancy at all, but maybe it will be useful to someone. I've made it
|
||||
while learning PHP.
|
||||
|
||||
## Usage ##
|
||||
The script can be run both from a browser and from the terminal.
|
||||
|
||||
### CLI usage ###
|
||||
First argument to the script is the amount of money on your savings account, the
|
||||
second argument is the interest (without the percent sign) and the third
|
||||
(optional) argument is the number of years. For example if you have 50.000 in
|
||||
@ -12,13 +15,16 @@ money you will have after 8 years:
|
||||
|
||||
php interest.php 50000 3 8
|
||||
|
||||
Or if you've made the script executable you can run it as follow:
|
||||
|
||||
./interest 50000 3 8
|
||||
|
||||
If you leave out the number of years the script will calulate the savings after
|
||||
one year.
|
||||
|
||||
### Browser usage ###
|
||||
Just put the script on a webserver with PHP and run access it from your browser.
|
||||
Then fill in the form and hit 'Calculate' and the script will calculate your
|
||||
savings for you.
|
||||
|
||||
Please note that the script does NOT do any form of data validation!
|
||||
|
||||
## Copying ##
|
||||
Copyright 2014 Jack-Benny Persson (jack-benny@cyberinfo.se)
|
||||
|
||||
|
78
interest.php
78
interest.php
@ -1,4 +1,3 @@
|
||||
#!/usr/bin/php
|
||||
<?php
|
||||
/* A simple function to calculate 'interest on interest' together with
|
||||
a simple usage of the function.
|
||||
@ -11,40 +10,69 @@ function InteOnInte($amount, $interest, $year=1)
|
||||
return $totalAmount;
|
||||
}
|
||||
|
||||
function Usage()
|
||||
function CLI_Usage()
|
||||
{
|
||||
print "Usage: php interest.php {amount to start with} {interest} [years]\n";
|
||||
print "Example: php interest.php 50000 3 10\n\n";
|
||||
print "Copyright 2014 - Jack-Benny Persson <jack-benny@cyberinfo.se>\n";
|
||||
}
|
||||
|
||||
// Sanity check
|
||||
if (!isset($argv[1]) || !isset($argv[2]))
|
||||
// If we are running from the terminal
|
||||
if (!array_key_exists("HTTP_USER_AGENT", $_SERVER))
|
||||
{
|
||||
print "Please enter amount and interest!\n";
|
||||
Usage();
|
||||
exit(1);
|
||||
// Sanity check
|
||||
if (!isset($argv[1]) || !isset($argv[2]))
|
||||
{
|
||||
print "Please enter amount and interest!\n";
|
||||
CLI_Usage();
|
||||
exit(1);
|
||||
}
|
||||
|
||||
// Declare some variables
|
||||
$money = $argv[1];
|
||||
$inter = $argv[2];
|
||||
|
||||
// If the user entered years
|
||||
if (isset($argv[3]))
|
||||
{
|
||||
$years = $argv[3];
|
||||
$mySavings = InteOnInte($money, $inter, $years);
|
||||
printf ("After %d years with %.2f%% interest on %.2f you will have " .
|
||||
"%.2f\n", $years, $inter, $money, $mySavings);
|
||||
}
|
||||
|
||||
// If the user did not enter years
|
||||
else
|
||||
{
|
||||
$mySavings = InteOnInte($money, $inter);
|
||||
printf ("After 1 year with %.2f%% interest on %.2f you will have " .
|
||||
"%.2f\n", $inter, $money, $mySavings);
|
||||
}
|
||||
}
|
||||
|
||||
// Declare some variables
|
||||
$money = $argv[1];
|
||||
$inter = $argv[2];
|
||||
|
||||
// If the user entered years
|
||||
if (isset($argv[3]))
|
||||
{
|
||||
$years = $argv[3];
|
||||
$mySavings = InteOnInte($money, $inter, $years);
|
||||
printf ("After %d years with %.2f%% interest on %.2f you will have %.2f\n",
|
||||
$years, $inter, $money, $mySavings);
|
||||
}
|
||||
|
||||
// If the user did not enter years
|
||||
// If we are running from a browser
|
||||
else
|
||||
{
|
||||
$mySavings = InteOnInte($money, $inter);
|
||||
printf ("After 1 year with %.2f%% interest on %.2f you will have %.2f\n",
|
||||
$inter, $money, $mySavings);
|
||||
print ("<!DOCTYPE html>\n" .
|
||||
"<html>\n<head><title>Intereset On Interest</title></head>\n<body>" .
|
||||
"<h1>Interest On Interest calculator</h1>\n" .
|
||||
"<form method=\"GET\" action=\"$_SERVER[PHP_SELF]\">\n" .
|
||||
"Money: <input type=\"text\" name=\"money\"><br />\n" .
|
||||
"Interest: <input type=\"text\" name=\"interest\"><br />\n" .
|
||||
"Years:<input type=\"text\" name=\"years\"><br />\n" .
|
||||
"<input type=\"submit\" value=\"Calculate\">\n" .
|
||||
"</form>\n<br />\n");
|
||||
|
||||
if (array_key_exists("money", $_GET) && array_key_exists("interest", $_GET)
|
||||
&& array_key_exists("years", $_GET))
|
||||
{
|
||||
print ("After $_GET[years] years with $_GET[interest]% interest on " .
|
||||
"$_GET[money] you will have ");
|
||||
$savings = InteOnInte($_GET['money'], $_GET['interest'], $_GET['years']);
|
||||
printf ("%.2f\n", $savings);
|
||||
}
|
||||
|
||||
print "</body>\n</html>\n";
|
||||
|
||||
}
|
||||
|
||||
?>
|
||||
|
Loading…
x
Reference in New Issue
Block a user