RE: [PHP] Another problem with conditional statements

2002-12-20 Thread Ford, Mike [LSS]
> -Original Message-
> From: Beauford.2002 [mailto:[EMAIL PROTECTED]]
> Sent: 20 December 2002 03:15
> 
> 
> Using switch would be more efficiant as it would stop once a 
> match is made
> (if you use break), but with eleif statements each one is evaluated in
> order.

Not sure that's true -- a sequence of elseifs will also stop evaluating once one of 
the conditions has evaluated to true.  The essential difference is that a switch/case 
structure is a repeated test against a single value (possibly derived from an 
expression), whereas an if ... elseif sequence can test multiple completely disjoint 
conditions.

> $chr = substr($a,$i,1);
> switch (TRUE) {
> 
> case $chr == "á" || $chr == "à" || $chr == "ã" || $chr == "â":
> $a = str_replace(substr($a,$i,1),"a",$a);
> break;
> 
> case $chr == "é" || $chr == "è" || $chr == "ê":
> $a = str_replace(substr($a,$i,1),"e",$a);
> break;
> 
> }

Actually, I'm not sure that's a good example, either!  I'd have thought it was better 
written as:

  switch (substr($a,$i,1)) {

  case "á":
  case "à":
  case "ã":
  case "â":
$a = str_replace(substr($a,$i,1),"a",$a);
break;

  case "é":
  case "è":
  case "ê":
$a = str_replace(substr($a,$i,1),"e",$a);
break;
 
  }

Cheers!

Mike

-
Mike Ford,  Electronic Information Services Adviser,
Learning Support Services, Learning & Information Services,
JG125, James Graham Building, Leeds Metropolitan University,
Beckett Park, LEEDS,  LS6 3QS,  United Kingdom
Email: [EMAIL PROTECTED]
Tel: +44 113 283 2600 extn 4730  Fax:  +44 113 283 3211 

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




RE: [PHP] Another problem with conditional statements

2002-12-20 Thread Ford, Mike [LSS]
> -Original Message-
> From: Sean Malloy [mailto:[EMAIL PROTECTED]]
> Sent: 20 December 2002 02:36
> To: PHP General
> Subject: RE: [PHP] Another problem with conditional statements
> 
> 
> Nowhere in the documentation does it specify switch should be 
> used in the
> context you are attempting.

The docs say:

 "In many occasions, you may want to compare the same variable
  (or expression) with many different values, and execute a
  different piece of code depending on which value it equals
  to. This is exactly what the switch statement is for."

TRUE is an expression, and so can be the subject of a switch statement. 

> The docs show a single variable and checking the case of that 
> variable.

Admittedly all the examples in the manual use a single variable, but from the first 
sentence of the switch description:

 "The switch statement is similar to a series of IF statements
  on the same expression."

... I think it's reasonable to deduce that the parentheses after switch can contain 
any legitimate expression.

Admittedly, all the examples in the manual use a single variable -- perhaps you might 
want to submit a Documentation Problem bug report suggesting some more complex 
examples.  Actually, looking at the online manual again, I see several user notes with 
potential candidates for moving to the manual proper, including one which demonstrates 
the exact usage that the OP was having problems with.

Cheers!

Mike

-
Mike Ford,  Electronic Information Services Adviser,
Learning Support Services, Learning & Information Services,
JG125, James Graham Building, Leeds Metropolitan University,
Beckett Park, LEEDS,  LS6 3QS,  United Kingdom
Email: [EMAIL PROTECTED]
Tel: +44 113 283 2600 extn 4730  Fax:  +44 113 283 3211 

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




RE: [PHP] Another problem with conditional statements

2002-12-20 Thread Ford, Mike [LSS]
> -Original Message-
> From: Rick Emery [mailto:[EMAIL PROTECTED]]
> Sent: 20 December 2002 00:34
> 
> switch() does not work that way.  Switch uses the value in 
> the parentheses and selects a
> CASE based upon that value.  Read the manual.
> 
> You will have to use a series of if()-elseif()-else()

Not true.  Absolutely nothing wrong with using switch in this way -- it's a
very neat and useful device.

Cheers!

Mike

-
Mike Ford,  Electronic Information Services Adviser,
Learning Support Services, Learning & Information Services,
JG125, James Graham Building, Leeds Metropolitan University,
Beckett Park, LEEDS,  LS6 3QS,  United Kingdom
Email: [EMAIL PROTECTED]
Tel: +44 113 283 2600 extn 4730  Fax:  +44 113 283 3211 

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




RE: [PHP] Another problem with conditional statements

2002-12-20 Thread Ford, Mike [LSS]
> -Original Message-
> From: Beauford.2002 [mailto:[EMAIL PROTECTED]]
> Sent: 20 December 2002 00:19
> 
> This should be as simple as breathing, but not today. I have 
> two variables
> $a and $b which I need to compare in a switch statement in 
> several different
> ways, but no matter what I do it's wrong.
> 
> This is what I have tried, can someone tell me how it should be.
> 
> TIA
> 
> switch (true):
> 
> case ($a == $b): This one seems simple enough.
> do sum stuff;
> break;
> 
> case ($a && $b == 124):   This appears not to work.
> do sum stuff;
> break;
> 
> case ($a == 124 && $b == 755):  If $a is equal to 124 and 
> $b is equal to
> 755 then it should be true..doesn't work.
> do sum stuff;
> break;
> 
> case ($a == 124 && $b != 124):   Nope, this doesn't appear to work
> either.
> do sum stuff;
> break;
> 
> endswitch;

Well, I've just cut-and-pasted this code and added a few echo statements
(and some other twiddles to get suitable values in), and it works perfectly
-- so I'd say your $a and $b aren't getting the values you think they are.
As a firtst step, I suggest you echo them out immediately before the switch
statement and see what you've got.

Cheers!

Mike

-
Mike Ford,  Electronic Information Services Adviser,
Learning Support Services, Learning & Information Services,
JG125, James Graham Building, Leeds Metropolitan University,
Beckett Park, LEEDS,  LS6 3QS,  United Kingdom
Email: [EMAIL PROTECTED]
Tel: +44 113 283 2600 extn 4730  Fax:  +44 113 283 3211 

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




Re: [PHP] Another problem with conditional statements

2002-12-20 Thread Jason Wong
On Friday 20 December 2002 08:28, Sean Malloy wrote:
> Its all wrong. You shouldn't be using a switch statement anyway. A switch
> is for evaluating a single variable.

You can use the switch construct in the context that the OP was using it. In 
fact I prefer use that instead of a whole bunch of if-then-else statements. 
For one thing it looks (IMO) a lot neater.

-- 
Jason Wong -> Gremlins Associates -> www.gremlins.biz
Open Source Software Systems Integrators
* Web Design & Hosting * Internet & Intranet Applications Development *

/*
Beware of bugs in the above code; I have only proved it correct, not tried it.
-- Donald Knuth
*/


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




Re: [PHP] Another problem with conditional statements

2002-12-19 Thread Jason Wong
On Friday 20 December 2002 08:19, Beauford.2002 wrote:
> Hi,
>
> This should be as simple as breathing, but not today. I have two variables
> $a and $b which I need to compare in a switch statement in several
> different ways, but no matter what I do it's wrong.
>
> This is what I have tried, can someone tell me how it should be.
>
> TIA
>
> switch (true):
>
> case ($a == $b): This one seems simple enough.
> do sum stuff;
> break;
>
> case ($a && $b == 124):   This appears not to work.
> do sum stuff;
> break;
>
> case ($a == 124 && $b == 755):  If $a is equal to 124 and $b is equal
> to 755 then it should be true..doesn't work.
> do sum stuff;
> break;
>
> case ($a == 124 && $b != 124):   Nope, this doesn't appear to work
> either.
> do sum stuff;
> break;
>
> endswitch;

AFAICS all of them should work. The 2nd case may not work the way you 
intended. It is testing for, if $a is TRUE, AND if $b equals 124.

Where are you getting $a and $b from?

-- 
Jason Wong -> Gremlins Associates -> www.gremlins.biz
Open Source Software Systems Integrators
* Web Design & Hosting * Internet & Intranet Applications Development *

/*
You'll feel much better once you've given up hope.
*/


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




Re: [PHP] Another problem with conditional statements

2002-12-19 Thread Beauford.2002
>From the manual at www.php.net.  This is a lot more sophiticated than mine
and apparantly works. So as a recent user of PHP, I'm at a loss as to what
is and what isn't.

Using switch would be more efficiant as it would stop once a match is made
(if you use break), but with eleif statements each one is evaluated in
order.

$chr = substr($a,$i,1);
switch (TRUE) {

case $chr == "á" || $chr == "à" || $chr == "ã" || $chr == "â":
$a = str_replace(substr($a,$i,1),"a",$a);
break;

case $chr == "é" || $chr == "è" || $chr == "ê":
$a = str_replace(substr($a,$i,1),"e",$a);
break;

}

- Original Message -
From: "Sean Malloy" <[EMAIL PROTECTED]>
To: "PHP General" <[EMAIL PROTECTED]>
Sent: Thursday, December 19, 2002 9:36 PM
Subject: RE: [PHP] Another problem with conditional statements


> Nowhere in the documentation does it specify switch should be used in the
> context you are attempting.
>
> The docs show a single variable and checking the case of that variable.
>
> I'm not going to berate you on syntax. If you can get it working like that
> then good for you. However, I would strongly advise you to use the
> if/elseif/else statements instead.
>
> an example of switch
>
> $action = $_POST['action']
> switch ($action)
> {
> case 'help': showHelp(); break;
> default : showDefault();
> }
>
> not
>
> $action = $_POST['action']
> switch (true)
> {
> case ($action == 'help'): showHelp(); break;
>     default : showDefault();
> }
>
>
> > -Original Message-
> > From: Beauford.2002 [mailto:[EMAIL PROTECTED]]
> > Sent: Friday, 20 December 2002 12:46 PM
> > To: Rick Emery
> > Cc: PHP General
> > Subject: Re: [PHP] Another problem with conditional statements
> >
> >
> > I believe you are incorrect. Switch will look for the first case
statement
> > that is true and execute that statement. The following works - case one
is
> > incorrect so it doesn't get executed but the second case does. Paste
this
> > into a test.php file and you will see it works.. Read the manual.
> >
> > $a=2;
> > $b=4;
> >
> > switch (true) {
> > case ($a < $b) and ($b > 5):
> > echo "Incorrect";
> > case ($a == 2):
> >     echo "Correct";
> >  }
> >
> > So my original question is still stands. The switch statement is
correct,
> > but there is a problem with my conditional statements.
> >
> > - Original Message -
> > From: "Rick Emery" <[EMAIL PROTECTED]>
> > To: "Beauford.2002" <[EMAIL PROTECTED]>; "PHP General"
> > <[EMAIL PROTECTED]>
> > Sent: Thursday, December 19, 2002 7:33 PM
> > Subject: Re: [PHP] Another problem with conditional statements
> >
> >
> > > switch() does not work that way.  Switch uses the value in the
> > parentheses
> > and selects a
> > > CASE based upon that value.  Read the manual.
> > >
> > > You will have to use a series of if()-elseif()-else()
> > > - Original Message -
> > > From: "Beauford.2002" <[EMAIL PROTECTED]>
> > > To: "PHP General" <[EMAIL PROTECTED]>
> > > Sent: Thursday, December 19, 2002 6:19 PM
> > > Subject: [PHP] Another problem with conditional statements
> > >
> > >
> > > Hi,
> > >
> > > This should be as simple as breathing, but not today. I have
> > two variables
> > > $a and $b which I need to compare in a switch statement in several
> > different
> > > ways, but no matter what I do it's wrong.
> > >
> > > This is what I have tried, can someone tell me how it should be.
> > >
> > > TIA
> > >
> > > switch (true):
> > >
> > > case ($a == $b): This one seems simple enough.
> > > do sum stuff;
> > > break;
> > >
> > > case ($a && $b == 124):   This appears not to work.
> > > do sum stuff;
> > > break;
> > >
> > > case ($a == 124 && $b == 755):  If $a is equal to 124 and
> > $b is equal
> > to
> > > 755 then it should be true..doesn't work.
> > > do sum stuff;
> > > break;
> > >
> > > case ($a == 124 && $b != 124):   Nope, this doesn't appear to work
> > > either.
> > > do sum stuff;
> > > break;
> > >
> > > endswitch;
> > >
> > >
> > >
> > > --
> > > PHP General Mailing List (http://www.php.net/)
> > > To unsubscribe, visit: http://www.php.net/unsub.php
> > >
> > >
> > >
> > >
> > > --
> > > PHP General Mailing List (http://www.php.net/)
> > > To unsubscribe, visit: http://www.php.net/unsub.php
> > >
> > >
> >
> >
> >
> > --
> > PHP General Mailing List (http://www.php.net/)
> > To unsubscribe, visit: http://www.php.net/unsub.php
> >
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>



-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




RE: [PHP] Another problem with conditional statements

2002-12-19 Thread Sean Malloy
Nowhere in the documentation does it specify switch should be used in the
context you are attempting.

The docs show a single variable and checking the case of that variable.

I'm not going to berate you on syntax. If you can get it working like that
then good for you. However, I would strongly advise you to use the
if/elseif/else statements instead.

an example of switch

$action = $_POST['action']
switch ($action)
{
case 'help': showHelp(); break;
default : showDefault();
}

not

$action = $_POST['action']
switch (true)
{
case ($action == 'help'): showHelp(); break;
default : showDefault();
}


> -Original Message-
> From: Beauford.2002 [mailto:[EMAIL PROTECTED]]
> Sent: Friday, 20 December 2002 12:46 PM
> To: Rick Emery
> Cc: PHP General
> Subject: Re: [PHP] Another problem with conditional statements
>
>
> I believe you are incorrect. Switch will look for the first case statement
> that is true and execute that statement. The following works - case one is
> incorrect so it doesn't get executed but the second case does. Paste this
> into a test.php file and you will see it works.. Read the manual.
>
> $a=2;
> $b=4;
>
> switch (true) {
> case ($a < $b) and ($b > 5):
> echo "Incorrect";
> case ($a == 2):
> echo "Correct";
>  }
>
> So my original question is still stands. The switch statement is correct,
> but there is a problem with my conditional statements.
>
> - Original Message -
> From: "Rick Emery" <[EMAIL PROTECTED]>
> To: "Beauford.2002" <[EMAIL PROTECTED]>; "PHP General"
> <[EMAIL PROTECTED]>
> Sent: Thursday, December 19, 2002 7:33 PM
> Subject: Re: [PHP] Another problem with conditional statements
>
>
> > switch() does not work that way.  Switch uses the value in the
> parentheses
> and selects a
> > CASE based upon that value.  Read the manual.
> >
> > You will have to use a series of if()-elseif()-else()
> > - Original Message -
> > From: "Beauford.2002" <[EMAIL PROTECTED]>
> > To: "PHP General" <[EMAIL PROTECTED]>
> > Sent: Thursday, December 19, 2002 6:19 PM
> > Subject: [PHP] Another problem with conditional statements
> >
> >
> > Hi,
> >
> > This should be as simple as breathing, but not today. I have
> two variables
> > $a and $b which I need to compare in a switch statement in several
> different
> > ways, but no matter what I do it's wrong.
> >
> > This is what I have tried, can someone tell me how it should be.
> >
> > TIA
> >
> > switch (true):
> >
> > case ($a == $b): This one seems simple enough.
> > do sum stuff;
> > break;
> >
> > case ($a && $b == 124):   This appears not to work.
> > do sum stuff;
> > break;
> >
> > case ($a == 124 && $b == 755):  If $a is equal to 124 and
> $b is equal
> to
> > 755 then it should be true..doesn't work.
> > do sum stuff;
> > break;
> >
> > case ($a == 124 && $b != 124):   Nope, this doesn't appear to work
> > either.
> > do sum stuff;
> > break;
> >
> > endswitch;
> >
> >
> >
> > --
> > PHP General Mailing List (http://www.php.net/)
> > To unsubscribe, visit: http://www.php.net/unsub.php
> >
> >
> >
> >
> > --
> > PHP General Mailing List (http://www.php.net/)
> > To unsubscribe, visit: http://www.php.net/unsub.php
> >
> >
>
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




Re: [PHP] Another problem with conditional statements

2002-12-19 Thread Beauford.2002
I believe you are incorrect. Switch will look for the first case statement
that is true and execute that statement. The following works - case one is
incorrect so it doesn't get executed but the second case does. Paste this
into a test.php file and you will see it works.. Read the manual.

$a=2;
$b=4;

switch (true) {
case ($a < $b) and ($b > 5):
echo "Incorrect";
case ($a == 2):
echo "Correct";
 }

So my original question is still stands. The switch statement is correct,
but there is a problem with my conditional statements.

- Original Message -
From: "Rick Emery" <[EMAIL PROTECTED]>
To: "Beauford.2002" <[EMAIL PROTECTED]>; "PHP General"
<[EMAIL PROTECTED]>
Sent: Thursday, December 19, 2002 7:33 PM
Subject: Re: [PHP] Another problem with conditional statements


> switch() does not work that way.  Switch uses the value in the parentheses
and selects a
> CASE based upon that value.  Read the manual.
>
> You will have to use a series of if()-elseif()-else()
> - Original Message -
> From: "Beauford.2002" <[EMAIL PROTECTED]>
> To: "PHP General" <[EMAIL PROTECTED]>
> Sent: Thursday, December 19, 2002 6:19 PM
> Subject: [PHP] Another problem with conditional statements
>
>
> Hi,
>
> This should be as simple as breathing, but not today. I have two variables
> $a and $b which I need to compare in a switch statement in several
different
> ways, but no matter what I do it's wrong.
>
> This is what I have tried, can someone tell me how it should be.
>
> TIA
>
> switch (true):
>
> case ($a == $b): This one seems simple enough.
> do sum stuff;
> break;
>
> case ($a && $b == 124):   This appears not to work.
> do sum stuff;
> break;
>
> case ($a == 124 && $b == 755):  If $a is equal to 124 and $b is equal
to
> 755 then it should be true..doesn't work.
> do sum stuff;
> break;
>
> case ($a == 124 && $b != 124):   Nope, this doesn't appear to work
> either.
> do sum stuff;
> break;
>
> endswitch;
>
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>



-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




Re: [PHP] Another problem with conditional statements

2002-12-19 Thread Rick Emery
switch() does not work that way.  Switch uses the value in the parentheses and selects 
a
CASE based upon that value.  Read the manual.

You will have to use a series of if()-elseif()-else()
- Original Message -
From: "Beauford.2002" <[EMAIL PROTECTED]>
To: "PHP General" <[EMAIL PROTECTED]>
Sent: Thursday, December 19, 2002 6:19 PM
Subject: [PHP] Another problem with conditional statements


Hi,

This should be as simple as breathing, but not today. I have two variables
$a and $b which I need to compare in a switch statement in several different
ways, but no matter what I do it's wrong.

This is what I have tried, can someone tell me how it should be.

TIA

switch (true):

case ($a == $b): This one seems simple enough.
do sum stuff;
break;

case ($a && $b == 124):   This appears not to work.
do sum stuff;
break;

case ($a == 124 && $b == 755):  If $a is equal to 124 and $b is equal to
755 then it should be true..doesn't work.
do sum stuff;
break;

case ($a == 124 && $b != 124):   Nope, this doesn't appear to work
either.
do sum stuff;
break;

endswitch;



--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




RE: [PHP] Another problem with conditional statements

2002-12-19 Thread Sean Malloy
Its all wrong. You shouldn't be using a switch statement anyway. A switch is
for evaluating a single variable.

alss, your code if ($a && $b == 124) is the equivelent of writing if ($a ==
true && $b == 124).


if ($a == $b)
{
// do struff
}
elseif ( ($a == 124) && ($b == 124) )
{
//do stuff
}
elseif ( ($a == 124) && ($b == 755) )
{
//do stuff
}
elseif ( ($a == 124) && ($a != $b) )
{
 // do stuff
}
else {
// do default
}


a switch statement is used in this context;

$a = 1;

switch ($a)
{
 case 1: $blah = $a; break;
 case 2: $blah = 'something else'; break;
 default: $blah = 'nothing';
}

you evaluate a single variable, otherwise, you are stuck with using is/else
statements


> -Original Message-
> From: Beauford.2002 [mailto:[EMAIL PROTECTED]]
> Sent: Friday, 20 December 2002 11:19 AM
> To: PHP General
> Subject: [PHP] Another problem with conditional statements
>
>
> Hi,
>
> This should be as simple as breathing, but not today. I have two variables
> $a and $b which I need to compare in a switch statement in
> several different
> ways, but no matter what I do it's wrong.
>
> This is what I have tried, can someone tell me how it should be.
>
> TIA
>
> switch (true):
>
> case ($a == $b): This one seems simple enough.
> do sum stuff;
> break;
>
> case ($a && $b == 124):   This appears not to work.
> do sum stuff;
> break;
>
> case ($a == 124 && $b == 755):  If $a is equal to 124 and $b
> is equal to
> 755 then it should be true..doesn't work.
> do sum stuff;
> break;
>
> case ($a == 124 && $b != 124):   Nope, this doesn't appear to work
> either.
> do sum stuff;
> break;
>
> endswitch;
>
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php