Рассчитать сложные проценты в PHP7
я хочу вычислить сложные проценты из капитала X, но сценарий, который я нашел здесь пример-вычислить проценты , не работает для меня. Это мой код:
HTML:
<!DOCTYPE html>
<html>
<head><meta charset="UTF-8"></head>
<title> Cálculo de intereses</title>
<body>
<center><h3>Introduce el capital, el porcentaje de interés y el tiempo en alos</h3></center>
<form name="Formulario" method="post" action="ejercicio6.php">
<table>
<tr>
<td>
Capital incial:
</td>
</tr>
<tr>
<td><input type="float" name="capital" value="Cantidad X" required></td>
<tr>
<td>
Tiempo (años):
</td>
</tr>
<tr>
<td><input type="number" name="tiempo" value="1" max="100" required></td>
</tr>
<tr>
<td>Porcentaje:</td>
</tr>
<tr>
<td><input type="number" name="porcen" value="5" max="100" required></td>
</tr>
<tr>
<tr>
<td><input type="submit" name="Enviar">
<input type="reset" name="Reestablecer">
</td>
</tr>
</tr>
</table>
</form>
</body>
</html>
PHP:
<?php
//Declaración de variables
$tiempo = $_POST['tiempo'];
$porcen = $_POST['porcen'];
$capital = $_POST['capital'];
$n = 1; //periodos por año, lo dejo a uno pues no se especifica nada más en el ejercicio
//Ejercicio
function interest($capital,$tiempo,$porcen,$n=1){
$acumulado=0;
if ($tiempo > 1){
$acumulado=interest($capital,$tiempo-1,$porcen,$n);
}
$acumulado1 = $capital;
$acumulado = $acumulado1* pow(1 + $porcen/(100 * $n),$n);
return $acumulado;
}
echo "el interés acumulado es : ". interest($capital,$tiempo,$porcen,$n);
?>
1
1 answers
Не "funciona"
Как вы ожидаете поскольку вы не делаете вызов функции, которая выполняет вычисление интереса interest
Кроме того, ваша переменная $acumulado3
нигде не отображается , вы не сможете получить доступ к переменным, которые вы используете внутри función
, потому что они находятся в другой области , одним из вариантов было бы просто изменить окончательную печать echo "el interes acumulado es $acumulado3";
на
echo "el interés acumulado es : ". interest($capital,$tiempo,$porcen,$n);
UPDATE Обратите внимание, что это функция recursiva
, Кроме того, это ошибка редактирования, которая может возникнуть (это нормально), ваша функция останется так.
<?php
//Declaración de variables
$tiempo = $_POST['tiempo'];
$porcen = $_POST['porcen'];
$capital = $_POST['capital'];
$n = 1; //periodos por año, lo dejo a uno pues no se especifica nada más en el ejercicio
function interest($capital,$tiempo,$porcen,$n){
$acumulado=0;
if ($tiempo > 1){
$acumulado=interest($capital,$tiempo-1,$porcen,$n);
}
$acumulado+= $capital;
$acumulado = $acumulado * pow(1 + $porcen/(100 * $n),$n);
return $acumulado;
}
echo "el interes acumulado es".interest($capital,$tiempo,$porcen,$n);
?>
1
Author: Dev. Joel, 2016-10-25 19:12:09