Question

I wanna compare string with "enumeration", can I do it simplier than in my solution bellow? First I know the enum type is not implemented in php.
Basically the problem question is: Is string s equal to one of the strings? Since php don't have enum, the values of enum could be probably in some array or something like that.


//$min_s is string value of some minute value for example "15".
if((strcmp($min_s, "00")== 0 || str_cmp($min_s,"15") == 0 ||
            strcmp($min_s, "30")== 0 || strcmp($min_s, "45") == 0)
{ // ok}

Goal:
Make my ifs more readable for this specific example and others in future when I wanna compare string to "enum".

Was it helpful?

Solution

To answer your question literally:

$fifteens = array('00', '15', '30', '45');
if (in_array($min_s, $fifteens)) {
    ...
}

But I would actually use arithmetic:

if ($min_s % 15 == 0) {
    ...
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top