php - 如何从 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>
我需要将总利息添加到每月分期付款中
解决方案
我只是给你一些提示: - 不要使用全局变量 - 不要直接使用来自超全局变量(例如 $_POST 和 $_GET)的数据,永远不要相信用户输入。
推荐阅读
- php - 如何在循环外调用数据?
- python - 用`pandas`解析csv文件时`with open(...)`有什么功能?
- sql - 无法识别语句中“DELETE”“FROM”“CTE”附近的输入
- python - 旋转由 numpy 数组处理的图像
- php - phpinfo() 显示与 php.ini 不同的值
- c# - 如何解决 masstransit.rabbitmq(5.3.2)、rabbitmq.client(5.1.0) 的大量发布消息的 541 问题
- javascript - 如何获取json数组?
- c# - Entity Framework Core - 有效地在数据库中存储/查询多语言记录
- docker - 使用 NGINX 的 SSL 代理通过 Docker 导致网关错误
- php - 在未知多维数组中查找并添加到关联数组