English 中文(简体)
php与任何同等操作者匹配
原标题:php matches any / match any operator equivalent

我经常发现自己撰写的文章,例如:if($var=bob , Kluwer = pineven , Kluwer = jacode ){......

是否在营地安装了扫描仪,以检查一个var是否与任何经营者相匹配? 或者,我必须写一下我自己的职务:

function matches_any($question, $pos_answer, $pos_answer, $pos_answer, $pos_answer, ...){
// Call the function args, get the first one, get the rest, compare all the rest, return true if any match, return false by default.
}

因此:

if(matches_any($var, $bob, $steven,  james , $william)){ ...
最佳回答

您可使用in_array(

if(in_array( value_to_test , array($var, $bob, $steven,  james , $william))) {}

$matches = array($var, $bob, $steven,  james , $william);
if (in_array( james , $matches)) { 
  // TRUE 
}
问题回答

Ideally you should avoid hard-coding any values into your code -- if you keep content in data structures (=think config or database ), you will minimize your chances for running into this problem because you re processing abstractions rather than specific values. This means changing your concept from "these are the definite people who can ever do this" into "let s ask another facility whether this guy can do this"

If you absolutely positively must have a function like what you described, you can do it with func_get_args():

function matches_any()
{
    $haystack = func_get_args();        
    $needle = array_shift( $haystack );

    return in_array( $needle, $haystack );
}

(未测试)





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签