Jump to content

Archived

This topic is now archived and is closed to further replies.

atitthaker

Switch control structure Problem

Recommended Posts

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???

Share this post


Link to post
Share on other sites
I have absolutely no idea what you mean by that.  Could you rephrase that a bit more clear?

Share this post


Link to post
Share on other sites
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....

Share this post


Link to post
Share on other sites
You can use an if/else or if/elseif/else state in side a case clause:
[code=php:0]<?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;
}

?>[/code]

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.