php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #79367 Undocumented behavior: Pass-by-ref arguments do not have to be initialized
Submitted: 2020-03-11 13:17 UTC Modified: 2020-03-11 13:23 UTC
From: mwolffhh at gmail dot com Assigned: cmb (profile)
Status: Not a bug Package: Documentation problem
PHP Version: Irrelevant OS:
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: mwolffhh at gmail dot com
New email:
PHP Version: OS:

 

 [2020-03-11 13:17 UTC] mwolffhh at gmail dot com
Description:
------------
---
From manual page: https://php.net/language.references.pass
---

Usually, you must define a variable before it is used, otherwise PHP will produce a notice.

When an argument defined as pass-by-reference though, defining the variable before using it is no longer neccessary.

This is not mentioned in the pass-by-ref docs, nor in the function argument docs:
https://www.php.net/manual/en/functions.arguments.php#functions.arguments.by-reference


Test script:
---------------
function output($s) {
    echo $s;
}
output($a); // => PHP emits notice, "Undefined variable: a"
$a = "Test";
output($a); // => outputs "Test"

function outputByRef(&$s) {
    echo $s;
}
outputByRef($a); // => no output, no notice
$a = "Test";
outputByRef($a); // => outputs "Test"


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2020-03-11 13:23 UTC] cmb@php.net
-Status: Open +Status: Not a bug -Assigned To: +Assigned To: cmb
 [2020-03-11 13:23 UTC] cmb@php.net
This is already documented in the section "Assign By
Reference"[1]:

| If you assign, pass, or return an undefined variable by
| reference, it will get created.

[1] <https://www.php.net/manual/en/language.references.whatdo.php#language.references.whatdo.assign>
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Apr 20 00:01:27 2024 UTC