【问题标题】:PHP function is not returning any valuesPHP函数没有返回任何值
【发布时间】:2014-07-05 09:17:40
【问题描述】:

我对 PHP 非常陌生,现在检查该函数是如何工作的。 我在 futurevalue.php 中有 3 个函数,正在从 display_results.php 调用。

我想使用我的函数获得结果: 1.future_value 将返回计算未来值的值 2. currency_formatting 将返回 2 个带有 $ 符号的值:$investment_f, $future_value_f 3. percent_formatting 将返回带有百分号的值 $yearly_rate_f

目前,没有返回值到 display_results.php,我也想知道我的函数调用是否正确。

感谢您的友好回应。

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>

【问题讨论】:

    标签: php html mysql post


    【解决方案1】:

    你有多个逻辑问题

    a) require_once() 重复多次,但始终需要同一个文件。这是没有意义的。 require_once()include_once() 将加载指定的ONCE,然后再次尝试加载相同的文件成为空操作。

    b) 你有这个:

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

    由于 $investment_f 未定义,您永远无法到达 number_format()return 调用,因此您的函数只是从底部跌落并且什么也不返回。什么都不返回的函数将是一个隐含的null 值:

    function foo() {
      // do nothing
    }
    
    $bar = foo(); // $bar becomes null
    

    c) 您正在对数值使用empty() 测试。 empty(0) 恰好是 TRUE,所以如果这是计算 0% APR 汽车贷款的贷款,那么您将使用无效的代码路径。

    【讨论】:

    • 可能isset($investment_f) 应该是isset($investment)
    • 我的意见是,如果您是“PHP 新手”并且您正在使用这样的命名空间块,那么您就太过头了。首先,为什么要命名空间?那只会让你感到困惑。你能回答我什么是命名空间,它的行为方式是什么?其次,虽然我感谢您检查功能是否存在,但您可以直接控制何时执行并知道它们执行的操作,这只会增加出错的空间。如果你无法控制什么时候执行(一些复杂的框架),那么上帝的速度和好运。因为当我第一次开始时,我没有触及你在十英尺的民意调查中所做的事情......
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多