note 114413 added to language.references.whatare

From: Date: Tue, 18 Feb 2014 02:06:34 +0000
Subject: note 114413 added to language.references.whatare
Groups: php.notes 
Request: Send a blank email to php-notes+get-198538@lists.php.net to get a copy of this message
If I make a function, using PHP 5.5.5: function recurring_mailer_form( $form, $form_state ) {} Is it the same as: function recurring_mailer_form( $form, & $form_state ) {} ? ---------- NO Using php 5.5.x+ If you pass an object as usual (ie, without the ampersand), you can (only) alter the object's state (properties) (but not change the whole obj into a new obj) $obj is an instance of the class Test which contains a member variable called hello: function modify($obj) { $obj->hello = 'world (modified)!'; } $obj->hello = 'world'; modify($obj); var_dump($obj->hello); // outputs "world (modified!)" ---------- Now, using the same code but assigning another value to $obj instead modifying the object's state results in no modification: function modify($obj) { $obj = 42; } var_dump($obj->hello); // outputs "world" ---------- Only accepting the parameter explicitly as a reference gives us the ability to completely change the variable's contents: function modify(&$obj) { $obj = 42; } var_dump($obj); // outputs "42" ---------- http://stackoverflow.com/questions/19847781/do-i-need-to-use-the-ampersand-in-php-5-5-x-and-above-anymore ---- Server IP: 72.52.91.14 Probable Submitter: 72.52.91.14 ---- Manual Page -- http://php.net/manual/en/language.references.whatare.php Edit -- https://master.php.net/note/edit/114413 Del: integrated -- https://master.php.net/note/delete/114413/integrated Del: useless -- https://master.php.net/note/delete/114413/useless Del: bad code -- https://master.php.net/note/delete/114413/bad+code Del: spam -- https://master.php.net/note/delete/114413/spam Del: non-english -- https://master.php.net/note/delete/114413/non-english Del: in docs -- https://master.php.net/note/delete/114413/in+docs Del: other reasons-- https://master.php.net/note/delete/114413 Reject -- https://master.php.net/note/reject/114413 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#198538) next »