How does using modulo operation compare to regular expressions for number filtering in PHP in terms of performance and accuracy?
When filtering numbers in PHP, using the modulo operation can be more efficient and accurate than regular expressions. Modulo operation allows you to easily check if a number is divisible by another number, making it ideal for filtering based on specific criteria. Regular expressions, on the other hand, can be more complex and slower for simple number filtering tasks.
// Using modulo operation for number filtering
$numbers = [10, 15, 20, 25, 30];
$filteredNumbers = array_filter($numbers, function($num) {
return $num % 5 == 0; // Filter numbers that are divisible by 5
});
print_r($filteredNumbers);