I was wondering if there's a one liner for PHP
that would allow me test multiple values against a function call. As example, say I want to test if foo()
returns either 1 or 2, (in pseudo code)
if( [1||2]==foo() );
Maybe above is a bad example of what I mean. Currently if I want to test two values against a function call I would use:
$test = foo();
if( 1==$test || 2==$test );
Its these two lines that I would like to simplify into one
2 Answers 2
There is nothing like that in PHP, however you can go for something like this -- even tho I'm not really recommending it:
if ( in_array(foo(), array(1, 2), true) ) ...
Please note that it's not gonna work on &&
conditions and you're limited only to ==
and ===
.
In the specific example given this would work:
if ( abs( foo() - 1.5 ) == 0.5 )
This works for two number values, but for the obvious reasons is not very easy to generalise