1

I am very new to PHP, now checking how does the function work. I have 3 functions in futurevalue.php , are being called from display_results.php.

I want to get the results using my functions : 1. future_value will return the value of calculation of future value 2. currency_formatting will return 2 values with $ sign: $investment_f, $future_value_f 3. percent_formatting will return value $yearly_rate_f with the percent signs

Currently, no values are returning to display_results.php, I also wonder if my function calling is in the right way.

appreciate your kind response.

futurevalue.php

<?php

namespace murach\futurevalue{

    // Calculate the future value
    if(!function_exists('future_value')){
        function future_value($investment,$years, $yearly_rate) {
            global $future_value;
            $future_value = $investment;;


            if (isset($_POST['compound_monthly'])) {
                // compound monthly
                $compounded_monthly = 'Yes';
                $months = $years * 12;
                $monthly_rate = $yearly_rate / 12;
                for ($i = 1; $i <= $months; $i++) {
                    $future_value = $future_value + ($future_value * $monthly_rate *.01);
                }
            } else {
                // compound yearly
                $compounded_monthly = 'No';
                for ($i = 1; $i <= $years; $i++) {
                    $future_value = $future_value + ($future_value * $yearly_rate *.01);
                }
            }  

            return $future_value;
        }
    }
    //Function for currency formatting
    if(!function_exists('currency_formatting')){
        function currency_formatting($investment, $future_value) {
            if(isset( $investment_f)){
                $investment_f = '$'.number_format($investment, 2);
                //   return $investment_f;


                $future_value_f = '$'.number_format($future_value, 2);
                return array($investment_f, $future_value_f);
            }
        }
    }

    //Function for percent formatting
    if(!function_exists('percent_formatting')){
        function percent_formatting( $yearly_rate) {
            if(isset( $yearly_rate_f)){

                $yearly_rate_f = $yearly_rate.'%';
                return $yearly_rate_f;
            }
        }
    }
}
?>

display_results.php

<?php
require('C:\xampp\htdocs\ex_solutions\ch07_en7-1\futurevalue.php');

// get the data from the form
if(isset($_POST['investment'])) {
    $investment = $_POST['investment'];
}

if(isset($_POST['interest_rate'])) {
    $yearly_rate = $_POST['interest_rate'];
}

if(isset($_POST['years'])) {
    $years = $_POST['years'];
}
if (isset($_POST['compounded_monthly'])){
    $compounded_monthly = $_POST['compound_monthly'];
}


// validate investment entry
if ( empty($investment) ) {
    $error_message = 'Investment is a required field.'; 
} else if ( !is_numeric($investment) )  {
    $error_message = 'Investment must be a valid number.'; 
} else if ( $investment <= 0 ) {
    $error_message = 'Investment must be greater than zero.';        
    // validate interest rate entry
} else if ( empty($yearly_rate) ) {
    $error_message = 'Interest rate is a required field.'; 
} else if ( !is_numeric($yearly_rate) )  {
    $error_message = 'Interest rate must be a valid number.'; 
} else if ( $yearly_rate <= 0 ) {
    $error_message = 'Interest rate must be greater than zero.';        
    // set error message to empty string if no invalid entries
} else {
    $error_message = '';
}

// if an error message exists, go to the index page
if ($error_message != '') {
    include('index.php');
    exit();
}

require_once('C:/xampp/htdocs/ex_solutions/ch07_en7-1/futurevalue.php');
$future_value = murach\futurevalue\future_value($investment, $yearly_rate, $years);


require_once('C:\xampp\htdocs\ex_solutions\ch07_en7-1\futurevalue.php');
$investment_f = murach\futurevalue\currency_formatting($investment, $future_value);

require_once('C:\xampp\htdocs\ex_solutions\ch07_en7-1\futurevalue.php');
$future_value_f = murach\futurevalue\currency_formatting($investment, $future_value);

require_once('C:\xampp\htdocs\ex_solutions\ch07_en7-1\futurevalue.php');
$yearly_rate_f =  murach\futurevalue\percent_formatting( $yearly_rate);

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Future Value Calculator</title>
<link rel="stylesheet" type="text/css" href="main.css"/>
</head>
<body>
<div id="content">
    <h1>Future Value Calculator</h1>

    <label>Investment Amount:</label>
    <span><?php echo $investment_f; ?></span><br />

    <label>Yearly Interest Rate:</label>
    <span><?php echo $yearly_rate_f; ?></span><br />


    <label>Number of Years:</label>
    <span><?php echo $years; ?></span><br />

    <label>Future Value:</label>
    <span><?php echo $future_value_f; ?></span><br />


    <label>Compound Monthly:</label>
   <span><?php  murach\futurevalue\future_value($compounded_monthly);  ?></span><br />


</div>
</body>
</html>
0

1 Answer 1

2

You have multiple logic problems

a) require_once() is repeated multiple times, yet always requiring the same file. This is pointless. require_once() and include_once() will load the specified ONCE and then trying to load the same file again becomes null operation.

b) You have this:

function currency_formatting($investment, $future_value) {
    if(isset( $investment_f)){
               ^^^^^^^^^^^^---- undefined variable

Since $investment_f is undefined, you never reach the number_format() or return calls, so your function simply falls off the bottom and returns nothing. A function which returns nothing will be an implicit null value:

function foo() {
  // do nothing
}

$bar = foo(); // $bar becomes null

c) You are using empty() tests on numeric values. empty(0) happens to be TRUE, so if this was calculation a loan for a 0% APR car loan, you'd be going down invalid code paths.

Sign up to request clarification or add additional context in comments.

2 Comments

Probably isset($investment_f) should be isset($investment).
My opinion, if you are "very new to PHP" and you are namespacing blocks like that, you are in over your head. First, why namespace? That is just going to confuse you. Can you answer me WHAT namespacing is, and how it behaves? Second, while I appreciate your checking if the functions exist, you have direct control over what is executed when and know they do, this just adds room for error. And if you somehow don't have control of what executes when (some complex framework), then god speed and good luck. Because I didn't touch what you are doing with a ten foot poll when I first started...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.