PHP "&" operator: Understanding Reference Assignment
In the snippet you are examining, the line $db_host = &$db_hosts[$db_item['hostid']]; contains a specific syntactic construct that often confuses developers coming from other languages or those unused to PHP's dynamic typing. To understand this, we must distinguish between the assignment operator (=), the reference operator (&), and how arrays are accessed.
The core of the confusion lies in the placement of the ampersand. In PHP, a reference is not a distinct data type; it is a way to create an alias for another variable or array element. When you write &$variable, you are telling the interpreter to treat $variable as a reference to whatever memory address the right-hand side points to.
In your specific code example:
$db_host = &$db_hosts[$db_item['hostid']];
The expression on the right, $db_hosts[$db_item['hostid']], evaluates to an element within the array $db_hosts. By prefixing this with &, you are creating a new variable, $db_host, that points directly to that specific array slot.
If you were to modify $db_hosts[$db_item['hostid']] later in your script, changes would instantly reflect when you access $db_host. Conversely, modifying $db_host updates the original array element. This behavior is identical to passing arguments by reference in a function definition (function foo(&$value)) or returning references from functions (available since PHP 5.3).
However, there is a critical logical pitfall in your source code that needs attention. The variable $db_hosts is initialized as an empty array:
$db_hosts = array();
The line $db_host = &$db_hosts[$db_item['hostid']]; attempts to create a reference to an index inside this array that does not yet exist. In PHP, accessing an undefined array key returns NULL. Therefore, you are attempting to create a reference to NULL.
This results in a warning (or error in strict modes) stating "Attempt to assign by reference into a non-referenceable value." You cannot take a reference of something that hasn't been created yet. For this code to function as intendedâstoring a reference to an array elementâyou must first ensure the element exists. This usually implies that $db_hosts is populated elsewhere, or that you need to initialize the specific slot before creating the alias:
$db_hosts[$db_item['hostid']] = null; // Initialize
$db_host = &$db_hosts[$db_item['hostid']]; // Now safe to reference
The spacing in your code (= &) is syntactically valid but stylistically unconventional. Standard PHP coding conventions usually place the ampersand immediately adjacent to the variable name it modifies, resulting in =& on the left side of an assignment or just &$var on the right. The space between the equals sign and the ampersand does not change the functionality, but omitting it reduces visual clutter.
Why Use References?
Developers often reach for references to avoid the overhead of cloning large arrays or objects when passing them around. If you have a massive dataset in $db_hosts and need to pass a specific row to another function without duplicating memory usage, creating a reference ensures that no copy is made. This is particularly useful in legacy PHP 5.x applications (like the version 5.1.6 mentioned in your source material) where object cloning could be expensive.
However, references can introduce subtle bugs if not managed carefully. If one part of the code accidentally reassigns the variable to a different type or value, it breaks the link to the original array slot. In modern PHP development, passing by value is often preferred unless performance profiling explicitly dictates otherwise, as it makes data flow more predictable and easier to debug.
If you are maintaining legacy code like this, your immediate task is likely not to refactor the reference logic itself, but to ensure that the array $db_hosts is pre-populated or initialized before this loop runs. If the intention was to store the result of DBfetch into $db_host, the syntax suggests a design pattern where multiple items are being accumulated in $db_hosts prior to this loop, and $db_host is simply an alias for convenience within the current scope. Always verify that the target index exists before attempting to alias it.