Home > front end >  Is there PHP shorthand for testing variable value for multiple conditions without using in_array?
Is there PHP shorthand for testing variable value for multiple conditions without using in_array?

Time:12-04

I find myself doing this a lot, and I assume there is a short hand way to make this faster. Yet in my research about shorthand and ternary operations, I can't quite seem to wrap my head around how to do it.

// below variable only be impactful if it's SET, not Null, and === important value.

$verbose_instructively_named_variable = $other_variable. "_modifier";

if(isset($verbose_instructively_named_variable) && $verbose_instructively_named_variable !== null && $verbose_instructively_named_variable === "key_text_modifier"):
     // Now do important thing here;
endif;

I am a beginning programmer obviously, but find myself attracted to longer variable names so when I revisit things later it flows linearly for me. So I find myself wanting to do the below all the time, and am frustrated I can't.

if(isset($verbose_instructively_named_variable) && !==null && === "key_text_modifier"):
     // Now do important stuff;
endif; 

I know this is a PHP question, but I find myself wanting this form of chained logic in javascript also. Am I missing a basic step of some kind?

Is there an altogether different way to test ONE variable for multiple conditions quickly and efficiently?

I have tried combinations of things found in similar questions. Like the in_array solution provided in this answer: In PHP, is there a short way to compare a variable to multiple values?

As well as things like the below standard shortcut/shorthand.

$result = $initial ?: 'default';

But what I want often is something more like this.

$result = ($initial !==null && !=="bad" && !in_array($initial,$record_set_from_mysql_query) ? $initial : 'default');

And keep in mind the main reason I don't like and don't want to do this:

$result = ($initial !==null && $initial !=="bad" $initial !=="even_worse" ? $initial : 'default');

Is because "$initial" maybe named something like $always_make_this_value_green_when_blue_present or something otherwise cumbersome to type repeatedly and it makes the code hard to read later with multi-line parameters in the functions etc.

Presently my best work around for this is to do this.

$a = $long_verbose_instructively_named_variable;
$result = $a !== null && $a !== "bad" && $a !== "even_worse" ? $a : 'default';

But this means in a function with a half dozen little small if/else checks I end up with a, aa, aaa, a_4, a_5 variables all over the place and it just gets cumbersome.

CodePudding user response:

I will still recommend you use in_array at this point. When the array is not big enough, the speed of in_array is still good enough. Because your code will be more easier to read.

As Markus Zeller commented, you can also use a match() method at this condition, you can read from here.

PHP also support array key with characters, it's mean you can use isset() method for your problem, you can read on here.

A tips, you may read PSR-1.

CodePudding user response:

There's a tradeoff between legible code that may be more verbose, and clever code that is shorter but harder to understand. That being said, this is a bit of a kludge but seems to achieve your goal of only referencing $long_verbose_instructively_named_variable once:

switch ($result = ($long_verbose_instructively_named_variable ?? 'default')) {
    //case NULL:
    case 'bad':
    case 'even_worse':
        $result = 'default';
}

The results for various test cases:

For [unset] got 'default'
For false got false
For NULL got 'default'
For 'bad' got 'default'
For 'even_worse' got 'default'
For 'foo' got 'foo'

How I generated these test cases:

$test_cases = [ FALSE, NULL, 'bad', 'even_worse', 'foo' ];

for ( $i=-1; $i < sizeof($test_cases); $i   ) {
    if ( -1 == $i ) {
        unset($long_verbose_instructively_named_variable);
    } else {
        $long_verbose_instructively_named_variable = $test_cases[$i];
    }

    echo "For " . ( (-1 == $i ) ? '[unset]' : var_export($long_verbose_instructively_named_variable, TRUE) );

    switch ($result = ($long_verbose_instructively_named_variable ?? 'default')) {
        //case NULL:
        case 'bad':
        case 'even_worse':
            $result = 'default';
    }

    echo " got " . var_export($result, TRUE) . "\n";
}
  • Related