FabricioG
FabricioG

Reputation: 3310

if statement has no comparison operator only an equal, what does it mean?

I have some code from a previous programmer and I'm a little confused to what it means. The if statement is:

    if( !$this->report = $this->get_report()){
... do something }

I'm used to seeing the if statement with some "truthy" condition or a comparison operator.

Is this saying if $this->report is false or doesn't exist then make it = $this_.get_report()?

Upvotes: 0

Views: 122

Answers (1)

Petr Hejda
Petr Hejda

Reputation: 43481

if( !$this->report = $this->get_report())

This line is doing two things:

  1. assigning the value of $this->get_report() to $this->report
  2. validating whether the value loosely compares to false

Only if it does compare (loosely to false), it runs the block do something


Example of another loose comparison:

<?php
if(!$foo = null) {
    // going to process this block
}
  1. $foo is assigned a null value
  2. null loosely compares to false in PHP
    1. null == false loose comparison
    2. null !== false strict comparison
  3. !false results in true, so the block gets executed

Upvotes: 1

Related Questions