Free cookie consent management tool by TermsFeed Generator PHP Pass Arguments by Reference | Amir Kamizi
AMIR KAMIZI
Home Blog Courses Books Newsletter Store Membership Buy me a coffee
PHP Pass Arguments by Reference

PHP Pass Arguments by Reference

Last Updated on Jan 12, 2023

Introduction

By default when we pass argument to a function we pass it by value which means if we change the value inside the function it won't affect the value outside of the function.

For example in the following code, outside the function the value of a is still 2.

function changeMyNumber($number){
    $number = $number + 10;
    return $number;
}
$a = 2;
$b = changeMyNumber($a);
echo $a; // 2
echo $b; // 12

But what if we wanted our function to have the ability ot change the original variable as well? then we should pass that argument by reference. which means we are passing its pointer to the function and whenever we change that variable instead looking at its name we will check its reference (its pointer) and change that.

Argument Reference

In order to pass the argument by reference we need to prepend an ampersand (&) to the argument name when we are defining our function. like this:

function nameOfTheFunction(&$arg){
    // do something with the argument
}

so if we wanted to actually change the value of a as well in our first example our function would be like this:

function changeMyNumber(&$number){
    $number = $number + 10;
    return $number;
}
$a = 2;
$b = changeMyNumber($a);
echo $a; // 12
echo $b; // 12

so now the value of a changes even outside the function.

It is a great feature but it can also get very tricky. it's good to know that some of the built in functions also get the reference of the argument. for example when we want to sort an array it sorts the original array so we need to be careful.

Conclusion

Now you know how to pass arguments by reference in PHP.

I recommend you to open a PHP files and try to define multiple functions. some of them would get the value of a variable and some would get the reference of the variable.

If you have any suggestions, questions, or opinions, please contact me. I’m looking forward to hearing from you!

Key takeaways

  • Pass arguments by Reference
  • Reference vs Value in function arguments

Category: programming

Tags: #php #tips and tricks

Join the Newsletter

Subscribe to get my latest content by email.

I won't send you spam. Unsubscribe at any time.

Related Posts

PHP catch exceptions
Feb 14, 2023 programming

PHP catch exceptions

Today we are going to talk about catching exceptions in PHP. Most of the time in your script you might face some exceptions. So today we are going to learn how to catch those exceptions. ...

7 Min Read Read More
PHP Method Chaining
Mar 22, 2023 programming

PHP Method Chaining

Today we are going to talk about chaining methods in PHP. It’s cleaner and more readable and I don’t have to refer to my object every time I want to run one of the methods. ...

7 Min Read Read More
PHP Sanitize Data
Feb 15, 2023 programming

PHP Sanitize Data

Today we are going to talk about sanitizing data in PHP. Sanitizing data is a very important step, especially when you are dealing with user data. ...

7 Min Read Read More
PHP Splat Operator
Feb 15, 2023 programming

PHP Splat Operator

Today we are going to talk about splat operator in PHP. Splat operator is when we add three dots before the name of the variable and it's way more useful than you think. ...

7 Min Read Read More

Recommended Courses

Introduction to Machine Learning in PHP

Introduction to Machine Learning in PHP

Learn to Build Different Machine Learning Models Easily ...

PHP Tutorial Beginner to Advanced

PHP Tutorial Beginner to Advanced

Learn everything you need to start a successful career as a PHP developer ...