dsaj20411
2015-12-30 09:26
浏览 39
已采纳

如何解释php中的“condition && $ var = value”语法?

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;
}

图片转代码服务由CSDN问答提供 功能建议

我正在检查一个wordpress主题代码,我找到了这行代码: < pre> 840&lt; = $ width&amp;&amp; $ sizes ='(最大宽度:709px)85vw,(最大宽度:909px)67vw,(最大宽度:1362px)62vw,840px';

这行在php方面意味着什么?

更清楚的是,这是整个函数:

  / **  
 *添加自定义图像尺寸属性以增强内容图像的响应图像功能
 * 
 * @since Twenty Sixteen 1.0 
 * 
 * @param string $ sizes用于''的源尺寸值 sizes'属性。
 * @param数组$ size图像大小。 接受以像素为单位的宽度和高度
 *值的数组(按此顺序)。
 * @return string用于内容图像'sizes'属性的源大小值。
 * / 
function twentysixteen_content_image_sizes_attr($ sizes  ,$ size){
 $ width = $ size [0]; 
 
 840&lt; = $ width&amp;&amp;  $ sizes ='(最大宽度:709px)85vw,(最大宽度:909px)67vw,(最大宽度:1362px)62vw,840px'; 
 
 if('page'=== get_post_type())  {
 840>  $ width&amp;&amp;  $ sizes ='(max-width:'。$ width。'px)85vw,'。  $宽度。  'px'; 
}其他{
 840&gt;  $ width&amp;&amp;  600&lt; = $ width&amp;&amp;  $ sizes ='(max-width:709px)85vw,(max-width:909px)67vw,(max-width:984px)61vw,(max-width:1362px)45vw,600px'; 
 600&gt;  $ width&amp;&amp;  $ sizes ='(max-width:'。$ width。'px)85vw,'。  $宽度。  'px'; 
} 
 
返回$ sizes; 
} 
   
 
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • du77887 2015-12-30 09:29
    已采纳

    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
    
    已采纳该答案
    打赏 评论
  • dongpanbo4727 2015-12-30 09:55

    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
    
    打赏 评论

相关推荐 更多相似问题