duanguane1670 2018-06-03 01:46
浏览 56

多个if else语句php

I have inputs $a,$b,$c,$d and $e and outputs $f and $g.

Based on the combinations of values of the inputs the values of outputs will vary. There are around 400 possible combinations.

Is there a better way to write this rather than writing 400 if else statements?

I'm writing in php

if ($a == 'aaa' && $b == 'baaa' && $c == 'abc' && $d == 'zzz' && $e = 'eee') 
{
    $f = 'Positive';
    $g = 'Negative';
} 
else if ..... // Have 400 such scenarios. 
  • 写回答

1条回答 默认 最新

  • douhuan6157 2018-06-03 02:11
    关注

    In my knowledge there is no other option than if else. But, I suggest an algorithmic approach: You can concatenate all the items $a $b ... in just one variable $concat, and then you test your condition on this last one with a : Switch ($concat){case: ...} Maybe you can minimize the number of cases : Example: if $a == 'abbc' then $f='positive' for any value $b $c ...etc You can also create a matrix(400,2) (2 columns and 400 rows) first column is the value of $concat the second one is the value tu put in $f. This matrix can be filled in a loop and used as below : $f=matrix[(string)$concat] The matrix can be filled from a text file like an excel sheet ;)

    评论

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?