I tried the following to make sure the input value is between 0 and 100 but it didn't work.
if (($_POST['rate']) >= '0' || =<'100') {
$rate = $_POST['rate']
}
esle {
echo '<p>Please enter a value between 0 and 100</p>';
}
Issue 1: string vs int
Currently, you are comparing strings, not numbers. Cast your $_POST variable to int and remove the apostrophes around 0 and 100 to fix this:
$rate = (int) $_POST['rate'];
if ($rate >= 0 || =< 100) { /* ... */ }
Issue 2: something is missing
However, this is still not going to yield the desired results as you are missing $rate for the second comparison. Change it to:
$rate = (int) $_POST['rate'];
if ($rate >= 0 || $rate =< 100) { /* ... */ }
Issue 3: or vs and
And we're still not there. Currently, you are using || (or). You need to use && (and) instead:
$rate = (int) $_POST['rate'];
if ($rate >= 0 && $rate =< 100) { /* ... */ }
Issue 4: =< vs <=
Still not going to work. One of your comparison operators is the wrong way round. =< should be <= instead:
$rate = (int) $_POST['rate'];
if ($rate >= 0 && $rate <= 100) { /* ... */ }
Issue 5: user input
$_POST holds data coming from a user. Never trust user data. What, for example, if there is no rate element? In that case, PHP will throw an error of type E_NOTICE (which you might not even see if you don't have your reporting set to E_ALL) and once converted to int, evaluate to 0! Hence, your test would pass, which is probably not what you want. To prevent that, use isset():
$rate = isset($_POST['rate']) ? (int) $_POST['rate'] : -1;
if ($rate >= 0 && $rate <= 100) { /* ... */ }
Issue 6: something esle
As pointed out by Pedro Lobito, you managed to also introduce a typo: esle should be else.
Issue 7: semicolon
As pointed out by Gert, your line $rate = $_POST['rate'] is missing the ; at the end.
Summary
I don't mean to be rude, but seeing how many mistakes you managed to squeeze into this simple if, I get the feeling that you should probably consult some basic PHP tutorials instead of Stack Overflow at this point.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With