edabit php problem solving
1.Return the Sum of Two Numbers
Create a function that takes two numbers as arguments and returns their sum.
Output:
addition(3,2) 5
addition(-3,-6) -9
addition(7,3) 10
function addition($a, $b) {
return $a+$b;
}
addition(3,2)
2.Return the Next Number from the Integer Passed
Create a function that takes a number as an argument, increments the number by +1 and returns the result.
output:
addition(0) ➞ 1
addition(9) ➞ 10
addition(-3) ➞ -2
i have to pass an preincrement operator because of -3 value
<?php
function addition($num) {
return ++$num;
}
?>
3.Return the First Element in an Array
Create a function that takes an array containing only numbers and return the first element.
output:
getFirstValue([1, 2, 3]) ➞ 1
getFirstValue([80, 5, 100]) ➞ 80
getFirstValue([-500, 0, 50]) ➞ -500
<?php
function getFirstValue($array) {
return array_values($array)[0];
//or
//return $array[0];
// return array_shift($array);
/*
Here one of the solutions was using array shift which removes the first element
from an array and returns it to either variable or a function ,but the original array
remove that value ,it's pass by reference so array wil be modified.
/*
}
?>