我怎样才能缩短这个PHP if语句? [英] How can I shorten this PHP if statement?

查看:151
本文介绍了我怎样才能缩短这个PHP if语句?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个if语句,我需要一个字符串比较许多不同的选择。在code下面我发布显示了我的意思是pretty清楚。我知道这样做的2种方式,但是其他甚至更长。那么,有没有使这样的事情,但在一个较短的方式任意功能?它可能看起来很可笑我问了,但我希望子页面的列表中增长非常快。这是code:

I have an if statement, and I need to compare a single string with many different options. The code I post below shows what I mean pretty clear. I know 2 ways of doing this, but the other is even longer. So, is there any function that makes something like this, but in a shorter way? It might seem silly what I asked for, but I expect the list of subpages to grow really fast. This is the code:

if  (
    $Url[0]=="contact"||
    $Url[0]=="report"||
    $Url[0]=="new"
    )
    {
    //Do something
    }

和我想知道如果有这样的事情:

And I'd like to know if there's something like this:

if ( weirdcomparefunction ($Url[0],"contact","report","new") )
    {
    //Do something
    }

我看到创建我自己的函数的问题在于传递变量的数量是(讽刺)变量。

The problem I see to create my own function is that the number of variables passed is (ironically) variable.

甚至是这样的:

$centered = array ("contact","report","new");
if ( weirdcomparefunction ($Url[0],$centered) )
    {
    //Do something
    }

我也想创建一个.txt来存储所有特征线子页面,但我需要一个大的脚本,所以我想首先要知道如果已经有一个weirdcomparefunction()。因此,就需要很短,否则我有没有问题做了一些田地和IFS约10-20线。

I'm also thinking about creating a .txt to store all the subpages with breaklines, but I'd need a big script so I'd like to know first if there is already a weirdcomparefunction(). Therefore it'd need to be SHORT, otherwise I'd have no problem making about 10-20 lines with some whiles and ifs.

感谢大家。

推荐答案

<一个href=\"http://php.net/manual/en/function.in-array.php\">http://php.net/manual/en/function.in-array.php

$centered = array ("contact","report","new");
if(in_array($Url[0], $centered)) {
    //Do something
}

这篇关于我怎样才能缩短这个PHP if语句?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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