Hello. Please tell me if I do the right thing by checking the parameters like this?

if (!empty($_POST['login']) && !empty($_POST['pass']) && !empty($_POST['pass1']) && !empty($_POST['question']) && !empty($_POST['answer']) && !empty($_POST['mail']) && !empty($_POST['antibot'])) 

Is there a more elegant, correct way?

    2 answers 2

    It is possible, for example:

     $ok = false; foreach (array('login', 'pass', 'pass1', 'question', 'answer', 'mail', 'antibot') as $code) if (!empty($_POST[$code])) $ok = false; // или handleError('reg', $code) или еще что-то... if ($ok) { // all is ok } 

    If this happens often, it’s better to put in a function

     function emptyPost() { foreach (func_get_args() as $code) if (empty($_POST[$code])) return true; return false; } if (!emptyPost('login', 'pass', 'pass1', 'question', 'answer', 'mail', 'antibot')) { // all is ok } 

    In general, I advise you to write validation / validation functions, your own, so that there is no such jumble.

      empty checks if the variable is empty. But at the same time it must exist. Whether there is checked through isset. Further, it is worth separating these checks in order, for example, mail, it is worth checking for the correct format, password for length, login for validity. Toist from your test of practical use zero.

      • I do not want to deploy all the code here. Everything is checked there, it is only a test for the existence of incoming parameters. I am not a teapot ... Plus to everything - you did not answer my question. Is there a better alternative to my verification? - FunnyMan
      • If you have already checked whether a variable exists, then why do you need this emptiness? There are rules to verify login? Well, apply it immediately, and if the validation rules are well-written, it will immediately discard an empty value. The same is about mail. I just do not see the point of checking for emptiness in general. - Arni
      • Is your isset faster empty? - FunnyMan
      • one
        Not that faster, they have different work logic and purpose. You can read more here http://habrahabr.ru/blogs/php/113253/ - DemoS
      • empty, between us, the insidious thing. )) empty (0) === true - Arni