首页 > 解决方案 > 如何从 php 中的不同函数添加两个单独的变量?

问题描述

我是 PHP 的初学者。我正在尝试从两个不同的函数中添加两个变量,但我收到一个错误,因为它没有显示任何东西,比如变量没有从函数中传递数据

如何添加这些单独的变量?

这是我的代码:

function intrestcal()
{
    global $rate , $diff, $principal, $totalint;

    if(isset($_POST['submit']))
    {
        $principal = $_POST['principal'];
        // $intrstRate = $_POST['intrest'] / 100;
        //$tenure = $_POST['tenure'];

        $time = ($diff/30);

        if ($time < 1)
        {
            $newtime = 1;
        }
        $totalint = (($principal * $rate * $newtime)/100);

        //$_SESSION['totalint'] =   $totalint;
        //$mthly = $principal + $totalint;

        echo $totalint;
    }
    elseif (isset($_POST['rst']))
    {
        echo '00.00';
    }
    else 
    {
        echo '00.00';

    }
} 

function installmentcal()
{
    global $totalint , $principal;

    //  $principal = $_POST['principal'];

    $monthint = $principal + $totalint;
    echo $monthint;

}

<h1 class="subtitle is-5" style="margin-top: -15px;">   Monthly Installments</h1>
<h1 class="title is-4"> $<?php installmentcal()?></h1>
<br>
<h1 class="subtitle is-5" style="margin-top: -15px;">   Total Interest</h1>
<h1 class="title is-3"> $<?php intrestcal();?></h1>

我需要将总利息添加到每月分期付款中

标签: phphtml

解决方案


我只是给你一些提示: - 不要使用全局变量 - 不要直接使用来自超全局变量(例如 $_POST 和 $_GET)的数据,永远不要相信用户输入。


推荐阅读