Ok I'm a beginner to Php and I have been given an assignment to do. I have completed it but I can't figure out how to get it to validate my data. I have put in a code that I thought would work and for some reason it doesn't. My assignment is this: "Write a code to convert the height entered by the user in foot and inches to centimeters. Handle the exceptions when negative values or decimal, non-numeric values are given as input.

So I created two php pages. One for the user to input their data, and the other to transfer the data and show the results. I'm stumped on validating the data start for hours now and it's about to drive me crazy. Here is what I got for my code. The first one is for the user and the other for transfer:

<html>
<head>
</head>
<body>
<form action="me3.php" method="get">
Please enter your height:<p>
Feet:<br>
<input type="text" name="feet"><p>
Inches:<br>
<input type="text" name="inches"><p>

<input type="submit" name="submit" value="Submit">
</form>

</body>
</html>

Here is my other:

<html>
<body>

<?php
$feet = $GET['feet'];
$inches = $
GET['inches'];
$centi;
$meters;

if (is_float($feet) && is_float($inches)) {
echo "You must enter a number bigger than 0. Please try again.";
}
else {

// Calculates extra inches to feet if nessessary.
if ($inches > 12)
{
$inches = $inches%12;
$feet2 = $inches/12;
$feet = $feet + $feet2;
}

// Calculates feet to meters.
$meters = $feet 0.3048;
$centi = $inches
2.54;

echo "Your height is: " . $meters . " and " . $centi;

}
?>

</body>
</html>

Note here is the code I was trying to use to validate my data:

if (is_float($feet) && is_float($inches)) {
echo "You must enter a number bigger than 0. Please try again.";
}
else
// Put the rest of the code here because it passed the if statement{

    a. All values received from the form will be strings, so the is_float() tests will always be false. You can, however, use is_numeric() to determine if they are numeric strings.
    b. Regardless, checking to see if they are floats does not have anything to do with them being > 0, since 0 is a float, as well as -1.0 or -99999.

    For the case of wanting to make sure the height is > 0, one might do something like:

    if(
        !is_numeric($feet)   or
        !is_numeric($inches) or
        $feet < 0            or
        $inches < 0          or
        $feet + $inches == 0
    ) {
        // not a valid height
    } else {
        // valid numbers representing a > 0 height
    }
    
      $meters = $feet * 0.3048;
      $centi = $inches * 2.54;

      Later you might also want to look at how metres and centimetres work together. (Incidentally, metres are almost never used to describe how tall a person is: I'm 180cm, and not described as 1m 80cm (nor 1.524m 27.6cm)

        Weedpacket wrote:

        Incidentally, metres are almost never used to describe how tall a person is: I'm 180cm, and not described as 1m 80cm (nor 1.524m 27.6cm

        That is not true in my experience: rarely do I hear the "1m 80cm tall" version, but "1.8m tall" seems fairly common to me. If it is not so for you, then this is probably just convention differences based on locality.

          Oh, I thought I'd given that disclaimer. Would you say "one point eight three metres" for someone 183cm tall?

            Write a Reply...