PHP lessons

PHP lessons

WILL IT OVERCOME?

carrello (2K)

A little trolley has to be pushed in order to go over a slope. You must find the smallest speed that allows the overcoming.

Slope: m

Speed of the initial push: m/s

.. let's see if it will overcome ....

The simple selection is a crossroads with two possibilities.
The road to go depends on a condition.
ifsemplice (7K)

The program WILL IT OVERCOME? allows to execute alternatively two instructions or two blocks of instructions, depending on the TRUE/FALSE value of a condition, i.e. of a boolean variable or expression.

The input data determine the TRUE or FALSE value of a condition that fixes the answer for the user between two possibilities.

In PHP (as in all the programming languages) the structure of the simple selection is given by the IF instruction

This is the syntax:

if (condition)
{instruction1;}
else
{instruction2;}
;
if the condition is TRUE the instructions between the fist curly brackets are executed, else are executed the ones between the following brackets.

Here it is how the program WILL IT OVERCOME? works:

overcome.htm codeovercome.php code
<form action="overcome.php" method=post>
<p>Slope:
<input type='text' name='h'> m</p>
<p>Speed of the initial push:
<input type='text' name='v0'> m/s</p>
<p>.. let's see if it will overcome ....
<input type=submit value="GO!"></p>
</form>
<?php
$g=9.8; //gravity acceleration
$ok = ($v0*$v0/2>=$g*$h);
if ($ok) {echo "Wow! The trolley is UP!!"; }
else {echo "There is not enough energy!";} ;
?>
The HTML page accepts, as variables, the slope h and the initial speed v0 with the values chosen by the user. The PHP program defines a boolean variable $ok as an inequality built by the input variables. $ok is TRUE if there is enough energy, else $ok is FALSE. The IF instruction chooses the road to go depending on the value of $ok.

Valid HTML 4.01 Transitional