3 завдання масив - PHP Online
Form of PHP Sandbox
Enter Your PHP code here for testing/debugging in the Online PHP Sandbox. As in the usual PHP files, you can also add HTML, but do not forget to add the tag <?php
in the places where the PHP script should be executed.
Result of php executing
Full code of 3 завдання масив.php
- <?php
- function countPositivesSumNegatives($array) {
- if ($array == null || empty($array)) {
- return [];
- }
- $positiveCount = 0;
- $negativeSum = 0;
- foreach ($array as $num) {
- if ($num > 0) {
- $positiveCount++;
- } elseif ($num < 0) {
- $negativeSum += $num;
- }
- }
- return [$positiveCount, $negativeSum];
- }
- // Приклад використання функції
- $input = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, -11, -12, -13, -14, -15];
- $result = countPositivesSumNegatives($input);
- echo "Result: " . implode(", ", $result) . "\n";
- ?>