PHP Mid Term Completed
PHP Mid Term Completed
1) (10 marks) Write a function named abs_val that returns the absolute value of an int
passed to it by value. You cannot use the abs() function library.
<?php
function abs_val($num) {
if ($num >= 0) { // if greater than or equal to 0 returns the number, if it's less
//than zero returns the negative of the number, making it positive and returning it
return $num;
} else {
return -$num;
}
}
// How it works
$number = -5;
echo "The absolute value of $number is " . abs_val($number);
?>
Prompt the user for two positive integers a and b. (asks for input)
rangeSum($positiveOne, $positiveTwo);
?>
<?php
$sum=0; $n=1;
$b;
b=5;
while(n<b)
{
$sum += $n;
$n += 1;
}
echo $sum;
?>
3b) What is the function prototype to declare a reference to the int
"numPlayers"?
<?php
function functionName(&$numPlayers) { //int $numPlayers as a parameter
// Function body
}
?>
3c) What is the purpose of a reference in PhP and when would you use it?
(Describe with an example)
References in PHP let you access the same content of a variable by a different
name. Ex:
<?php
$greeting = "hello";
$ref1 =& $greeting;
echo "$ref1\n"; // Outputs: 'hello' referencing $greeting
echo $greeting;
?>
Arrays can store primitive data types and objects, whereas ArrayLists can
contain only object elements. With arrays you can’t use generics while ArrayList
allows use of generics to ensure type safety.
3e) What type of programming language is PhP. What is the difference between
a server-side language and a client-side language?