What is faster: many ifs, or else if?

后端 未结 11 1842
夕颜
夕颜 2020-12-29 22:35

I\'m iterating through an array and sorting it by values into days of the week.

In order to do it I\'m using many if statements. Does it make any differ

11条回答
  •  醉话见心
    2020-12-29 23:09

    To be honest I don't think it would matter which way you do it in terms of performance, I doubt you would see any difference. I would recommend using a switch statement which isn't a performance enhancment, simply syntactically nicer:

    switch ($day) 
    {
        case "Monday":
            // do something with Monday
            break;
        case "Tuesday":
            // do something with Tuesday
            break;
        case "Wednesday":
            // do something with Wednesday
            break;
    }
    

提交回复
热议问题