PHP

Topic: Functions

How To Pass Variables By References?

You can pass a variable by reference to a function by taking the reference of the original variable, and passing that reference as the calling argument. Here is a PHP script on how to use pass variables by references:<?phpfunction swap($a, $b) {  $t = $a;   $a = $b;  $b = $t;}$x = "PHP";$y = "JSP";print("Before swapping: $x, $y\n");swap(&$x, &$y);print("After swapping: $x, $y\n");?>This script will print:Before swapping: PHP, JSPAfter swapping: JSP, PHPAs you can see, the function modified the original variable.Note that call-time pass-by-reference has been deprecated. You need to define arguments as references. See next tip for details.

Browse random answers: