[英]Is there any shortcut for this logical operator?
I was just wondering if there is a shortcut to do something like this example : 我只是想知道是否有一个快捷方式来做这样的例子:
if ( isset($_POST['name']) && $_POST['name'] == 'George' || $_POST['name'] == 'Mike' );
[EDIT] : Sorry if the question seemed ambiguous at first but what I meant to ask is "since I am doing all those checks on the same variable is there a better way not to write it three times?" [编辑]:对不起,如果这个问题起初看起来很模糊,但我要问的是“因为我对同一个变量做了所有这些检查,有没有更好的方法不写三次?”
is there any easier way that is rare and .. 是否有任何更容易的方式是罕见的..
This is just a general knowledge question, I was just getting bored of writing nested ifs or long logical operations like the one above. 这只是一个常识性问题,我只是厌倦了编写嵌套ifs或长逻辑操作,如上所述。
You could define a function for getting post data: 您可以定义获取发布数据的函数:
function getPOST($name) {
return isset($_POST[$name]) ? $_POST[$name] : null;
}
The function will never show the notice about accessing a not existing array field and the result can be easily compared to the list of values in your example: 该函数永远不会显示有关访问不存在的数组字段的通知,并且结果可以轻松地与示例中的值列表进行比较:
if (in_array(getPOST('name'), array('George', 'Mike')) {
}
I am not sure if I understood your question right, but looks like you want to compare a particular variable to a set of values. 我不确定我是否理解你的问题,但看起来你想要将特定变量与一组值进行比较。 You can do the following:
您可以执行以下操作:
in_array
function to find if the value of the variable exists in the array of values. in_array
函数查找变量值是否存在于值数组中。 在Ruby中
if $_POST['name'] && ['George', 'Mike'].include?($_POST['name'])
Use isset
to make sure the value is not NULL
and then strlen
to make sure you actually have something in there. 使用
isset
确保值不是NULL
然后strlen
以确保你实际上有东西在那里。
if(isset($_POST['name']) && strlen($_POST['name']) > 0)
{
//Do stuff
}
function post_data($key){
if(isset($_POST[$key]) && $_POST[$key]!='') return $_POST[$key];
return null;
}
$possible_names = array('George', 'Mike');
if(in_array(post_data('name'), $possible_names)) {}
Well, the question is extremely general. 嗯,问题非常笼统。 How can you not use the same variable name many times?
你怎么不多次使用相同的变量名? Well, what does your code look like and I'll tell you :) In general that's not really a possible question to answer.
那么,你的代码是什么样的,我会告诉你:) 一般来说 ,这不是一个可能的问题。 In your example case, you could do something like this (in C#):
在您的示例中,您可以执行以下操作(在C#中):
string x = "foo";
if ( new string[]{"bar","baz","foo","foobar"}.Contains(x)) ....
Or: 要么:
public static class Extensions {
public static bool In<T>(this T needle, params T[] haystack){
return haystack.Contains(needle);
}
}
string x = "foo";
if (x.In("bar", "baz", "foo", "foobar")) {
The Symfony 1.4 framework utilizes sfParameterHolder
to deal with the problem of trying to determine whether an element is present in an array. Symfony 1.4框架利用
sfParameterHolder
来处理试图确定元素是否存在于数组中的问题。
You could clean things up a bit by copying/duplicating the functionality of sfParameterHolder
in your project. 您可以通过在项目中复制/复制
sfParameterHolder
的功能来清理一些东西。
You can also use a switch
statement if you prefer that syntax to clean up all the values in your conditionals. 如果您希望使用该语法清除条件中的所有值,也可以使用
switch
语句。
The end result might look something like this: 最终结果可能如下所示:
$post = new sfParameterHolder($_POST);
switch( $post->get('name') )
{
case 'george':
case 'mike':
// Do something.
break;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.