Control Structure

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Control Structure

Post by Benjamin »

What is the method for creating a control structure without using an else if, that would work as the following would work if switches supported expressions on a per case basis.

Code: Select all

switch (null)
{
    case $a = $x:
        echo 1;
    break;
    case $c = $x:
        echo 2;
    break;
}
GM
Forum Contributor
Posts: 365
Joined: Wed Apr 26, 2006 4:19 am
Location: Italy

Post by GM »

Isn't your example the equivalent of:

Code: Select all

switch($x) {
    case $a:
        echo 1;
    break;
    case $b:
        echo 2;
    break;
}
??
User avatar
daedalus__
DevNet Resident
Posts: 1925
Joined: Thu Feb 09, 2006 4:52 pm

Post by daedalus__ »

I think he was asking about something to that effect.
GM
Forum Contributor
Posts: 365
Joined: Wed Apr 26, 2006 4:19 am
Location: Italy

Post by GM »

Did I take the example too literally then? :?

In that case, I saw this example in the user notes of the manual:

Code: Select all

switch (TRUE){
    case ($v_BAL <= 0): //less then 0 , -0 
         echo $v_BAL;
    break;

    case ($v_BAL <= 10 AND $v_BAL >= 1): //less then 10 and greater then 1
         echo $v_BAL;
    break;

    default: //default
        echo $v_BAL;
    break;
}
Which seems pretty useless, as all three switches do the same thing, but it illustrates the point. Probably.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

No, there is a third value, $c. I need something similar to a switch but that allows expressions.

Like,

Code: Select all

if $x = '15';
  // do something
break;
if $y = 'yellow'
  // do something else
break;
// etc..
GM
Forum Contributor
Posts: 365
Joined: Wed Apr 26, 2006 4:19 am
Location: Italy

Post by GM »

I edited my post above - does the switch(true) syntax do what you need?
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

No you can't use a comparison operator in a case. :(

Hmm maybe it does with (), I'll try that..

Yeah that works.. Awesome!

Thank you!
User avatar
psurrena
Forum Contributor
Posts: 355
Joined: Thu Nov 10, 2005 12:31 pm
Location: Broolyn, NY

Post by psurrena »

Why would you not use an if / then? What's the advantage of switch?
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

Just cleaner code in my opinion. I always prefer to use switches when possible. They are generally more efficient than elseif as well.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

Not sure if this is an answer to your question, but the OO pattern for implementing a switch is Chain of Responsibility. And warning -- there are two flavors of that pattern.
(#10850)
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

Well it's in a method inside of a class already, if that matters at all...
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

Code: Select all

switch (true) // can also use false
{
    case ($a == $b):
        echo $a;
        break;

    case ($a != $b):
        echo $b;
        break;
}
Post Reply