Question:
Suppose you have an exercise in the following aspect, I need to take from an array with random integers, the largest number that is between the ranges.
For example, I have an array: array(2, 8, 4);

would have to order: 2,4,8

check the range from 2 to 4 = 2, and from 4 to 8 = 4

the largest interval, in this case, would be 4 , between 4 and 8.
Starting from this idea, I have the following code structure:
<?php
class VerifyNumbers
{
private $arr = array();
public function __construct(array $arr)
{
$this>arr = $arr;
}
public function verifyMaxIntersectionFromArray()
{
$collection = array();
sort($this>arr);
for ($i= 0; $i < count($this>arr); $i++) {
if (isset($this>arr[$i + 1])) {
$collection[] = ($this>arr[$i + 1]  $this>arr[$i]);
}
}
return max($collection);
}
}
$verifyNumber = new VerifyNumbers(array(100, 3000, 4000, 25, 540, 20, 200, 300));
$maxIntersection = $verifyNumber>verifyMaxIntersectionFromArray();
echo $maxIntersection;
The question I ask is, and if in this case, I had input values in this array that exceeded the 4 billion place, or even greater, because there could be negative numbers, which would double, the range, how could I convert these numbers input in a smaller, readable string, since in PHP 5, an invalid digit is passed to an octal integer (eg 8 or 9), the rest of the number will be ignored.
Answer:
You must format the number. Let's imagine that $num
is the variable of the number I want to format:
echo "O seu número formatado é: ". number_format($num, 4);
What's in front of the $num
is how many places this number is going to have. So, in that case, instead of being for example 1.000000 it's going to be 1.0000!