Write a PHP function increase () that has two parameters, the first by-reference
ID: 3575862 • Letter: W
Question
Write a PHP function increase () that has two parameters, the first by-reference. and the second by-value. It adds the value of the second on to the first. For example, $u = 2; increase ($u, 3); echo $u; outputs 5. Note that there is no return statement. Answer (My solution has one line in the body of the function.) Write a PHP class MyClass that has one public property. $x. which is expected to be a number and is initialized when an instance is created. This class has one instance method, foo (). which is passed a number and returns the sum of that number and the value of property $x (Do not check that values arc of the proper type.) For example. $cl = new MyClass (2); echo $cl rightarrow foo(3); outputs 5. (My solution has 6 lines, not counting blank lines and lines containing only brackets.)Explanation / Answer
Answer
(7)
<?php
$originalvalue = 20;
//&$u reference variable
//function defination
function increase(&$u,$num){
//sum of two values
$sumofvalue = $u + $num;
echo "sum of two values = $sumofvalue";
}
//method call
increase($originalvalue,3);
?>
Answer
(8)
<?php
//class MyClass declaration
class MyClass{
//$x declaration
var $x;
//foo method
function foo($num){
// sum of x and num values
$sum = $x + $num;
//return sum
return $sum;
}
}
//object creation
$c1 = new MyClass(2);
//method call with object
echo $c1->foo(3);
?>
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.