Doc #79367 [Opn->Nab]: Undocumented behavior: Pass-by-ref arguments do not have to be initialized
Edit report at https://bugs.php.net/bug.php?id=79367&edit=1
ID: 79367
Updated by: cmb@php.net
Reported by: mwolffhh at gmail dot com
Summary: Undocumented behavior: Pass-by-ref arguments do not
have to be initialized
-Status: Open
+Status: Not a bug
Type: Documentation Problem
Package: Documentation problem
PHP Version: Irrelevant
-Assigned To:
+Assigned To: cmb
Block user comment: N
Private report: N
New Comment:
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>
Previous Comments:
------------------------------------------------------------------------
[2020-03-11 13:17:53] 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"
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=79367&edit=1
Thread (2 messages)