Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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

share|improve this question

2 Answers 2

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

share|improve this answer

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.