Home >Backend Development >PHP Tutorial >PHP Pass by Reference
The word pass by reference of the PHP Programming Language itself says that whenever the variables are passing by reference then ampersand symbol (&) will be added just before the variable’s argument/arguments. Now take a sample example “function(&$x)”, here both global and function variable’s scope is to become global values because they are defined using the same reference concept. So whenever the global variable is going to be changed then the variable which is inside of the function will also changes and vice versa and it will be applicable to all these.
ADVERTISEMENT Popular Course in this category PHP DEVELOPER - Specialization | 8 Course Series | 3 Mock TestsStart Your Free Software Development Course
Web development, programming languages, Software testing & others
Syntax and parameters
function function_name(&$function_parameter){ .. .. }
Explanation of the parameters of the PHP pass by reference:
The pass by reference of the PHP Programming Language basically works just bypassing the variable/variables by reference with the help of the ampersand symbol(&) just before the variable’s argument. If we pass a variable by the reference concept then a function can have the capability of modifying the variable. In this way, the PHP pass by reference concept works and it is an important concept to know at some of the coding cases. For some PHP versions, pass by reference concept doesn’t work well. In PHP 5.3.0, you will encounter a warning which says that “call-time pass-by-reference” and in PHP 5.4.0 version, call-time passing by reference will be removed so using of it will raise some fatal errors in some PHP versions.
Below are the examples mentioned:
This is the example of illustrating the pass by reference concept of the PHP Programming Language. Here at first a function “calculate()” is created along with its reference function parameter “$a1”. Then inside of the function, that variable value is incremented. Then after coming out of the function “$a1” variable value is declared as “5”. Then by using the echo functionality value of $a1 will be printed and then the calculate() function is called. After calling the function, echo function is used again and this time the variable value will be printed as “6” due to incrementing.
Code:
<?php function calculate(&$a1){ $a1++; } $a1=5; echo "This is the value of a1 variable before the function call :: "; echo $a1; echo "<br>"; echo "This is the value of a1 variable after the function call :: "; calculate($a1); echo $a1; ?>
Output:
This is also an example of illustrating the same concept of PHP language but here string values are used instead of using numerical values just like the example 1. Here print_string() function is with a variable “string1” after the ampersand symbol. Then inside of the function, string1 variable value is assigned as “Function sake” and then used to print the string1 variable value. Then outside of the function, again string1 variable value is assigned as “Global sake”. Then again $string1 variable is printed using the print functionality but this time the string value which is present inside the function is printed but not the global value. This is because the same concept of PHP.
Code:
<?php // Here Function is used just for assigning the new value to // some $string1 variable and then printing it echo "This is the exampe of PHP pass by reference concept :: "; echo "<hr>"; function print_string( &$string1 ) { $string1 = "Function sake \n"; // Print $string1 variable print( $string1 ); echo "<br>"; } // Drivers code $string1 = "Global sake \n"; print_string( $string1 ); print( $string1 ); echo "<br>"; ?>
Output:
This example is similar to example 2 but here ampersand symbol is not used just to make sure what will happen if it is not used. If ampersand is not used we can call it as pass by value concept too. So don’t forget to use & symbol before the variable parameters of the function if you won’t the capability. Here at the second print output, you will get “Global sake” because of the lack of pass by reference concept. Just check out the outputs of example 2 and example 3 to understand the concept better.
Code:
<?php // Here Function is used just for assigning the new value to // some $string1 variable without using ampersand and then printing it echo "This is the example of PHP pass by reference concept but exempted ampersand symbol :: "; echo "<hr>"; function print_string( $string2 ) { $string2 = "Function sake \n"; // Print $string1 variable echo "This is the function's variable parameter value inside function :: "; print( $string2 ); echo "<br>"; } // Drivers code $string2 = "Global sake \n"; print_string( $string2 ); echo "This is the function's variable parameter value outside the function :: "; print( $string2 ); echo "<br>"; ?>
Output:
This is the example of implementing the swapping functionality with the help of the swap() function and the same concept of the PHP Programming Language. Here at first two variables a1 , b1 is created with numerical and string values. Then those numbers are swapped with the swap() function. Then inside of the function using an extra variable value are jumbled to one other for swapping functionality. Then swapping will be done with that function. You can check out the output in the output section to understand the pass by reference with the swapping mechanism of PHP.
Code:
<?php // ------------------------------------------ // This is sample Demo call of swap(...) function below. echo "This is the example of swapping two variable values using the pass by reference concept of PHP :: "; echo "<br>"; $a1 = 123.456; $b1 = 'abcDEF'; print "<pre class="brush:php;toolbar:false">Define:\na1 = $a1\nb1 = '$b1'"; swap($a1,$b1); print "
After swap(a1,b1):\na1 = '$a1'\nb1 = $b1"; // ------------------------------- function swap (&$arg11, &$arg12) { // Now Swapping the contents of the indicated variables. $w1=$arg11; $arg11=$arg12; $arg12=$w1; } ?>
Output:
I hope you learned what is the definition of PHP pass by reference along with its syntax and some explanation of the parameters, How the works in PHP Programming Language along with various examples of PHP to understand PHP pass by reference concept better.
The above is the detailed content of PHP Pass by Reference. For more information, please follow other related articles on the PHP Chinese website!