清理PHP [英] Cleaning up PHP

查看:122
本文介绍了清理PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

无论如何我可以清理

if($class == 2 AND $posts >=1){$showpost ="1";} else {$showpost ="$posts";
if($class == 3 AND $posts >=2){$showpost ="2";} else {$showpost ="$posts";
if($class == 4 AND $posts >=3){$showpost ="3";} else {$showpost ="$posts";
if($class == 5 AND $posts >=4){$showpost ="4";} else {$showpost ="$posts";
if($class == 6 AND $posts >=5){$showpost ="5";} else {$showpost ="$posts";
if($class == 7 AND $posts >=6){$showpost ="6";} else {$showpost ="$posts";
}}}}}}

因为我不想要}}}}}最后。

As I don't want }}}}}} at the end.

推荐答案

if (($class >= 2) && ($class <= 7) && ($posts >= $class - 1))
    $showpost = $class - 1;
else
    $showpost = $posts;

如果分为两部分:

 if (
     ($class >= 2) && ($class <= 7) // Check $class is between 2 and 7 inclusive

     && ($posts >= $class - 1)) // Check $posts >= $class -1
                                // so if $class is 4 this checks if $posts >= 3

这与原始代码中的逻辑 - 这只是看到模式的问题。

This matches the logic in your original code - it's just a matter of seeing the pattern.

如果你需要$ showpost是一个字符串(很可能你没有),你可以像这样强制转换为字符串:

If you need $showpost to be a string (chances are you don't), you can cast to string like this:

$showpost = (string)$showpost;

这篇关于清理PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆