Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alternative to if(x >= 200 && x <= 299)?

In PHP (or any similar language), is there a better alternative to:

if(x >= 200 && x <= 299){
   return 'ok';
}

My goal is to validate if a number is in the range of the 2xx code (for HTTP requests). I don't like the double-if clause because I must define the end of the range, and for some reason it's not practical when doing various automated validations.

like image 959
FMaz008 Avatar asked Nov 29 '22 03:11

FMaz008


1 Answers

In PHP (or any similar language), is there a better alternative to...

No there isn't, in my opinion.

Your code:

if (x >= 200 && x <= 299) {
    return 'ok';
}

is very readable and clearly defines what is being checked.

like image 147
webbiedave Avatar answered Dec 12 '22 22:12

webbiedave