简体   繁体   中英

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 ..

  1. cleaner than nesting.
  2. shorter than the syntax above?
  3. in any of (Ruby, C#, or PHP) since I mainly use those 3 languages.

This is just a general knowledge question, I was just getting bored of writing nested ifs or long logical operations like the one above.

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:

  1. Put all the values in an array.
  2. Check the variable if it is set.
  3. Use in_array function to find if the value of the variable exists in the array of values.

在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.

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#):

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.

You could clean things up a bit by copying/duplicating the functionality of sfParameterHolder in your project.

You can also use a switch statement if you prefer that syntax to clean up all the values in your conditionals.

The end result might look something like this:

$post = new sfParameterHolder($_POST);

switch( $post->get('name') )
{
  case 'george':
  case 'mike':
    // Do something.
    break;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM