Drezard Posted November 27, 2007 Share Posted November 27, 2007 I am currently reading the Zend Certification book. It talks about functions starting with a &. But, I dont quite understand what it means. When do you put a & at the front of a function? The example it gives is: function &query($sql) { $result = mysql_query($sql); return $result; } // The following is incorrect and will cause PHP to emit a notice when called. function &getHello() { return "Hello World"; } // This will also cause the warning to be issued when called function &test() { echo ’This is a test’; } Thanks, Daniel Link to comment https://forums.phpfreaks.com/topic/79034-functions/ Share on other sites More sharing options...
Daukan Posted November 27, 2007 Share Posted November 27, 2007 You can't use an & in front of a function. The & is used to call a variable by reference. It can be used with functions that return references. <?php $var1 = 'test'; $var2 =& $var; function test_function($v) { if($v) return $v; } $var3 =& test_function($var2); ?> A functions argument can reference a variable <?php $foo = 'test'; function change_var(&$var) { $var .= ' var'; } change_var($foo); echo $foo;// displays: test var ?> Link to comment https://forums.phpfreaks.com/topic/79034-functions/#findComment-399981 Share on other sites More sharing options...
btherl Posted November 27, 2007 Share Posted November 27, 2007 Yes you can use & in front of a function. But very few people do because it usually doesn't make sense. The only situation I can think of when return by reference makes sense is when dealing with arrays. The examples in Drezard's post, there seems to be no reason why you would return a mysql result be reference. Link to comment https://forums.phpfreaks.com/topic/79034-functions/#findComment-400000 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.