I basically store the history in a hidden input.
The idea is to send to your php script the updated value of the historical field in a post variable. Once it arrives in your script it is concatenated with the string representing the current and echoed operation.
Well, initially it is empty but after the first run it starts to accumulate the history contained in the input:
$historico = $_POST['historico'] . '</br>' . $valor1 . $operador . $valor2 . '=' . $resultado ;
So the value inside the input is being updated:
<input hidden="" type="text" name="historico" value="<?php if (isset($historico)) { echo $historico; } ?>" >
I've just added this block inside your if:
if(isset($valor2) && isset($valor1) && isset($operador)){
$historico = $_POST['historico'] . '</br>' . $valor1 . $operador . $valor2 . '=' . $resultado ;
echo $historico;
echo "</br>";
}
It checks if the operations variables are set and adds the variable $histórico
to the value of the hidden input:
<input hidden="" type="text" name="historico" value="<?php if (isset($historico)) { echo $historico; } ?>" >
This field has the function of sending to your php script the history to be concatenated with '</br>' . $valor1 . $operador . $valor2 . '=' . $resultado ;
Note that I use isset so there are no empty variable alerts when they are not set.
Full Code
<?php
if ( isset($_POST['calcularbtn']) ) {
$valor1 = $_POST['valor1'];
$valor2 = $_POST['valor2'];
$tipo = $_POST['tipo'];
if ($tipo == 'Somar') {
$operador = '+';
$resultado = $valor1 + $valor2;
}elseif ($tipo == 'Subtrair') {
$operador = '-';
$resultado = $valor1 - $valor2;
}elseif ($tipo == 'Multiplicar') {
$operador = '*';
$resultado = $valor1 * $valor2;
}elseif ($tipo == 'Dividir') {
$operador = '/';
$resultado = $valor1 / $valor2;
}elseif ($tipo == 'Potência') {
$resultado = pow($valor1, $valor2);
}else{
$resultado = pow($valor1, 1/2);
}
echo 'Resultado=' . $resultado;
}else{
echo "Esperando cálculo...";
}
if(isset($valor2) && isset($valor1) && isset($operador)){
$historico = $_POST['historico'] . '</br>' . $valor1 . $operador . $valor2 . '=' . $resultado ;
}
?>
<form class="form-signin" method="post" action="#">
<div class="form-label-group">
<input hidden="" type="text" name="historico" value="<?php if (isset($historico)) { echo $historico; } ?>" >
<input class="form-control" type="number" name="valor1" size="5" placeholder="Valor 1"><br>
<select class="custom-select d-block w-100" name="tipo" >
<option selected="selected">Somar</option>
<option>Subtrair</option>
<option>Multiplicar</option>
<option>Dividir</option>
<option>Potência</option>
<option>Raiz Quadrada</option>
</select><br><br>
</div>
<div class="form-label-group">
<input class="form-control" type="number" name="valor2" size="5" placeholder="Valor 2"><br>
</div>
<input class="btn btn-outline-secondary" type="submit" name="calcularbtn" value="Calcular">
</form>'
<?php
if(isset($historico)){
echo $historico;
echo "</br>";
}
?>
Conclusion : There are a billion ways to do this. But there are more appropriate ways. This is not the most appropriate way to build a calculator using php as well as building this using php is not appropriate.
However for teaching purposes the use of hidden fields are quite useful for transferring data from the client to the server without the user having control over them.
Tip:
Use javascript
with it you can leave the process dynamic and use more resources. Think: why ask the server for something that your browser already knows.
Ask the browser about javascript and it will know very well what to do with your data. In addition, of course, not having to reload the same page to get the result.