根据两个变量返回一个值

Kea*_*ire 0 php variables

我想根据两个变量的内容返回一个值.让我解释一下,我有三个变量:

$var1 = 2; // This variable can change from 1 to 10 (based on the value in the database)
$var2 = 1; // This variable can have two values: 0 and 1 (based on the value in the database)

$result = ''; // This variable have a value based on the content of the two variables
Run Code Online (Sandbox Code Playgroud)

我从数据库的两个不同列($ var1和$ var2)获取两个值,我有$ result,一个变量,其值基于$ var1和$ var2的内容

这可以轻松完成,制定20个条件,如下所示:

if ($var1 == 1 && $var == 0) $result = 'good morning';
if ($var1 == 1 && $var == 1) $result = 'hello';
if ($var1 == 2 && $var == 0) $result = 'never';
Run Code Online (Sandbox Code Playgroud)

但我的问题是:有一种方法可以更容易地编写这段代码,易于阅读和更易于管理?

PS:$ result具有完全不同的值,在这种情况下不需要连接.

谢谢.

Nie*_*sol 6

我认为这可能是一个更易于管理的代码版本:

switch([$var1,$var2]) {
    case [0,0]:
        // code
        break;
    case [0,1]:
        // code
        break;
    // ...
    default:
        // pairing wasn't defined
}
Run Code Online (Sandbox Code Playgroud)

如果不知道的确切性质$var1$var2,也没有意义$result,我真的不能帮助更多的,但这应该是一个良好的开端.