Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Can i build a switch() routine dynamically?
Old 05-21-2009, 10:41 AM Can i build a switch() routine dynamically?
Skilled Talker

Posts: 83
Trades: 0
Hello,

I was wondering if it is possible to build up a switch() routine dynamically, based on how many elements there are in an array.

So if there are 3 elements in this array, my switch would need to look like:
PHP Code:
switch ($variable) {
case 
1:
//do stuff
break;

case 
2:
//do stuff
break;

case 
3:
//do stuff
break;

default:
//do stuff
break;

Or, if there are 2 elements in this array, my switch would need to look like:
PHP Code:
switch ($variable) {
case 
1:
//do stuff
break;

case 
2:
 
//do stuff
 
break;

default:
 
//do stuff
 
break;

Is this somehow possible?

thanks!

Matt
killerwhale65 is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 05-21-2009, 11:44 AM Re: Can i build a switch() routine dynamically?
helloworlder's Avatar
Novice Talker

Posts: 10
Trades: 0
Can you give some background on what you're trying to do? :-)
__________________

Please login or register to view this content. Registration is FREE
- Learn Chinese through bite sized blocks

Please login or register to view this content. Registration is FREE
- Land the Plane

Please login or register to view this content. Registration is FREE
- Blog
helloworlder is offline
Reply With Quote
View Public Profile
 
Old 05-21-2009, 11:50 AM Re: Can i build a switch() routine dynamically?
NullPointer's Avatar
Will Code for Food

Posts: 2,815
Name: Matt
Location: Irvine, CA
Trades: 0
You might be able to hack something like that together using eval() but I don't recommend it. Is there any reason you can't use a loop:
PHP Code:
for($i 0$i $arraySize$i++)
{
     if(
$variable == $i)
     {
          
//do stuff
     
}

More details might yeild a better answer.
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
NullPointer is offline
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Old 05-21-2009, 01:24 PM Re: Can i build a switch() routine dynamically?
JeremyMiller's Avatar
WT Moderator

Posts: 1,712
Name: Jeremy Miller
Location: Las Vegas, NV
Trades: 0
Matt's code is suggesting a question: What is the "stuff" you want to do? Very likely, if you are trying to do a dynamic switch, you have something off in how you're thinking about the problem. Try presenting us with the problem you're solving with this switch and we can look at suggesting an alternative solution.
__________________
Jeremy Miller

Please login or register to view this content. Registration is FREE
JeremyMiller is offline
Reply With Quote
View Public Profile Visit JeremyMiller's homepage!
 
Reply     « Reply to Can i build a switch() routine dynamically?
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.13499 seconds with 12 queries