[英]PHP: what does a double assignment look like in longform?
I wasn't even sure how to Google this. 我甚至不确定如何谷歌这个。 How would this PHP statement be written longform?
如何将这个PHP语句写成longform?
$recentlyViewed = $products = $this->getRecentlyViewedProducts();
Optimizations like this make experts feel smart, and beginners feel really stupid. 这样的优化让专家感觉很聪明,初学者觉得非常愚蠢。 I'm pretty sure I understand what the outcome is, but maybe I'm wrong.
我很确定我明白结果是什么,但也许我错了。
$products = $this->getRecentlyViewedProducts();
$recentlyViewed = ($products) ? true : false;
$products = $this->getRecentlyViewedProducts();
$recentlyViewed = $products;
Via Twitter, seems B is equivalent. 通过Twitter,似乎B是等价的。
Write glaringly simple code. 写出明显简单的代码。 Don't be clever.
别聪明。
$recentlyViewed = $products = $this->getRecentlyViewedProducts();
And 和
$products = $this->getRecentlyViewedProducts();
$recentlyViewed = ($products) ? true : false;
I think this is equivalent:
我认为这是等价的:
Nope its not equivalent. 不,它不相同。
Let's see the difference 让我们看看差异
$recentlyViewed = $products = range(1,10);
So if you print_r
then the value'll be 所以如果你
print_r
那么值就是
print_r($recentlyViewed);
print_r($products);
This'll print two arrays from [1,2,3,....10]
but the 这将从
[1,2,3,....10]
打印两个阵列但是
$products = range(1,10);
$recentlyViewed = ($products) ? true : false;
So if you print the $products
and $recentlyViewed
then the result will be the first'll print an array
and the other one'll print 1
. 因此,如果您打印
$products
和$recentlyViewed
recentViewed,那么结果将是第一个打印array
,另一个将打印1
。
So whats the equivalent of 所以什么相当于
$recentlyViewed = $products = $this->getRecentlyViewedProducts();
will be 将会
$products = $this->getRecentlyViewedProducts();
$recentlyViewed = $products;
The equivalent is this 相当于此
$products = $this->getRecent();
$recentlyViewed = $products;
I'm not sure how a test for $products
would make sense there as the double assignment does not return booleans. 我不确定对于
$products
的测试是如何有意义的,因为双重赋值不会返回布尔值。
See here the difference between raw types and objects. 在这里看到原始类型和对象之间的区别。
Are multiple variable assignments done by value or reference? 多个变量赋值是通过值还是引用完成的?
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). PHP所做的是从右手乞求并将最正确的值分配给左侧变量(如果有的话)。 This value can be a const value (ie string or number), another variable or return value of a function (
$this->getRecentlyViewedProducts()
in this case). 此值可以是const值(即字符串或数字),函数的另一个变量或返回值
$this->getRecentlyViewedProducts()
在本例中$this->getRecentlyViewedProducts()
)。 So here are steps: 所以这是步骤:
(
$this->getRecentlyViewedProducts() in this case)
(
$这个- > getRecentlyViewedProducts() in this case)
$products
$products
$product
to $recentlyViewed
$product
分配给$recentlyViewed
so if we assume your getRecentlyViewedProducts
function returns 'Hello Brendan!', at the end of execution, both $products
and $recentlyViewed
would have same value. 因此,如果我们假设您的
getRecentlyViewedProducts
函数返回'Hello Brendan!',则在执行结束时, $products
和$recentlyViewed
将具有相同的值。
In PHP, variable types are implicit and therefore you can use them directly in if
statements like this: 在PHP中,变量类型是隐式的,因此您可以在
if
语句中直接使用它们:
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. 在这种情况下,如果
$recentlyViewed
是设置好的,它的值$recentlyViewed
是什么,其他是0
, false
或null
,你if
条件将满足。
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): 在PHP中使用非布尔值作为检查条件是很常见的,无论如何,如果你使用
$recentlyViewed
作为标志,为了代码可读性和内存优化,最好这样做(注意如果你的函数返回例如一个大字符串,将其值复制到一个单独的变量中,将其用作标志不是明智的选择):
$recentlyViewed = $products ? true : false;
or 要么
$recentlyViewed = $products ? 1 : 0;
althogh the result would not be different. althogh结果不会有所不同。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.