Switch function with function in case statement?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jraede
Forum Contributor
Posts: 254
Joined: Tue Feb 16, 2010 5:39 pm

Switch function with function in case statement?

Post by jraede »

Currently my URL rerouting engine has a bunch of if-elseif-else statements that go through the various types of displays. However, I'm trying to reduce CPU usage and I've read that switch statements are much faster than if-else statements, so I'm trying to consolidate the rerouting engine into a switch statement. My problem is, in some of my if clauses, I have something like

Code: Select all

elseif($area = get_is_area($explode[2])) { $resourceID = $area;}
Is there a way to put that function in a case for a switch($explode[2]) ?
Alkis
Forum Commoner
Posts: 31
Joined: Fri Mar 26, 2010 8:41 am

Re: Switch function with function in case statement?

Post by Alkis »

Which is the value you are checking against other conditions? Is the $explode[2] expression, or the $area variable?
jraede
Forum Contributor
Posts: 254
Joined: Tue Feb 16, 2010 5:39 pm

Re: Switch function with function in case statement?

Post by jraede »

It's $explode[2];
Alkis
Forum Commoner
Posts: 31
Joined: Fri Mar 26, 2010 8:41 am

Re: Switch function with function in case statement?

Post by Alkis »

yes you can do it.

Code: Select all

switch($explode[2]){

    case "something":
      //do stuff...
    break;

    case 5:
      //do stuff...
    break;


}
Last edited by Benjamin on Sat Mar 27, 2010 11:49 pm, edited 1 time in total.
Reason: Added [syntax=php] tags.
jraede
Forum Contributor
Posts: 254
Joined: Tue Feb 16, 2010 5:39 pm

Re: Switch function with function in case statement?

Post by jraede »

That's not a function... I was asking for something like "case $area = get_is_area(explode[2])"
Alkis
Forum Commoner
Posts: 31
Joined: Fri Mar 26, 2010 8:41 am

Re: Switch function with function in case statement?

Post by Alkis »

Ok,
well the expression:
case $area = get_is_area(explode[2])

will either evaluate to true, or false. No other possible value. Unless you mean to check equality? -> case $area == get_is_area($explode[2])

Also write me a few lines on how you would use the switch block. Code.
jraede
Forum Contributor
Posts: 254
Joined: Tue Feb 16, 2010 5:39 pm

Re: Switch function with function in case statement?

Post by jraede »

Yeah, I want it to evaluate to either true or false. If it's true, it'll return the database resource ID for that particular area, and I can do the URL rerouting. If not, then it's not an area, and I can continue.

The switch statement looks like this:

Code: Select all

switch($explode[2]) {
     case 'XXX':
          // do something
          break;
     case $area = get_is_area($explode[2]):
          // will this work?
          break;

}
I mean, I guess I could try this and see if it runs. Just wanted to see if it's feasible, or if there's a better way, before I did.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Switch function with function in case statement?

Post by AbraCadaver »

Gets kind of ugly using a switch like this, but it depends upon your other conditions. Here's a sample:

Code: Select all

switch(true) {
    case ($area = get_is_area($explode[2])):
        //do something
        break;

    case ($explode[2] == 'XXX'):
        //do something else
        break;
}
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Post Reply