0

some_function($input) returns the integer 23

$other_int has a value that is not equal to 23

Code snippet 1

if ($int = some_function($input) && $int != $other_int) {...}

Code snippet 2

$int = some_function($input);
if ($int != $other_int) {...}

I coded snippet 1 thinking that the if statement would return true given the above conditions. I was wrong.

The if statement in snippet 2 is true.

Why?

2
  • 2
    you can't put an assignment inside the if clause Commented Oct 15, 2015 at 0:20
  • 1
    @Dagon Yes you can. That's the whole point of this question. Why does this code yield an expected result when assignment is outside of the clause rather than inside. Commented Oct 15, 2015 at 0:37

5 Answers 5

4

So, assignments in an if clause like that is a bad practice for a reason. That said, you're not assigning what you think you are. This is what you're actually doing:

$int = (some_function($input) && $int != $other_int);

which evaluates to true. To get the result you want, change the code to

if (($int = some_function($input)) && ($int != $other_int)) {...}

But really, don't assign variables in an if() statement.

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

Comments

3

As others have mentioned, && has higher precedence than assignment, so you're performing the logical operation before you assign to the variable.

Instead of && you can use and. and and or have lower precedence than assignment, and are intended precisely for situations like this.

if ($int = some_function($input) and $int != $other_int)

Comments

1

The && and the != have higher precedence than the assignment operator =.

See http://www.php.net/manual/en/language.operators.precedence.php

So inside the conditional:

if ($int = some_function($input) && $int != $other_int) {...}

It executes the != then the &&, but $int hasn't been initialized yet (assuming you haven't initialized it outside that conditional) so the right side != always returns false.

Comments

0

You can't assign a value like that inside the if statement AND compare that value in the same statement

https://3v4l.org/FlE6u

<?php

function somefunc() {
    return 23;
}

if($int = somefunc() && $int == 23) {
    echo 'Equal';
} else {
    echo 'Not Equal';
}
echo "\n";
var_dump($int);

You'll note the error

Notice: Undefined variable: int in /in/GHVKs on line 7

And at the end $int is false. Assign it outside your if

1 Comment

Although I don't personally like the approach, you can assign and compare in the if statement, the OP is just missing the parenthesis for order of operations, no?. https://3v4l.org/IPDQK
0

Interestingly, a variable cannot be both initialized and used within the condition of an if statement. You may have found a bug/feature of php.

function some_function()
{
    return 23;
}

$intA = 0;
if ($intA = some_function()
    && $intA != $other_int
) {
    echo 'hello world!'; // works fine
}

if ($intB = some_function()
    && $intB != $other_int
) {
    echo 'hello world!'; // PHP Notice:  Undefined variable: intB
}

Comments

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.