NameSilo

PHP Switch { case

SpaceshipSpaceship
Namecheap AuctionsNamecheap Auctions
SpaceshipSpaceship
Watch

www.techdo.com

Established Member
Impact
0
Is there a limit to the number of "cases" in the

Switch ( ) {

case..

}

?

i think i'm going to need 50/60 items


thank you for your reply



.
 
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
GoDaddyGoDaddy
I don't think so but TBH I have never needed so many alternatives, perhaps the code can be streamlined in some way :)
 
0
•••
there are no restrictions but to be honest why do you need 60? Maybe a better way of doing it?
 
0
•••
There is no limit, but there might be alternatives. PM me if you need a little help, I have over 10 years of hardcore PHP experience.
 
0
•••
In this case you should better use an array and a forloop to do what you want to do, because 60 cases is messy coding o.0
 
0
•••
Using lot's of arguments could make your code slower and not very professional. Maybe you should use other method, maybe showing more of what you need...
 
0
•••
This thread got me thinking about how a switch in PHP would actually be implemented. In some languages, a compiler implements a switch as a jump table. I though I'd try this out in PHP and was disappointed to find that my switch executed about the same speed as a series of if statements.

So, to answer the OP, this is probably in inefficient way to do what you want. Depending on what it is, you could perhaps implement the same thing using an alternative technique, such as using the condition as a key into an array, with the value of the array representing the operation in some way.
 
0
•••
This thread got me thinking about how a switch in PHP would actually be implemented. In some languages, a compiler implements a switch as a jump table. I though I'd try this out in PHP and was disappointed to find that my switch executed about the same speed as a series of if statements.

Switches are in fact faster. If you want to look properly how it works you would be better getting the opcode 2 comparable pages, 1 using if and 1 using switches
 
0
•••
I have never needed to use 60, lol, but I don't see why there would be any problems, however im sure theres a quicker way fo doign whatever you need to do.
 
0
•••
Switches are in fact faster.

Not in my test.

I wrote 5 functions, each performing the same algorithm, which I chose to be suitable for jump table optimisation. The first used a switch. The second used a sequence of "if" statements. The third used "if ... else" statements. The fourth used "if ...; continue". The fifth used a key into an array to determine the operation. The first and fourth tests took the same time to execute. The second was much slower, the third was slightly slower than the first and the fifth was the fastest.
 
0
•••
Appraise.net

We're social

Escrow.com
Spaceship
Escrowly
CryptoExchange.com
Domain Recover

NamePros updates

Parking: find your domains by pasting a list
You can now paste a list of domain names into the search box at the top of the Parking menu. Put one name on each line. Your domain list...
AIVikings
Catchy
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back