note 123995 added to language.references.pass

From: Date: Mon, 01 Jul 2019 05:58:09 +0000
Subject: note 123995 added to language.references.pass
Groups: php.notes 
Request: Send a blank email to php-notes+get-211452@lists.php.net to get a copy of this message
<?php // PHP >= 5.6 // Here we use the 'use' operator to create a variable within the scope of the function. Although it may seem that the newly created variable has something to do with '$x' that is outside the function, we are actually creating a '$x' variable within the function that has nothing to do with the '$x' variable outside the function. We are talking about the same names but different content locations in memory. $x = 10; (function() use ($x){ $x = $x*$x; var_dump($x); // 100 })(); var_dump($x); // 10 // Now the magic happens with using the reference (&). Now we are actually accessing the contents of the '$y' variable that is outside the scope of the function. All the actions that we perform with the variable '$y' within the function will be reflected outside the scope of this same function. Remembering this would be an impure function in the functional paradigm, since we are changing the value of a variable by reference. $y = 10; (function() use (&$y){ $y = $y*$y; var_dump($y); // 100 })(); var_dump($y); // 100 ?> ---- Server IP: 208.43.231.9 Probable Submitter: 185.85.3.195 (proxied: 2804:d57:2e2d:c900:f8d3:c75e:23a7:ba90) ---- Manual Page -- http://php.net/manual/en/language.references.pass.php Edit -- https://master.php.net/note/edit/123995 Del: integrated -- https://master.php.net/note/delete/123995/integrated Del: useless -- https://master.php.net/note/delete/123995/useless Del: bad code -- https://master.php.net/note/delete/123995/bad+code Del: spam -- https://master.php.net/note/delete/123995/spam Del: non-english -- https://master.php.net/note/delete/123995/non-english Del: in docs -- https://master.php.net/note/delete/123995/in+docs Del: other reasons-- https://master.php.net/note/delete/123995 Reject -- https://master.php.net/note/reject/123995 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#211452) next »