格式化if语句以提高可读性

编程入门 行业动态 更新时间:2024-10-27 00:31:22
本文介绍了格式化if语句以提高可读性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

if(strpos($ file,'.jpg',1 )&& file_exists($ thumbsdir / $ file)== false || strpos($ file,'.gif',1)&& file_exists($ thumbsdir / $ file)== false | | strpos($ file,'.png',1)&& file_exists($ thumbsdir / $ file)== false){ createThumb($ gallerydir / $ file,$ thumbsdir / $文件,$ thumbsize); fwrite($ log,date(Y-m-d)。@.date(H:i:s)。CREATED:$ thumbsdir / $ file \\\);

解决方案

是一个图像的逻辑到自己的功能,这使得如果更具可读性,也允许您集中逻辑。

函数is_image($ filename){ $ image_extensions = array('png','gif','jpg'); $ b foreach($ image_extensions as $ extension) if(strrpos($ filename,。$ extension)!== FALSE) return true; 返回false; if(is_image($ file)&!file_exists($ thumbsdir / $ file)){ createThumb($ gallerydir / $ file ,$ thumbsdir / $ file,$ thumbsize); fwrite($ log,date(Y-m-d)。@.date(H:i:s)。CREATED:$ thumbsdir / $ file \\\); }

What's the best way to format this for readability?

if (strpos($file, '.jpg',1) && file_exists("$thumbsdir/$file") == false || strpos($file, '.gif',1) && file_exists("$thumbsdir/$file") == false || strpos($file, '.png',1) && file_exists("$thumbsdir/$file") == false) { createThumb("$gallerydir/$file", "$thumbsdir/$file",$thumbsize); fwrite($log,date("Y-m-d")." @ ".date("H:i:s")." CREATED: $thumbsdir/$file\n"); }

解决方案

I'd extract the "is an image" logic into its own function, which makes the if more readable and also allows you to centralize the logic.

function is_image($filename) { $image_extensions = array('png', 'gif', 'jpg'); foreach ($image_extensions as $extension) if (strrpos($filename, ".$extension") !== FALSE) return true; return false; } if (is_image($file) && !file_exists("$thumbsdir/$file")) { createThumb("$gallerydir/$file", "$thumbsdir/$file",$thumbsize); fwrite($log,date("Y-m-d")." @ ".date("H:i:s")." CREATED: $thumbsdir/$file\n"); }

更多推荐

格式化if语句以提高可读性

本文发布于:2023-11-06 01:49:35,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1562413.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:可读性   语句

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!