[PHP] Switch with different types
Author | Message |
---|---|
Posey | # Posted yesterday |
Hello. I'm trying to make the switch statement follow a strict comparison model as opposed to the default loose method (in other words, use === instead of ==) in order to make it respect data types.<?phpObviously this code doesn't work the way I intend it to. What are some solutions other than switching to if/else. |
|
Ads | |
Taurre | # Posted two hours ago |
Bare in mind that you're asking PHP, an interpreted high-level language, to juggle types on the fly. You're better off doing a type-check first before proceeding to the conditional loop. | |
Celeri | # Posted one hour ago |
Yeah, and he's asking precisely how to have PHP not juggle types.switch (true) { |
|
Kim Jong Un | # Posted 37 minutes ago |
Celeri's approach is probably the best. Alternatively you can try basic typecasting: switch((string)$value) . |
|
Posey | # Posted 5 minutes ago |
Thanks all, I'll try these methods. |