Download presentation
Presentation is loading. Please wait.
1
Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, frmcclurg@gmail.com © Copyright 2010, All Rights Reserved 1
2
Chapter Five Pass by Value vs. Pass by Reference 2 http://webcert.kirkwood.edu/~fmcclurg/co urses/php/slides/chapter05.valueVsRef.ppt
3
What really happens to a variable that is passed to a function? Passing Variables: The Inside Story 3
4
Description: Pass by value makes a local copy of the variable whose scope resides within the function. Variable Scope: When the function exits, the variable and the value is cleared (freed). This is why a “pass by value” variable value must be passed back to the calling function via a return statement. Pass by Value Distinctives $x 1005 123 Name: Address: Data: $y 1010 123 Name: Address: Data: function() valuecopiedvaluecopied 4
5
<?php function passByValue( $y ) { $y = 321; // function value printf("Inside: %d ", $y); } $x = 123; // initial value printf( "Before: %d ", $x ); passByValue( $x ); printf( "After: %d ", $x ); ?> Pass by Value Example 5
6
Advantages: Makes code more self- contained (encapsulated). Disadvantages: 1. Requires more memory. 2. Slower for large datasets. 3. Only a single variable can be returned via the return statement. Pass by Value Strengths/Weaknesses 6
7
Description: Pass by reference copies the memory location of the value. This acts as a pointer to the original data. Pass by reference acts as a handle to access the original data both inside and outside the function. Variable Scope: When the function exits, the copied address is cleared (freed), however, the original data is still available. Pass by Reference Distinctives $x 1005 123 Name: Address: Data: &$y 1010 1005 Name: Address: Data: function() addresscopiedaddresscopied
8
<?php function passByRef( &$y ) { $y = 321; // function value printf("Inside: %d ", $y); } $x = 123; // initial value printf( "Before: %d ", $x ); passByRef( $x ); printf( "After: %d ", $x ); ?> Pass by Reference Example 8
9
Distinctive: Changes performed on the data within the function also modifies the data outside the function. Advantages: 1.It's fast because values do not need to be copied. 2.Ideal for large datasets. 3.Return multiple values. Disadvantage: Makes the function less encapsulated (less self-contained). Pass by Reference Strength/Weakness 9
10
Problem: Write a function that swaps two values and uses “pass by reference” to return those values. Requirements: 1.Write a driver that calls the swap function with two pass by reference parameters. 2.Write a swap function. 3.Print the value of the variables before and after the call to the function. Student Exercise (Pass by Reference) 10
11
<?php function swap( &$x, &$y ) { $temp = $x; $x = $y; $y = $temp; } $x = 123; $y = 456; printf( "Before: %d, %d ", $x, $y ); swap( $x, $y ); printf( "After: %d, %d ", $x, $y ); ?> Student Solution (Pass by Reference) 11
12
to be continued... http://webcert.kirkwood.edu/~fmcclurg/c ourses/php/slides/chapter05c.include.ppt
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.