<?php
    
if (isset($_GET['source'])) {
        
highlight_file(__FILE__);
        exit;
    }

    if (!isset(
$_GET['g'])) {
        
?>
            <form>
                Starting speed: <input name=startingspeed type=number value=0> m/s<br>
                How many gs: <input name=g type=number><br>
                Where one g is: <input name=gravityconstant type=number value=9.8 step=0.01> m/s (default: typical Earth gravity)<br>
                <br>
                <strong>Pick either:</strong><br>
                Duration: <input name=t type=number value=0> s (to calculate distance)<br>
                <strong>or:</strong><br>
                Distance: <input name=d type=number value=0> km (to calculate duration)<br>
                <strong>(If you fill in both fields, it will calculate the distance.)</strong><br>
                <br>
                <input type=submit value=Calculate>
            </form>
            Or, <a href="?source">view the source code of this page.</a>
        <?php
        
exit;
    }

    
set_time_limit(3);

    
$info " <a href='?clear'>Do a new calculation</a> or <a href='?source'>view source code</a>.";

    
$g floatval($_GET['g']);
    
$gravityconstant floatval($_GET['gravityconstant']);

    
$distancetraveled 0;
    
$speed 0;
    
$seconds 0;

    if (
ceil($_GET['t']) == 0) {
        
$distancelimit floatval($_GET['d']) * 1000// *1000 because we asked it in km but our gravity is in m/s
        
while ($distancetraveled $distancelimit) {
            
$speed += $gravityconstant $g;
            
$distancetraveled += $speed;
            
$seconds++;
        }
        
$speed /= 1000;
        
$distancetraveled /= 1000;
        
$distancelimit /= 1000;
        echo 
"After ${seconds}s, you reached a speed of ${speed}km/s and traveled ${distancetraveled}km. As of this second, you exceeded your limit of ${distancelimit}km.$info";
        exit;
    }

    
$duration floatval($_GET['t']);
    for (
$i 0$i $duration$i++) {
        
$speed += $gravityconstant $g;
        
$distancetraveled += $speed;
    }
    
$speed /= 1000;
    
$distancetraveled /= 1000;
    echo 
"After ${duration}s, you reached a speed of ${speed}km/s and traveled ${distancetraveled}km.$info";