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
Benjamin
Site Administrator
Posts: 6935 Joined: Sun May 19, 2002 10:24 pm
Post
by Benjamin » Tue Sep 26, 2006 12:28 am
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 » Tue Sep 26, 2006 2:48 am
Isn't your example the equivalent of:
Code: Select all
switch($x) {
case $a:
echo 1;
break;
case $b:
echo 2;
break;
}
??
daedalus__
DevNet Resident
Posts: 1925 Joined: Thu Feb 09, 2006 4:52 pm
Post
by daedalus__ » Tue Sep 26, 2006 2:50 am
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 » Tue Sep 26, 2006 2:58 am
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.
Benjamin
Site Administrator
Posts: 6935 Joined: Sun May 19, 2002 10:24 pm
Post
by Benjamin » Tue Sep 26, 2006 3:01 am
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 » Tue Sep 26, 2006 3:03 am
I edited my post above - does the switch(true) syntax do what you need?
Benjamin
Site Administrator
Posts: 6935 Joined: Sun May 19, 2002 10:24 pm
Post
by Benjamin » Tue Sep 26, 2006 3:18 am
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!
psurrena
Forum Contributor
Posts: 355 Joined: Thu Nov 10, 2005 12:31 pm
Location: Broolyn, NY
Post
by psurrena » Tue Sep 26, 2006 8:20 am
Why would you not use an if / then? What's the advantage of switch?
Benjamin
Site Administrator
Posts: 6935 Joined: Sun May 19, 2002 10:24 pm
Post
by Benjamin » Tue Sep 26, 2006 3:48 pm
Just cleaner code in my opinion. I always prefer to use switches when possible. They are generally more efficient than elseif as well.
Christopher
Site Administrator
Posts: 13596 Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US
Post
by Christopher » Wed Sep 27, 2006 1:50 am
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)
Benjamin
Site Administrator
Posts: 6935 Joined: Sun May 19, 2002 10:24 pm
Post
by Benjamin » Wed Sep 27, 2006 3:35 am
Well it's in a method inside of a class already, if that matters at all...
Jenk
DevNet Master
Posts: 3587 Joined: Mon Sep 19, 2005 6:24 am
Location: London
Post
by Jenk » Wed Sep 27, 2006 5:49 am
Code: Select all
switch (true) // can also use false
{
case ($a == $b):
echo $a;
break;
case ($a != $b):
echo $b;
break;
}