Pregunta

I actually found out how to solve this particular problem on my own, but it's still driving me crazy wondering why the problem came about to begin with. I had a conditional statement:

if($_SESSION['authenticated'] = 1) {
    DOSTUFF;
}

Now prior to this if statement I know that $_SESSION['authenticated'] is empty by using print_r(). However, after executing this code block this conditional statement assigns 1 to $_SESSION['authenticated'], which makes the if statement evaluate to true no matter what! I found a way around this using isset(), but I still have no clue why a conditional statement would assign a value to a variable in the first place when it should only evaluate whether or not the condition is true or false.

¿Fue útil?

Solución

Because = is assignment. You want == or === which test for equality. === checks that the operands are both equal and of the same type. == only checks for equality.

Otros consejos

You have a semantic (or syntactic) (or typing) error. You should use double equal sign for equality comparison like this:

if($_SESSION['authenticated'] == 1) {
    DOSTUFF;
}

If you use single equality sing, that means assignment, and the assigned value gets evaluated in the if statement.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top