I am inspecting a wordpress theme code and I found this row of code:
840 <= $width && $sizes = '(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 1362px) 62vw, 840px';
What does this row mean in terms of php?
To be more clear, this is the whole function:
/**
* Add custom image sizes attribute to enhance responsive image functionality
* for content images
*
* @since Twenty Sixteen 1.0
*
* @param string $sizes A source size value for use in a 'sizes' attribute.
* @param array $size Image size. Accepts an array of width and height
* values in pixels (in that order).
* @return string A source size value for use in a content image 'sizes' attribute.
*/
function twentysixteen_content_image_sizes_attr( $sizes, $size ) {
$width = $size[0];
840 <= $width && $sizes = '(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 1362px) 62vw, 840px';
if ( 'page' === get_post_type() ) {
840 > $width && $sizes = '(max-width: ' . $width . 'px) 85vw, ' . $width . 'px';
} else {
840 > $width && 600 <= $width && $sizes = '(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px';
600 > $width && $sizes = '(max-width: ' . $width . 'px) 85vw, ' . $width . 'px';
}
return $sizes;
}
This checks a condition and sets a variable in case it is true.
It is like saying:
conditionA && $variable="value";
In your case, it is like saying:
if (840 <= $width) {
$sizes = '(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 1362px) 62vw, 840px';
}
See a little test:
<?php
$a=3;
$v=0;
echo "v = $v
";
$a == 3 && $v = "hello";
echo "v = $v";
If you execute it you get:
v = 0
v = hello
just like this:
840 <= $width && $sizes = '(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 1362px) 62vw, 840px';
is
conditionA && conditionB
equal to
if( conditionA && conditionB ){}
or
if( conditionA ) { conditionB }
the same with
conditionA || conditionB
like
if( conditionA || conditionB ){}
for example
$a = 1;
$b = 0;
// if( $a == 2 && $b = 3 ){}
$a == 2 && $b = 3; // && do not exec $b = 3,becuase $a != 2
echo $a,' ',$b; // 1 0
// if( $a == 1 && $b = 4 ){}
$a == 1 && $b = 4; // exec $b = 4,becuase $a == 1
echo $a,' ',$b; // 1 4
// if( $a == 1 && $b = 4 ){}
// and how about ||
$a = 1;
$b = 0;
// if( $a == 1 || $b = 3 ){}
$a == 1 || $b = 3; // && do not exec $b = 3,becuase $a == 1
echo $a,' ',$b; // 1 0
// if( $a == 2 || $b = 4 ){}
$a == 2 || $b = 4; // exec $b = 4,becuase $a == 1 not 2
echo $a,' ',$b; // 1 4