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 Password Hashing
Mar 22, 2023 programming

PHP Password Hashing

Today we are going to talk about hashing passwords in PHP. Storing and managing passwords is very important. And in PHP it’s very easy. ...

4 Min Read Read More
PHP Array Search
Feb 14, 2023 programming

PHP Array Search

Today we are going to talk about array search function in PHP. array search is a very useful function that helps you find the key of a value you’re looking for. ...

3 Min Read Read More
Creating a Simple Jupyter Notebook in PHP
Aug 28, 2024 programming

Creating a Simple Jupyter Notebook in PHP

This tutorial will guide you through the steps to create a simple PHP-based notebook interface. The notebook allows you to write and run PHP code in a web browser, maintaining the state between code executions. ...

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