HarryFlopper
HarryFlopper

Reputation: 205

A simple yii2 function doesn't work

I have two functions, which has the same purpose, but I wrote it differently. The first one works fine, but the second one - doesn't work and I can't see the difference between them. Could someone explain me why the second one doesn't work properly?

  1. public function getClientPhone()
    {
    if (is_null($this->client_id)) {
        return '';
    }
    
    $phone = Client::getStaticClientPhone($this->client_id);
    
    if (is_null($phone)) {
        return '' ;
    }
    
    return $phone;
    }
    
  2. public function getClientPhone()
    {
    $phone = Client::getStaticClientPhone($this->client_id);
    
    if ($this->client_id || $phone === null) {
        return '';
    }
    
    return $phone;
    }
    

Upvotes: 1

Views: 33

Answers (1)

Nana Partykar
Nana Partykar

Reputation: 10548

Change

if ($this->client_id || $phone === null) {
    return '';
}

To

if ($this->client_id === null || $phone === null) {
    return '';
}

Or

if (!$this->client_id || $phone === null) {
     return '';
}

Upvotes: 2

Related Questions