2

How do I delcare a variable in an if statment that only goes in the if block if the variable is not a nullish value?

Also, what is the scope of the $var in code like:

if(($var = funky()) != null) {}

Can I reference $var outside the if block?

9
  • 1
    if($var = funky()) - I take it that that is pseudo code? If not, then what you're doing is assigning = rather than comparing ==, unless that's what you want to do. Commented May 3, 2017 at 16:33
  • "Will the code inside the if block still execute or do I need a conditional as well?" - Yes, because it will always be considered as TRUE, since it's an assignment. Your edit: "such as if(!empty($var = funky()))" - only if it's not empty. Commented May 3, 2017 at 16:34
  • 1
    it won't always be true, it'll assign the return value of the function, and that'll be evaluated Commented May 3, 2017 at 16:36
  • 1
    ...which is what I said, just explained differently, for the latter part of the above comment. Commented May 3, 2017 at 16:36
  • 1
    @Fred-ii- i was referencing this: "Will the code inside the if block still execute or do I need a conditional as well?" - Yes, because it will always be considered as TRUE, since it's an assignment. < it won't if the function doesn't return truthy Commented May 3, 2017 at 16:49

1 Answer 1

2

An assignment expression in PHP returns the assigned value. From the documentation:

The value of an assignment expression is the value assigned.

So if whatever funky() returns evaluates to something loosely equal to null, $var = funky() will evaluate to false, so the if block will not be executed.

Doing the assignment in the if condition does not affect the scope of the assigned variable, though. $var will be available in the current scope (inside and outside the if block) after the assignment statement.

For example:

function funky() {
    return false;
}

if(($var = funky()) != null) {
    echo 'something';
}

var_dump($var);

Here, you'll just see boolean false


The only way I can think of to ensure that a variable is only available inside an if block is to assign it there and unset it before the end of the block.

if (funky() != null) {  // evaluate funky() without assigning
    $var = funky();     // call funky() again to assign
    // do stuff
    unset($var);
}

But I can't really think of any good reason to do this.

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

3 Comments

Wow. I am surprised $var is available outside the if block
@KolobCanyon why? You haven't altered the scope at all (an if statement isn't a closure). You've basically just assigned a variable, and then wrote an if statement, except you've done it all in the same line
@billyonecan Gotcha. Did not know that. I always had tried to assign the variable and then do the if statement on the next line, but I saw some code that one lined it... which lead me to this question

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.