dongxing7083 2013-06-17 17:48
浏览 80

PHP:在行内添加If条件

    if($i==6) {
    $result .= '<a style="display:none" class="fancybox" data-fancybox-group="gallery" href="'.$value['images']['standard_resolution']['url'].'"></a> '.$caption.PHP_EOL;;}
    else {
    $result .= "\t".'<a class="fancybox" data-fancybox-group="gallery" href="'.$value['images']['standard_resolution']['url'].'"><img id="thumb'.++$i.'" src="'.$value['images']['low_resolution']['url'].'" alt="'.$value['caption']['text'].'" width="'.$width.'" height="'.$height.'" /></a> '.$caption.PHP_EOL;}

This means: for all the items greater than 6 $result is display:none and blahblah, for all the others $result is images with caption.

What I'd like to add is: if counter is EQUAL to 4, print the "else" $result with the ONLY change that instead of src="'.$value['images']['low_resolution']['url'].'", it would be src="'.$value['images']['thumb_resolution']['url'].'"

I think that this would work, but I'm looking for a cooler, one-liner way :D

    if($i==6) {
    $result .= '<a style="display:none" class="fancybox" data-fancybox-group="gallery" href="'.$value['images']['standard_resolution']['url'].'"></a> '.$caption.PHP_EOL;;}
    elseif($i===4) {
    $result .= "\t".'<a class="fancybox" data-fancybox-group="gallery" href="'.$value['images']['standard_resolution']['url'].'"><img id="thumb'.++$i.'" src="'.$value['images']['thumb_resolution']['url'].'" alt="'.$value['caption']['text'].'" width="'.$width.'" height="'.$height.'" /></a> '.$caption.PHP_EOL;}
    else {
    $result .= "\t".'<a class="fancybox" data-fancybox-group="gallery" href="'.$value['images']['standard_resolution']['url'].'"><img id="thumb'.++$i.'" src="'.$value['images']['low_resolution']['url'].'" alt="'.$value['caption']['text'].'" width="'.$width.'" height="'.$height.'" /></a> '.$caption.PHP_EOL;}
  • 写回答

3条回答 默认 最新

  • doue1925 2013-06-17 18:26
    关注

    Look up the conditional operator, or ternary operator. It is used like this:

    $check = true;
    $var = $check ? 5 : 3;
    

    The conditional operator checks the value of the expression before the ? and outputs either the value before the : if the expression is true or the value after the ':' if it is false. So the line above sets $var to 5 because $check is true. You could also have done this:

    $check = 2354;
    $var = ($check < 40) ? 5 : 3;
    

    That would have given you the opposite result, $var would have been set to 3, because $check did not meet the condition $check < 40. Think of it like a function which outputs one of two possible values based on the truth value of a given expression. You can embed that directly into the result .= as:

    ($i === 4) ? $value['images']['low_resolution']['url'] : $value['images']['thumb_resolution']['url'];
    

    For more information about the conditional operator, you can look at the PHP documentation page about it here. Note that the part about the conditional operator is a little way down the page.

    评论

报告相同问题?

悬赏问题

  • ¥20 易康econgnition精度验证
  • ¥15 线程问题判断多次进入
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致