所以我有这样的命令处理:
$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;
}
}
这基本上会检查命令。
$command =在斜杠("/")后面输入的单词。
case '':
基本检查斜杠后面是否没有命令。
问题:我希望系统也检查命令是否存在。
例如:用户写道:/你好
但是这个命令不存在,因为我们只有case 'ban', case 'prune', case 'test'和case "。
没有case 'hello',所以会抛出错误。有这样的函数吗?我该怎么做呢?
我相信你要找的是一个default:
案例。
例子:
<?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";
}
?>
编辑:修复了被聊天的问题:http://privatepaste.com/bd34e7e63b
使用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.";
}