Jump to content


Photo

Switch control structure Problem


  • Please log in to reply
6 replies to this topic

#1 atitthaker

atitthaker
  • Members
  • PipPipPip
  • Advanced Member
  • 52 posts
  • LocationBanglore, India

Posted 09 August 2006 - 06:58 AM

Can I compare switch expression with case expression by === instead of default ==. I meant can I compare it with strict type checking instead of simple value checking. I know to write the same in case but I dont know whether any such method is available with PHP built in to it or not???
Beginner with PHP and need help

#2 nethnet

nethnet
  • Members
  • PipPipPip
  • Advanced Member
  • 284 posts
  • LocationPhiladelphia, PA, USA

Posted 09 August 2006 - 07:30 AM

I have absolutely no idea what you mean by that.  Could you rephrase that a bit more clear?
"Creativity is allowing yourself to make mistakes. Art is knowing which ones to keep."  --Scott Adams

Current project: nethnet.com

#3 atitthaker

atitthaker
  • Members
  • PipPipPip
  • Advanced Member
  • 52 posts
  • LocationBanglore, India

Posted 09 August 2006 - 08:26 AM

I meant that switch...case compares by values only, not by type. Right!!
What if I want it to check type also in addition to values....
Beginner with PHP and need help

#4 king arthur

king arthur
  • Members
  • PipPipPip
  • Advanced Member
  • 335 posts
  • LocationUK HQ

Posted 09 August 2006 - 09:58 AM

You'd be better off using an IF....ELSEIF construct.
Sir Isaac Newton said "If I have seen farther, it is by standing on the shoulders of giants". But it is not recorded as to whether he said it before or after he was hit on the head by a falling apple.

#5 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 09 August 2006 - 10:09 AM

You can use an if/else or if/elseif/else state in side a case clause:
<?php

$str = TRUE;
$str2 = 'TRUE';

switch($str)
{
    case TRUE:
        // now check the actual type of $str
        if($str === $str2)
        {
            echo 'str is a string';
        }
        else
        {
            echo 'str is not a string';
        }
    break;
}

?>


#6 atitthaker

atitthaker
  • Members
  • PipPipPip
  • Advanced Member
  • 52 posts
  • LocationBanglore, India

Posted 09 August 2006 - 11:30 AM

So PHP do not have built in support for comparing it that way...
Beginner with PHP and need help

#7 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 09 August 2006 - 12:31 PM

Not with a switch no.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users