Exercise 1 The following set of short questions will focus on getting you famili
ID: 3702084 • Letter: E
Question
Exercise 1 The following set of short questions will focus on getting you familiar with how to write PHP functions as well as how to make use of the pre-existing PHP functions. All your PHP functions must be declared in the document body section and each functions name must be as specified below. To demonstrate the functionality of each method, you must make function calls in the document body. Include a heading(hl h6) that indicates which function is being tested before each function demonstration. The use of Global Variables is forbidden! Test all your functions with some relevant input parameter values. A. Function: computefactorial Parameter(s): Number Given the number, find its factorial and return it. If the parameter is not an integer number or if it is a negative number, return false.Explanation / Answer
Answer :
<?php
function findSummation($n = 1){
// check if n is integer or not
if(is_int($n) && $n>0)
{
// return sum of n elements
return $n * ($n + 1)/2;
}
return "false";
}
function uppercaseFirstandLast( $str ){
// ucwords capitalize first chracter of each word
return ucwords(strrev(ucwords(strrev($str))));
}
function findAverage_and_Median($arr){
// count function counts length of array
$arr_count = count($arr);
// array_sum function calculates sum of all elements of array
$arr_sum = array_sum($arr);
$average = ($arr_sum * 1.0) / ($arr_count);
$mid_index = floor($arr_count / 2);
sort($arr, SORT_NUMERIC);
// median value in case of odd count
$median = $arr[$mid_index];
// median valaue on case of even count
if ($arr_count % 2 == 0)
$median = ($median + $arr[$mid_index - 1]) / 2;
return array($average,$median);
}
function find4Digit($str){
// split string seprated by space
$numbers=explode(" ",$str);
// return string of length 4
foreach($numbers as $num)
{
if(strlen($num)==4)
return $num;
}
return "false";
}
echo "Summation of n elements = ",findSummation(5),"<br>";
echo "Capitalize first and last chracter of each word ",uppercaseFirstandLast('practice example'),"<br>";
$arr=findAverage_and_Median(array(2,1,3)),"<br>";
echo "Average of array is = ",$arr[0]," ","Median of array = ",$arr[1],"<br>";
echo "First 4 digit number = ",find4Digit('123 1234 21'),"<br>";
?>
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.