Question

I need to divide an integer value into 3 parts using php

for eg: I have a value 9 the answer is like 3 + 3 + 3 if i have value 10 the ans like 3 + 3 + 4 or something like that

Was it helpful?

Solution

You can use a modulus function.

<?php
    $yourInt=10;
    $remainder=$yourInt % 3;
    $third=floor($yourInt/3);
    $lastBit=$third+$remainder;

    echo "The numbers are $third + $third + $lastBit.";
?>

Output:

The numbers are 3 + 3 + 4.

OTHER TIPS

If you have a value that is >= 3 you can do the following:

$number = 10;
$number1and2 = floor($number/3);
$number3 = $number - (2*$number1and2);
$num = 11; 

$d = [$num/3,$num/3,$num/3];

$round = array_map('round', $d);    
list($first, $second, $third) = $round;
$round = (is_float($num/3)) ? $num-(round($num/3)*3) : 0;

echo $first.' '.$second.' '.($third += (is_float($num/3)) ? $num-(round($num/3)*3) : 0); 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top