CS 199 Willamette University Spring, 2019
 
Programming in PHP, Databases with MySQL,
and Web Applications
 

Laboratory Exercise on Local and Global Variables with Functions

Summary: This laboratory exercise examines the concepts of local and global variables, and the use of these concepts within functions. The lab also provides more practice with the use of loops and arrays within functions.

Scope

Be sure to read pages 220-221 in your textbook for a definition of variable scope. On page 220 the textbook says, "Variables created inside a function (including any argument variables) exist only within that function, and disappear when the function has run its course." However, the reading also describes a way to access variables outside the function and use them within the function, as covered later in this lab.

Now take a look at the following code which clarifies the description in the text.

$value1 = 1; // Global variable

function hello($value2){ //Value2 is in the [local] scope of the function
	echo "$value2";  //printing variable in the function's local scope
	echo $GLOBALS['value1']; //printing the out-of-scope or global variable
}
  1. For the following problem, run the code to determine the output.

    function add($num1, $num2){
    	$num3 = $num1 + $num2;
    	echo "$num1 + $num2 = $num3";
    }
    
    add(3,4);
    
    
    1. What happens when this code is executed?
    2. For each variable given, indicate whether it is local or global and briefly explain.
  2. For the following problem, run the code to determine the output.

    $num1 = 3;
    $num2 = 4;
    
    function add(){
    	$num3 = $num1 + $num2;
    	echo "$num1 + $num2 = $num3";
      }
      
    add( );
    
    1. What happens when this code is executed?
    2. For each variable given, indicate whether it is local or global and briefly explain.
  3. For the following problem, run the code to determine the output.

    $a = 27;
    $b = 19;
    
    function Sum()
    {
        $GLOBALS['b'] =  $GLOBALS['a'] + $GLOBALS['b'];
    } 
    
    Sum();
    echo $b; 
    
    1. What happens when this code is executed?
    2. How was the function, Sum, able to retrieve the variables outside of the function?
  4. Consider the following function:

    function mult(){
    	return $x * $y;
    }
    
    $x = 7;
    $y = 5;
    $z = mult();
    echo "$z"
    
    $x = 19;
    $y = 6;
    $z = mult();
    echo "$z"
    
    

    The intention of this code is to compute $z as the product $x and $y. However, the code does not work. Two fixes are proposed:

    1. Define the function so it has 2 parameters and pass $x and $y in it.
    2. Access $x and $y outside of the function using the GLOBALS concept.
    3. Looking at these two approaches, do either of these seem preferred? Explain.
  5. Run the following code.

    $num1 = 5;
    
    function checking($num2){
    	echo "num1 = $num1 " + "num2 = $num2";
    }
    
    echo "num1 = $num1  + num2 = $num2";
    
    checking(7);
    
    

    Describe what happens and explain why.

  6. In the Nested loop Lab, you created a checkerboard. Create a function checkerboard that makes a checkerboard, but the parameters allow you to choose two colors of the checkerboard. As parameters you should pass color 1 ($r1, $b1, $g1) and color 2 ($r2, $b2, $g2).

  7. Create a function countThis with an array of numbers as the parameter, $array. Within the funtion, initialize a variable called $count to 0, and run through a loop to check if the values in the array are even or odd. If it is an even number, it should count it (increment the $count variable). Finally, the function should print the total number of even numbers. (If there are no even numbers, it should print out "there are 0 even numbers".)

  8. In the nested for loop lab you created:

    1
    22
    333
    4444
    

    This time, the function should take as parameter the number of levels the triangle should go. The variable $levels should be passed within the parameters.

  9. In the Groceries Lab you wrote a block of code that created a table of groceries. Now move the code that prints the table to a separate function that has as parameter a variable $row that contains all the rows from the table. Inside the function, a while loop allow the printing of the table, row by row.

  10. For this problem you will calculate the wind chill based off of the following chart. This is the formula for calculating wind chill:

    wind chill = 35.74 + 0.6215T - 35.75(V0.16) + 0.4275T(V0.16)

    Create a function windChill that displays a table of wind chill temperatures, as shown below.

    Hints:

                                 Temperature
               
                    0        5      10     15     20   ...   60
    Velocity    0  35.74   38.85  41.96  45.06  48.17  ...  ...
                2  ...      ...    ...    ...    ...   ...  ...
                4  ...      ...    ...    ...    ...   ...  ...  
                6  ...      ...    ...    ...    ...   ...  ...
                8  ...      ...    ...    ...    ...   ...  ...
                .   
                .
    	    .   
               24  ...      ...    ...    ...    ...   ...  ...
    



created 14 April 2019 by Saniya Lakka
revised 19 April 2019 by Saniya Lakka and Henry M. Walker
Valid HTML 4.01! Valid CSS!