6

I don't seem to be able to assign multiple variables in an "if" statement. The following code:

<?php

function a ($var)
{
    if ($var == 1)
    {
        return 'foo';
    }

    return false;
}

function b ($var)
{
    if ($var == 1)
    {
        return 'bar';
    }

    return false;
}

if ($result1 = a(1) && $result2 = b(1))
{
    echo $result1 . ' ' . $result2;
}

?>

Returns "1 bar" rather than "foo bar". If I remove the second condition/assignment it returns "foo".

Is there a way to assign multiple variables in an "if" statement or are we limited to just one?

1
  • This is a very bad idea. What if you put an 'or' condition between two assignments? IF (e=mc^2 or E=Mc^2) ... Did the second assignment execute? Commented Sep 21, 2017 at 12:37

4 Answers 4

5

This is all about operator precedence

<?php

function a ($var)
{
    if ($var == 1)
    {
        return 'foo';
    }

    return false;
}

function b ($var)
{
    if ($var == 1)
    {
        return 'bar';
    }

    return false;
}

if (($result1 = a(1)) && ($result2 = b(1)))
{
    echo $result1 . ' ' . $result2;
}

?>

https://repl.it/IQcU

UPDATE

assignment operator = is right-asscoiative, that means, evaluation of operand on rhs has precedence over the lhs operand.

thus,

$result1 = a(1) && $result2 = b(1)

is equivalent of,

$result1 = (a(1) && $result2 = b(1))

which evaluates

$result1 = ("foo" && [other valild assignment] )

which will result that,

$result1 becomes true

and echo true/string value of boolean true (strval(true)) outputs/is 1

you can also check that revision, https://repl.it/IQcU/1

to see that below statement

$result1 = a(1) && $result2 = b(1)  

is equivalent of this one.

 $result1 = (a(1) && $result2 = b(1)) 
Sign up to request clarification or add additional context in comments.

2 Comments

correct.+1.But need to add more explanation regarding why 1 bar is coming?
Aha. Can't believe I never came across this before. Normally I'm either assigning just a single value in the if condition or doing multiple comparisons ($a == 'foo'), which apparently do not associate across && and similar structures. I thought && was sort of a condition separator but apparently when doing assignment it is not. Thanks!
4

Need to add parentheses() in each assignment like below:-

if (($result1 = a(1)) && ($result2 = b(1)))
{
    echo $result1 . ' ' . $result2;
}

Output:- https://eval.in/804770

Correct explanation is given by @marmeladze here:-

Why 1 bar is coming through OP's code

Comments

1

The last if statements need some brackets, it should have been:

if (($result1 = a(1)) && ($result2 = b(1)))
{
     echo $result1 . ' ' . $result2;
}

This ensures that things in the bracket are executed first and it will help.

2 Comments

correct.+1.But need to add more explanation regarding why 1 bar is coming?
Things in the bracket are executed first. Without them it would have been: if ($result1 = a(1) && $result2 = b(1)) resulting in non assignment of the second variable.
0

You have to add == to check a condition.

Try this,

if ($result1 == a(1) && $result2 == b(1))
{
    echo $result1 . ' ' . $result2;
}

Checked with belo example

<?php 

function a ($var)
{
    if ($var == 1)
    {
        return 1;
    }

    return false;
}

function b ($var)
{
    if ($var == 1)
    {
        return 2;
    }

    return false;
}

if (1 == a(1) && 2 == b(1))
{
    echo 'success';
}
?>

1 Comment

I don't think it's anyhow solving problem of OP or giving him a clue why 1 bar is coming in his case?

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.