0

So I have this commands handling:

$message = the entered message.

    public function handleCommands($message, $username)
    {
        //Variables we're going to use
        $space = strpos($message, ' '); # The first space.
        $command = trim(substr($message, 1, $space)); # The command after the slash
        $name = substr($message, $space + 1); # The name after the command.


        switch ($command)
        {
            case 'ban':
                $this->ban($name, $username);
            break;

            case 'prune':
                $this->prune($username);
            break;

            case '':
                echo 'Please use a command!';
            break;

            case 'test':
                try
                {
                    $this->test($name);
                }
                catch (exception $r)
                {
                    echo $r->getMessage();
                }
            break;
        }
    }

This basically will check for the command.

$command = the entered word after the slash ( " / " ).

Can you see

case '':

This basically checks if there is no command after the slash.

Question: I want the system to check aswell, if the command exists in the cases.

For example:

user wrote:

/hello

But that command doesn't exists, considering we only have case 'ban', case 'prune', case 'test' and case ''.

there is no case 'hello', so it will throw an error. Is there a function that does this sort of thing? How can I do this?

4

2 回答 2

2

I believe what you're looking for is a default: case.

Example:

<?php
switch ($i) {
    case 0:
        echo "i equals 0";
        break;
    case 1:
        echo "i equals 1";
        break;
    case 2:
        echo "i equals 2";
        break;
    default:
       echo "i is not equal to 0, 1 or 2";
}
?>

EDIT: Fixed version of problem that was chatted about: http://privatepaste.com/bd34e7e63b

于 2013-06-20T19:14:55.867 回答
1

Use the case default:

    switch ($command)
    {
        case 'ban':
            $this->ban($name, $username);
        break;

        case 'prune':
            $this->prune($username);
        break;

        case '':
            echo 'Please use a command!';
        break;

        case 'test':
            try
            {
                $this->test($name);
            }
            catch (exception $r)
            {
                echo $r->getMessage();
            }
        break;

        default:
            echo "That command does not exist.";
    }
于 2013-06-20T19:14:11.417 回答