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
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
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-2020 The PHP Group
All rights reserved.
Last updated: Thu Nov 26 04:01:23 2020 UTC