In: Computer Science
Requirements: In this assignment, you are going to create two switch functions. The first function is going to be called switchVal and the second is going to be called switchRef. The goal of this assignment is to demonstrate the understanding of what pass-by-reference (Links to an external site.) and pass-by-value (Links to an external site.) do when working with functions that you create. The two functions that you will modify need to accept two parameters and inside them they need to switch the values.
Getting Started: Create a new folder ('assignment6'). Use the following template to create a PHP web page named switchExercise.php in your assignment5 folder. You will need to modify the two function definitions to do the work of switching the x and y values. You also need to add echo statements in the functions to announce which function was called and the values of x and y before the function exits.
<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project
Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html>
<head>
<meta
charset="UTF-8">
<title></title>
</head>
<body>
<?php
// Retrieve the
values from the web page for x and y
$x =
$_POST['x'];
$y =
$_POST['y'];
function
switchVal() {
}
function
switchRef() {
}
?>
<form
action="switchExercise.php" method="post">
$x=
<input name="x" style="width: 50px;" type="number" > (enter a
number value to use for X)<br/>
$y=
<input name="y" style="width: 50px;" type="number"
> (enter a number value to use for
Y)<br/>
<br/>
<input
type="submit" name="switchVal"
value=" Switch By
Value "/><br/>
<input
type="submit" name="switchRef" value="Switch By
Reference"/><br/>
</form>
<?php
if (!empty($x)
&& !empty($y)) {
echo
"Values before function call <br/>";
echo
"X: $x Y: $y";
//
Write a conditional statement to determine which button was
selected
//
and call the appropriate function
echo
"Values after function call
<br/>";
echo
"X: $x Y:
$y";
}
?>
</body>
</html>
Source Code :
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<?php
// Retrieve the values from the web page for x and y
$x = $_POST['x'];
$y = $_POST['y'];
//pass by value function
//when this is used
//only the value inside the function is changed
function switchVal($x,$y) {
echo "<br/>switchVal function is called";
//temp variable is used to store
//a value when swapping or switching
//otherwise value in one variable will be lost
//value of x is stored in $temp
$temp = $x;
//value of $y is stored in $x
$x = $y;
//value of $temp (initial value in $x) is stored in $y.
$y = $temp;
//display the value before the function exit
echo "<br/>values before switchVal function exit
<br/>";
echo "X: $x Y: $y";
}
//pass by reference
function
//here & is used
//when it is used
//the change in value will also reflect in actual value
//because here the reference is passed
function switchRef(&$x,&$y) {
echo "<br/>switchRef function is called";
//temp variable is used to store
//a value when swapping or switching
//otherwise value in one variable will be lost
//value of x is stored in $temp
$temp = $x;
//value of $y is stored in $x
$x = $y;
//value of $temp (initial value in $x) is stored in $y.
$y = $temp;
//display the value before the function exit
echo "<br/>values before switchRef function
exit<br/>";
echo "X: $x Y: $y";
}
?>
<form action="switchExercise.php" method="post">
$x= <input name="x" style="width: 50px;" type="number"
> (enter a number value to use for X)<br/>
$y= <input name="y" style="width: 50px;" type="number" >
(enter a number value to use for Y)<br/>
<br/>
<input type="submit" name="switchVal" value=" Switch By Value
"/><br/>
<input type="submit" name="switchRef" value="Switch By
Reference"/><br/>
</form>
<?php
if (!empty($x) && !empty($y)) {
//display value befor function call
echo "Values before function call <br/>";
echo "X: $x Y: $y";
// Write a conditional statement to determine which button was
selected
// and call the appropriate function
//isset() checks whether a variable is set or not
//if set and not null, it will return true
//checks if switchVal button is clicked
if(isset($_POST['switchVal'])){
switchVal($x,$y);
}
//checks if switchRef button is clicked
if(isset($_POST['switchRef'])){
switchRef($x,$y);
}
//display value after function call
echo "<br/>Values after function call <br/>";
echo "X: $x Y: $y";
}
?>
</body>
</html>
Output and screenshots :