0

I saw this line of code and I was asking myself the reason of this syntaxe

if(false && isset($_SESSION['new_user']) && $_SESSION['new_user'] == true)

Basically the context is if we have a new user we treat him differently than existing users.

My first guess is that is made to avoid search values in a null variable. So first he tests if there is a variable, and if there is he checks its value. Is that what he does?

But everything inside the same if statement.

My question is, if the first part of the if statement is false, the second part is not tested? Is this a good practice? or would be better to do like this:

if(($_SESSION['new_user']){
    if ($_SESSION['new_user'] == true){
        [...]
    }
}
Matheus Oliveira
  • 587
  • 3
  • 10
  • 33
  • 1
    `if(false && ... &&)` always evaluates to `FALSE`. This is a quick way to temporarily disable a block of code (it forces the execution to take the `else` branch, if it exists). – axiac Dec 02 '16 at 15:25
  • That if will always evaluate to false because of the first element in the if. `if (false && ...)` – Dragos Dec 02 '16 at 15:25
  • 1
    Unrelated to the heart of the question, but FWIW, you're accessing an _array_ in your code, not an object. – Patrick Q Dec 02 '16 at 15:28

3 Answers3

3

Your interpretation is correct. When using &&(AND), if the first condition is not met, no other conditions will be checked.

So it doesn't matter how you format your if, as long as you understand how the code works.

However, in your first example the false will make sure the condition is never true. In this case it is pretty useless.

Jerodev
  • 32,252
  • 11
  • 87
  • 108
1

If the first part of an AND Expression in an IF-statement is false, there is no need to test the rest. So this is correct and is used in that way.

That IF-statement will always return false because of

if (false && ...)

So the if-block is never run. This could be done to simply exclude code from running without deleting it (because someone might need it a little bit later). This is not good programming practice.

If you want to check if a value exists and is not empty (in php false == empty) you can do the following to make the query simpler (leaving out the first condition false):

if (!empty($_SESSION['new_user'])) {

empty() implicitly checks, if a key exists. That means you can get rid of isset(). But this only works if you check for an emtpy/not empty value. If you want to check for a specific id, than you have to use

if (isset($_SESSION['new_user']) && $_SESSION['new_user'] == 'myNewUser') {
Seb
  • 1,521
  • 1
  • 12
  • 19
0

Your surmise is correct, but that code has a feature -- and a catch.

if(false && isset($_SESSION['new_user']) && $_SESSION['new_user'] == true)

The feature is that the initial false immediately aborts the comparison, so that the following items are not evaluated (this you must keep in mind if you ever place there some code with side effects, such as a function call initializing some value).

The catch is that you should never use isset to verify whether a key exists. In this case it works, but in general, existing values that evaluate to NULL will make isset return false.

Finally, if the variable is typed, just to be sure, use the strong comparison operator ===:

if (false && array_key_exists('new_user', $_SESSION) && $_SESSION['new_user'] === true)

Personally I would have written it

if (false && array_key_exists('new_user', $_SESSION) && (true === $_SESSION['new_user']))

since I sometimes mistype a = and end up assigning what I want to compare. If you're not as fat-fingered as I am, you may spare yourself this paranoid frill :-)

Community
  • 1
  • 1
LSerni
  • 55,617
  • 10
  • 65
  • 107