PHP

Topic: Functions

How Variables Are Passed Through Arguments?

Like more of other programming languages, variables are passed through arguments by values, not by references. That means when a variable is passed as an argument, a copy of the value will be passed into the function. Modifying that copy inside the function will not impact the original copy. Here is a PHP script on passing variables by values:<?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: PHP, JSPAs you can see, original variables were not affected.

Browse random answers: