function check_login($login, $pass) { return ($_POST['login'] == 'root') && ($_POST['pass'] == '123321'); } 

Correctly, I understand that this function returns true / false depending on the username / password match? I don't understand what the abbreviated ifs syntax is ..

    1 answer 1

    Think for yourself. The comparison itself returns true or false by itself, right?

    Let's go further. The first part is true if and only if login was root, right? And the second - if the pass was 123321. That is, everything together will be true if the login root, and pass 123321. And of course false otherwise.

    You see that no if are needed.

    That is, in principle, the function could be rewritten with if 's, in a more familiar way, like this:

     function check_login($login, $pass) { $result = ($_POST['login'] == 'root') && ($_POST['pass'] == '123321'); if ($result == true) { return true; } else { return false; } } 

    Or so:

     function check_login($login, $pass) { $result = ($_POST['login'] == 'root') && ($_POST['pass'] == '123321'); if ($result == true) { return $result; } else { return $result; } } 

    But why?

    • one
      VladD, thanks for the clarification. When it is true, and when it is false, I understood. I was confused by the syntax, is it really acceptable? I saw a shortened version of the conditional operator, but it was a little different from what I remember - devmonkey
    • @sof_ka: Good question. Of course, acceptable! A boolean expression can not only be “fed” in if , it is in principle no different from a number, for example. You do not see problems in the return 3 + 5; code return 3 + 5; ? Boolean expression is no worse. - VladD
    • @sof_ka: on the contrary, for me the code is if (expr) {return true; } else {return false; } or return expr? true: false; it seems redundant: after all, in any case, we return exactly what the value of our expr , so why check it once again? - VladD
    • Yes, you are right :) Thank you - devmonkey
    • @sof_ka: Please! - VladD