php - Can we use one logic expression to compare multiple strings at once? -
i'm trying find simpler way check 1 var not equal multiple values in 1 comparison string.
i found can reduce code empty()
not ==
string values.
empty()
example validate concept.
if (empty($var_1 . $var_2 . $var_3) { echo 'all these vars empty, run code...'; }
the above checks if $var_1, $var_2 , $var_3 empty.
but there way run similar when using !==
?
see code explanation below...
test('unknown_value'); echo php_eol; test('value_1'); function test($var = '') { // below method ideal... // if ($var !== 'value_1' . 'value_2' . 'value_3') { // below method 2nd ideal // if ($var !== 'value_1' , 'value_2' , 'value_3') { // have write below... // i'm looking way not have write $var !== each comparison since not equal if ($var !== 'value_1' , $var !== 'value_2' , $var !== 'value_3') { echo 'failed!!!'; } elseif ($var == 'value_1' or $var == 'value_2' or $var == 'value_3') { echo 'accessed!!!'; } }
use in_array, so:
if (in_array(trim($somevariable), [ 'this', 'that', 'the other'] )) { // $somevariable 1 of elements in array }
Comments
Post a Comment