Passing arguments to functions is a fundamental aspect of programming. How these arguments are passed, and whether they’re treated as references or values, can have a significant impact on how your code behaves. In this article, we’ll delve into how passing arguments in PHP and JavaScript work.
PHP Default Behavior: Pass by Value
By default, PHP passes function arguments by value. This means that a copy of the variable’s value is passed to the function, rather than the actual variable itself. Any changes made to the parameter inside the function do not affect the original variable outside the function.
function increment($num) {
$num++;
}
$num = 5;
increment($num);
echo $num; // Output: 5In this example, the value of $num remains unchanged outside the increment() function, despite being modified inside the function.
The same default behaviour applies to other data types like arrays:
$numbers = [1,2];
function add_number($list){
$list[] = 3;
print_r($list);
}
add_number($numbers); // Output Array ( [0] => 1 [1] => 2 [2] => 3 )
print_r($numbers); // Output Array ( [0] => 1 [1] => 2 )Again, the value of the $numbers array remains unchanged outside the add_number() function, despite being changed inside the function.
Passing Arguments by Reference in PHP
In PHP, you can explicitly pass arguments by reference, allowing functions to modify the original variable directly. This is achieved by prefixing the parameter in the function definition with an ampersand (&).
Note that this must be done when the function is defined not when it’s called.
function increment(&$num) {
$num++;
}
$num = 5;
increment($num);
echo $num; // Output: 6In this example, $num is passed by reference to the increment() function. Any changes made to $num inside the function directly affect the original variable $num outside the function.
You can also pass other data types (liked arrays) by reference.
$numbers = [1,2];
function add_number(&$list){
$list[] = 3;
print_r($list);
}
add_number($numbers); // Output Array ( [0] => 1 [1] => 2 [2] => 3 )
print_r($numbers); // Output Array ( [0] => 1 [1] => 2 [2] => 3 )In this example because the $list argument is passed by reference, any changes made to is inside the function affects the original array outside the function.
JavaScript Default Behavior
In JavaScript, primitive data types such as numbers, strings, and booleans are passed by value by default. On the other hand objects (including arrays and functions) are passed by reference. So the default behaviour depends on the data type of the argument.
function increment(num) {
num++;
}
let num = 5;
increment(num);
console.log(num); // Output: 5In this example, the value of num remains unchanged outside the increment() function, despite being modified inside the function. This is because the data type of the argument is a primitive (number).
function updateName(person) {
person.name = "Alice";
}
let user = { name: "Bob" };
updateName(user);
console.log(user.name); // Output: "Alice"In this example, the updateName() function modifies the name property of the user object directly, demonstrating pass by reference behavior.
Conclusion
In summary, PHP is more flexible and predictable about how it handles passing arguments to a function. By default all data types are passed by value, and if you want a function to pass arguments by reference, you can easily do that by adding an & in the function definition. In contrast, JavaScript only supports pass by value for primitive types (like numbers and strings) and pass by reference for objects.