1

I am trying to set a if statement(s) for a self-processing form that checks if the user entered a positive whole number for quantity. This would be the following conditions:

is_numeric

is_int

and > 0

I've read that I can use || and && to set multiple conditions in a single if function, however it's not working because when I typed "-1" or "jkgh" as quantity, it still printed the table row.

If statement:

    if (is_numeric($_POST['qty_entered'][0]) || is_int($_POST['qty_entered'][0]) || $_POST['qty_entered'][0] > 0) {
    print "'<tr><td>' test '</td>'";

Thanks!

1
  • 1
    || stands for OR, && stands for AND. Once you know that, just try to phrase your conditions and replace with appropriate symbols Commented Oct 8, 2017 at 21:20

1 Answer 1

3

Your code says if the input is a number, or is an integer, or is just greater than zero, then it's alright, not if all of those conditions are true.

You probably mean && to force all of those to match instead of any one of them with ||.

When you define this sort of test you'll want to formalize it in a function so that the intent of your code is clear:

function is_sufficiently_numeric($v) {
  return is_numeric($v) && is_int($v) && $v > 0;
}

Then your code becomes more self-explanatory, and as a bonus, less verbose.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, that worked! I have another issue, I want to display a second table after the submit button is pressed, however, there also must be no errors in the quantity entered. Currently I have: if (array_key_exists ('submit_button', $_POST)) && $errors = false{ print "Invoice <br> <table style='border-collapse: collapse; text-align: center' However, it gives me an error when I add in the && $errors = false
Hold on there. Stack Overflow is all about addressing one question at a time. That sounds like a well-defined problem, so instead of cramming it in a comment here, hopen up a new question. Having questions focused on a singular problem helps keep them narrow enough in scope they might help other people with the same problem in the future.
Okay, I will. Thank you so much for the help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.