1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > php优化if语句 php - 如何在php中优化64 if else if语句 - SO中文参考 - www.soinside.com

php优化if语句 php - 如何在php中优化64 if else if语句 - SO中文参考 - www.soinside.com

时间:2024-03-19 16:19:32

相关推荐

php优化if语句 php - 如何在php中优化64 if else if语句 - SO中文参考 - www.soinside.com

让我们以符合逻辑的方式在数学上解决它。

我们可以看到,一个不同的恒河猴因子预示着一个男孩,一个男孩是平等的。血型方案类似。如果两种类型都是偶数或奇数,则预测女孩,如果偶数/奇数不同,则预测男孩。有一个例外,当至少有一个血型是III或IV时,那么血型通常会预测男孩。

与通常的信息学一样,我们对血型I-IV使用零基础的0-3枚举。这样我们使用二进制数00,01,10,11,即2位。如果设置了较低位,则id号是奇数(即,甚至类型I-IV)。如果设置了第二位,我们有III或IV类型之一。

第三位(值4)我们将用来表示“恒河猴因子+”。

我们可以生成标签,保留最后选择的选项,如:

function blood_options($sel)

{

foreach(['O(I)', 'A(II)', 'B(III)', 'AB(IV]'] as $k => $v)

{

?>

><?php echo $v, ' Rh-'?>;

><?php echo $v, ' Rh+'?>;

}

}

一个按位XOR运算,PHP中的^运算符,表示两个操作数的每个位的差异。我们可以用它来检查第3位(值4,我们的Rhesus因子位)和位0(值1,偶数/奇数)。

$m = intval($_POST['groupM']); // get the numeric value

$f = intval($_POST['groupF']); // get the numeric value

$xor = $m ^ $f; // indicate bitwise differences

现在我们可以看出,血型和恒河猴因子预测了哪些性别:

$type1 = 1 & $xor ; // last bit of (m xor f) => even/odd group different

$type2 = 1 & (($m | $f)>>1); // at least one of the second bits set => there is a group 2 (III) or 3 (IV)

$type = $type1 | $type2 ; // => 1 if at least one of type1 or type2 indicates maskulinum

$rhf = 1&($xor>>2) ; // different rhesus? => 1 indicates maskulinum

$result = $type + $rhf ; // sum results in 0: 2x femininum ; 1: 1x m + 1x f ; 2: 2x maskulinum

var_dump($type1, $type2, $type,

在上面的代码中,我们还使用SHIFT LEFT运算符>>将有效位移动到位置0(值1),并通过AND运算1 &屏蔽掉所有其他位。

在将总和计算为$result后,我们得到了

0 = 2个预测:女孩

1 =不同的预测

2 = 2个预测:男孩

Putting it all together and written in short expressions, it finally looks like that:

if(isset($_POST['groupM'], $_POST['groupF']))

{

$m = intval($_POST['groupM']);

$f = intval($_POST['groupF']);

$xor = $m ^ $f;

echo

[

"It's a girl",

"Baby's gender can not be predicted — Rh factor and blood type produce different results.",

"It's a boy"

][(1 & ((($m | $f)>>1) | $xor)) + (1&($xor>>2))];

}

The HTML generation:

function blood_options($sel)

{

foreach(['O(I)', 'A(II)', 'B(III)', 'AB(IV]'] as $k => $v)

{

?>

><?php echo $v, ' Rh-'?>;

><?php echo $v, ' Rh+'?>;

}

}

?>

Father's blood group:

Mother's blood group:

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。