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

the ultimate guide to Git: learn everything you need in less than 1 hour
Nov 28, 2022 programming

the ultimate guide to Git: learn everything you need in less than 1 hour

Here is everything you need to know about git in a series of short question and answers. ...

11 Min Read Read More
PHP xml and json
Mar 21, 2023 programming

PHP xml and json

Today we are going to talk about working XML and JSON in PHP. XML and JSON are the most common formats that are used in APIs. knowing how to work with them is essential for any web developer. ...

9 Min Read Read More
PHP Namespace
Mar 23, 2023 programming

PHP Namespace

Today we are going to talk about namespaces in PHP. Namespace is like putting the classes in a group. it helps you organize your code much better. It's a must-know topic for a PHP programmer. ...

7 Min Read Read More
React Beginner to Advance 2025: A Complete Guide to Building Modern Web Applications
Jul 18, 2025 programming

React Beginner to Advance 2025: A Complete Guide to Building Modern Web Applications

React JS has firmly established itself as one of the most widely adopted JavaScript libraries for building dynamic user interfaces. Whether you're crafting a small personal website or architecting a large-scale enterprise dashboard, React's component-based architecture and flexible ecosystem allow developers to create highly performant, scalable, and maintainable applications. ...

29 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 ...