Jump to content

Recommended Posts

This is quite simple, however i would like your opinions...

 

When should i use switch instead of ifelse ?

 

after 2 or more possibilities?

5 or more?

 

Currently i am using this for the most simplistic design:

else if($command=='content'&&count($commands)>0){
$content = 'n/a';
if($commands[0]=='dashboard'){
	$content = 'Dashboard';
}
elseif($commands[0]=='settings'&&isset($commands[1])){
	if($commands[1]=='general'){
		$content = 'Configuration Settings = General Settings';
	}
	elseif($commands[1]=='server'){
		$content = 'Configuration Settings = Server Settings';
	}
	elseif($commands[1]=='member'){
		$content = 'Configuration Settings = Member Settings';
	}
	elseif($commands[1]=='page'){
		$content = 'Configuration Settings = Page Settings';
	}
	elseif($commands[1]=='plugin'){
		$content = 'Configuration Settings = Plugin Settings';
	}
	elseif($commands[1]=='file'){
		$content = 'Configuration Settings = File Settings';
	}
}
styler::validExit($content);
}

 

now i think i should switch to 'switch' statements; but this is just the beginning.

 

ps: this is the core of my administration panel for my cms.

Link to comment
https://forums.phpfreaks.com/topic/263841-design-pattern/
Share on other sites

I usually use switch when I have more than 2 cases.

 

I think it is more of a preference thing and what you think is easier to read. I've seen some benchmarks where switch is faster and I've seen some where if/else is faster. This tells me there isn't a huge performance difference, so use whatever you like more.

Link to comment
https://forums.phpfreaks.com/topic/263841-design-pattern/#findComment-1352061
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.