When you write:
$recentlyViewed = $products = $this->getRecentlyViewedProducts();
what PHP does is that begging from the right hand and assigns most right value to the left side variable (if any). This value can be a const value (i.e. string or number), another variable or return value of a function ($this->getRecentlyViewedProducts()
in this case). So here are steps:
- calculate return value of
(
$this->getRecentlyViewedProducts()in this case)
- assign calculated value to
$products
- assign
$product
to $recentlyViewed
so if we assume your getRecentlyViewedProducts
function returns 'Hello Brendan!', at the end of execution, both $products
and $recentlyViewed
would have same value.
In PHP, variable types are implicit and therefore you can use them directly in if
statements like this:
if($recentlyViewed)
{
...
}
and in this case if $recentlyViewed
is setted and its value $recentlyViewed
is anything other that 0
, false
or null
, your if
condition will satisfy.
It's very common to use non-boolean values in PHP as check conditions, anyway if you are using $recentlyViewed
just as a flag, it's better to do this for the sake of code readability and memory optimization (attention that if your function returns for example a large string, copying its value in a separate variable to use it as just a flag is not a wise choice):
$recentlyViewed = $products ? true : false;
or
$recentlyViewed = $products ? 1 : 0;
althogh the result would not be different.