doucan1996 2014-04-10 18:58
浏览 58
已采纳

替代使用'is_null'为坏链接创建默认路径

I'm using a loop to display SQL data. All the rows in my db contain some URL for an image src, however there is not actually an image stored at the location (yet) for some the URLs.

How would I default those 'bad links' to a particular image path. I was trying the 'is_null' function but I'm realizing it will not work the way as I am using it because there is in fact a URL coming from the dp - its just a bad link. Is there an alternative method for what I'm trying to do? A way to verify there is actually in image for the URL specified. Here's my dysfunctional code:

 if(is_null($smaller_img)){$smaller_img = "http://default image";}
  • 写回答

1条回答 默认 最新

  • doushi1912 2014-04-10 19:00
    关注

    If is_null is not working for you that means you are getting some other kind of "empty" value (perhaps an empty string?). You might want to find out why and fix that -- if the data is coming from SQL then it should definitely be null to signify the absence of an image.

    That said, in this case you can simply use the ?: operator:

    echo $smaller_img ?: "http://default image";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?