George Lamb
George Lamb

Reputation: 51

PHP input and output of four digit integer

Attempting to create an input field of a 4 digit number or four letter string and store it in variable $x in PHP. Also attempting to create an output that indicates whether $x is odd or even.

Have been able to create the input field and store the string, however I can't seem to get the output to display whether its odd or even for some reason, here is my code so far:

<td>
    <form method="post">
        Enter four digit number/letter string <input type="text" name="name"
            maxlength="4"> <input type="submit" />
    </form>
</td>
<?php
if ($_POST ['name'] && $_POST ['name'] != "") {
    $x = urldecode ( $_POST ['name'] );
} else {
    $x = "not set";
}

echo "$x" . "<br/>";
?>
</td>

<td>
<?php
$x = urldecode ( $_POST ['name'] );
if (is_numeric ( $x )) {
    if ($x % 2 == 0) {
        return "It's even";
    } else {
        return "It's odd";
    }
}
echo "$x";
?>
</td>

Any help would be greatly appreciated. Thanks G

Upvotes: 1

Views: 419

Answers (1)

AbraCadaver
AbraCadaver

Reputation: 79024

return is used to return data from a function. You don't have a function. You seem to already know about echo, and you did it correctly here How to indicate if a number is odd or even in PHP in an HTML file:

echo "It's even";

And:

echo "It's odd";

Upvotes: 1

Related Questions