如何将一个整数值分成多个整数值

时间:2016-10-08 11:41:44

标签: php arrays integer

我需要将整数值分成2个或更多整数值。

示例输入:$total_marks = 42

输出:

$sub1 = 20;
$sub2 = 22;

条件:$total_marks = $sub1 + $sub2;

感谢

1 个答案:

答案 0 :(得分:0)

试试这个,它适用于偶数和奇数标记。

$total_marks = 42;
if(($total_marks%2) == 0)
{
    $num1 = $total_marks/2;
    $num2 = $num1;
    echo $num1 ." AND ".$num2." sum is ".$total_marks ;
}else
{
    $num1 = $total_marks/2; 
    $num1 = floor($num1);
    $num2 = $num1  + 1;

    echo $num1 ." AND ".$num2." sum is ".$total_marks ;
}