我尝试用变量替换递归回声。
我得到的结果不一样
这是工作脚本:
if ( $currentpage <= 3 && $currentpage != "all" ) {
$a = 3;
echo '<a class="page bgcolor2" href="?pagenb=' . ($a-2) . '">' . ($a-2) . '</a>';
echo '<a class="page bgcolor2" href="?pagenb=' . ($a-1) . '">' . ($a-1) . '</a>';
echo '<a class="page bgcolor2" href="?pagenb=' . ($a) . '">' . ($a) . '</a>';
echo '<a class="page bgcolor2" href="?pagenb=' . ($a+1) . '">' . ($a+1) . '</a>';
echo '<a class="page bgcolor2" href="?pagenb=' . ($a+2) . '">' . ($a+2) . '</a>';
}
if ( $currentpage > 3 && $currentpage != "all" && $currentpage < $pages-2) {
$a = $currentpage;
echo '<a class="page bgcolor2" href="?pagenb=' . ($a-2) . '">' . ($a-2) . '</a>';
echo '<a class="page bgcolor2" href="?pagenb=' . ($a-1) . '">' . ($a-1) . '</a>';
echo '<a class="page bgcolor2" href="?pagenb=' . ($a) . '">' . ($a) . '</a>';
echo '<a class="page bgcolor2" href="?pagenb=' . ($a+1) . '">' . ($a+1) . '</a>';
echo '<a class="page bgcolor2" href="?pagenb=' . ($a+2) . '">' . ($a+2) . '</a>';
}
if ( $currentpage >= $pages-2 && $currentpage != "all" ) {
$a = $pages-2;
echo '<a class="page bgcolor2" href="?pagenb=' . ($a-2) . '">' . ($a-2) . '</a>';
echo '<a class="page bgcolor2" href="?pagenb=' . ($a-1) . '">' . ($a-1) . '</a>';
echo '<a class="page bgcolor2" href="?pagenb=' . ($a) . '">' . ($a) . '</a>';
echo '<a class="page bgcolor2" href="?pagenb=' . ($a+1) . '">' . ($a+1) . '</a>';
echo '<a class="page bgcolor2" href="?pagenb=' . ($a+2) . '">' . ($a+2) . '</a>';
}
我的尝试没有成功(没有错误,但结果不一样):
$pagenb = '<a class="page bgcolor2" href="?pagenb=' . ($a-2) . '">' . ($a-2) . '</a>
<a class="page bgcolor2" href="?pagenb=' . ($a-1) . '">' . ($a-1) . '</a>
<a class="page bgcolor2" href="?pagenb=' . ($a) . '">' . ($a) . '</a>
<a class="page bgcolor2" href="?pagenb=' . ($a+1) . '">' . ($a+1) . '</a>
<a class="page bgcolor2" href="?pagenb=' . ($a+2) . '">' . ($a+2) . '</a>';
if ( $currentpage <= 3 && $currentpage != "all" ) {
$a = 3;
echo $pagenb;
}
if ( $currentpage > 3 && $currentpage != "all" && $currentpage < $pages-2) {
$a = $currentpage;
echo $pagenb;
}
if ( $currentpage >= $pages-2 && $currentpage != "all" ) {
$a = $pages-2;
echo $pagenb;
}
答案 0 :(得分:3)
尝试并稍微移动代码:
if ( $currentpage <= 3 && $currentpage != "all" ) {
$a = 3;
}
if ( $currentpage > 3 && $currentpage != "all" && $currentpage < $pages-2) {
$a = $currentpage;
}
if ( $currentpage >= $pages-2 && $currentpage != "all" ) {
$a = $pages-2;
}
$pagenb = '<a class="page bgcolor2" href="?pagenb=' . ($a-2) . '">' . ($a-2) . '</a>
<a class="page bgcolor2" href="?pagenb=' . ($a-1) . '">' . ($a-1) . '</a>
<a class="page bgcolor2" href="?pagenb=' . ($a) . '">' . ($a) . '</a>
<a class="page bgcolor2" href="?pagenb=' . ($a+1) . '">' . ($a+1) . '</a>
<a class="page bgcolor2" href="?pagenb=' . ($a+2) . '">' . ($a+2) . '</a>';
echo $pagenb;
?>
答案 1 :(得分:3)
PHP本身没有懒惰的评估(除了eval()之外只有...)
但是你可以把它写成你用$a
作为函数参数调用的函数:
function pagenb ($a) {
return '<a class="page bgcolor2" href="?pagenb=' . ($a-2) . '">' . ($a-2) . '</a>
<a class="page bgcolor2" href="?pagenb=' . ($a-1) . '">' . ($a-1) . '</a>
<a class="page bgcolor2" href="?pagenb=' . ($a) . '">' . ($a) . '</a>
<a class="page bgcolor2" href="?pagenb=' . ($a+1) . '">' . ($a+1) . '</a>
<a class="page bgcolor2" href="?pagenb=' . ($a+2) . '">' . ($a+2) . '</a>';
}
if ( $currentpage <= 3 && $currentpage != "all" ) {
echo pagenb(3);
}
if ( $currentpage > 3 && $currentpage != "all" && $currentpage < $pages-2) {
echo pagenb($currentpage);
}
if ( $currentpage >= $pages-2 && $currentpage != "all" ) {
echo pagenb($pages-2);
}
答案 2 :(得分:1)
以下是使用分配给变量的匿名函数的解决方案:
<?php
$pages = 10;
$currentpage = 8;
$pagination = function($value) {
for ($a = $value, $p = -2; $p <= 2; $p++) {
echo '<a class="page bgcolor2" href="?pagenb=' . ($a+$p) . '">' . ($a+$p) . '</a>';
}
};
if ($currentpage != 'all') {
if ($currentpage <= 3) {
$pagination(3);
} elseif ($currentpage > 3 && $currentpage < $pages-2) {
$pagination($currentpage);
} elseif ($currentpage >= $pages-2) {
$pagination($pages-2);
}
}