Case Statement Question

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View


I have a question with regards to a case statement.. Currently I have
the following if statement....

if ($name->temp['temp_name'] != Null)
   function here.....
else if ($name->temp['temp_name1'] != Null)
   function here.....

What I'm trying to do is this:


   case: $name->temp['temp_name']:
     function here
   case: $name->temp['temp_name1']:
     function here

So far the case statement is not working.. Is there an easy way to
remove the if statement by using a case statement of a different


Re: Case Statement Question

Kevin Davis wrote:
Quoted text here. Click to load it

Not really.  You're checking $name->temp against
$name->temp['temp_name'] or $name->temp['temp_name1'].  Whichever one
matches first will be processed.

switch{} statements are meant to check a single value against various
conditions; you're trying to check different values.

There are other ways you could do it, but the if statement is the
clearest, IMHO.

Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.

Re: Case Statement Question


Quoted text here. Click to load it

You got a problem with yer colon! ;)

# php -r 'switch(1){case: 1: echo "hello\n";}'

Parse error: syntax error, unexpected ':' in Command line code on line
# php -r 'switch(1){case 1: echo "hello\n";}'

Of course when you saw the parse error, you rushed to the manual to
check the switch statement syntax, right?

Re: Case Statement Question

Quoted text here. Click to load it

I expect you're looking for something like:

$aTemp = $name->temp;
switch (false) {
  case (is_null($aTemp['temp_name'])):
    function here;
  case (is_null($aTemp['temp_name1'])):
    other function here;
    break;   }

The above will match the first false condition.
If it is the same function that is being run, then
you would modify the expressions above to combine
the case statements in some fashion.

Csaba Gabor from Vienna

Site Timeline