InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

Menu
  • Home
  • Sitemap

Python Programming Language Best Tutorials and Code Examples

Learn Python Right Now!
Home
PHP
Returning Values by Reference in PHP
PHP

Returning Values by Reference in PHP

InfinityCoder November 21, 2016

You want to return a value by reference, not by value. This allows you to avoid making a duplicate copy of a variable.

The syntax for returning a variable by reference is similar to passing it by reference.

However, instead of placing an & before the parameter, place it before the name of the function:

1
2
3
4
5
6
7
function &array_find_value($needle, &$haystack) {
   foreach ($haystack as $key => $value) {
      if ($needle == $value) {
        return $haystack[$key];
      }
   }
}

Also, you must use the =& assignment operator instead of plain = when invoking the function:

1
$band =& array_find_value('The Doors', $artists);

Returning a reference from a function allows you to directly operate on the return value and have those changes directly reflected in the original variable.
The following code searches through an array looking for the first element that matches a value. It returns the first matching value.

For instance, you need to search through a list of famous people from Minnesota looking for Prince, so you can update his name:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
function &array_find_value($needle, &$haystack) {
   foreach ($haystack as $key => $value) {
      if ($needle == $value) {
        return $haystack[$key];
      }
   }
}
 
$minnesota = array('Bob Dylan', 'F. Scott Fitzgerald',
                   'Prince', 'Charles Schultz');
 
$prince =& array_find_value('Prince', $minnesota);
 
$prince = 'O(+>'; // The ASCII version of Prince's unpronounceable symbol
 
print_r($minnesota);

This prints:

1
2
3
4
5
6
7
Array
(
    [0] => Bob Dylan
    [1] => F. Scott Fitzgerald
    [2] => O(+>
    [3] => Charles Schultz
)

Without the ability to return values by reference, you would need to return the array key and then rereference the original array:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
function array_find_value($needle, &$haystack) {
   foreach ($haystack as $key => $value) {
       if ($needle == $value) {
         return $key;
       }
   }
}
 
$minnesota = array('Bob Dylan', 'F. Scott Fitzgerald',
                   'Prince', 'Charles Schultz');
 
$prince = array_find_value('Prince', $minnesota);
// The ASCII version of Prince's unpronounceable symbol
$minnesota[$prince] = 'O(+>';

When returning a reference from a function, you must return a reference to a variable, not a string. For example, this is not legal:

1
2
3
4
5
6
7
8
9
function &array_find_value($needle, &$haystack) {
   foreach ($haystack as $key => $value) {
       if ($needle == $value) {
         $match = $haystack[$key];
       }
   }
 
   return "$match is found in position $key";
}

That’s because “$match is found in position $key” is a string, and it doesn’t make logical sense to return a reference to nonvariables. This causes PHP to emit an E_NOTICE.

Unlike passing values into functions, in which an argument is either passed by value or by reference, you can optionally choose not to assign a reference and just take the returned value.

Just use = instead of =&, and PHP assigns the value instead of the reference

Share
Tweet
Email
Prev Article
Next Article

Related Articles

Connecting to an SQL Database in PHP
You want access to a SQL database to store or …

Connecting to an SQL Database in PHP

Keeping Passwords Out of Your Site Files in PHP
You need to use a password to connect to a …

Keeping Passwords Out of Your Site Files in PHP

About The Author

InfinityCoder
InfinityCoder

Leave a Reply

Cancel reply

Recent Tutorials InfinityQuest

  • Adding New Features to bash Using Loadable Built-ins in bash
    Adding New Features to bash Using Loadable …
    June 27, 2017 0
  • Getting to the Bottom of Things in bash
    Getting to the Bottom of Things in …
    June 27, 2017 0

Recent Comments

  • fer on Turning a Dictionary into XML in Python
  • mahesh on Turning a Dictionary into XML in Python

Categories

  • Bash
  • PHP
  • Python
  • Uncategorized

InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

About Us

Start learning your desired programming language with InfinityQuest.com.

On our website you can access any tutorial that you want with video and code examples.

We are very happy and honored that InfinityQuest.com has been listed as a recommended learning website for students.

Popular Tags

binary data python CIDR convert string into datetime python create xml from dict python dictionary into xml python how to create xml with dict in Python how to write binary data in Python IP Address read binary data python tutorial string as date object python string to datetime python

Archives

  • June 2017
  • April 2017
  • February 2017
  • January 2017
  • December 2016
  • November 2016
Copyright © 2021 InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more
Programming Tutorials | Sitemap