如何比6x OR语句更好地做到这一点


How to do this better than 6x OR statements

我对编程完全陌生,找不到答案,或者我根本不明白答案。

不管怎样,这都是我的代码。我如何更改它,使其不使用6或语句?

 {if $smarty.server.REQUEST_URI == '/home-product.php?id_product=1' 
OR $smarty.server.REQUEST_URI == '/home-product.php?id_product=2'
OR $smarty.server.REQUEST_URI == '/home-product.php?id_product=3'
OR $smarty.server.REQUEST_URI == '/home-product.php?id_product=4'
OR $smarty.server.REQUEST_URI == '/home-product.php?id_product=5'
OR $smarty.server.REQUEST_URI == '/home-product.php?id_product=6'}

在我看来,你不应该在Smarty中做这样的事情。您应该使用例如in_array在PHP中执行此操作。Smarty只是用于显示数据,而不是用于此类比较。

您也可以使用Smarty语法作为:

{assign var="test" value ="5"}
{if in_array($test, array(1,2,5,8))}
is in array
{else}
is not in array
{/if}

编辑

在这种情况下,它可能很简单:

{if in_array($smarty.server.REQUEST_URI, 
    array(
    '/home-product.php?id_product=1',
    '/home-product.php?id_product=2',
    '/home-product.php?id_product=3',
    '/home-product.php?id_product=4',
    '/home-product.php?id_product=5',
    '/home-product.php?id_product=6'
    ))}
is in array
{else}
is not in array
{/if}

您可以直接在Smarty中访问GET变量。

{if in_array($smarty.get.id_product, range(1,6))}
    is in array
{else}
    is not in array
{/if}
$num = substr($smarty.server.REQUEST_URI, -1);
if ($num > 0 AND $num < 7) { /* TRUE */ }

显然,这是PHP,而不是Smarty语法。