iwato Posted September 14, 2010 Share Posted September 14, 2010 QUESTION: Besides the fact that foo() will only accept string-type values as its parameter. Is there any difference between the following two functions and the way that they might behave when called? function foo($arg = '') { echo $arg;}function bar($arg) { echo $arg;} Roddy Link to comment Share on other sites More sharing options...
Synook Posted September 14, 2010 Share Posted September 14, 2010 http://www.php.net/manual/en/functions.arg...guments.defaultSo you can call foo() without any arguments, and $arg will be initialized to ''. I believe it will still accept an argument of any data type though... Link to comment Share on other sites More sharing options...
ShadowMage Posted September 14, 2010 Share Posted September 14, 2010 QUESTION: Besides the fact that foo() will only accept string-type values as its parameter. Is there any difference between the following two functions and the way that they might behave when called? function foo($arg = '') { echo $arg;}function bar($arg) { echo $arg;} Roddy The only real difference is that you can call the first one without any parameters and it will still work. If you call foo() then $arg will be initialized as an empty string but if you call bar() you'll get an undefined variable notice.Like Synook said though, you can still pass any type of variable you want to foo() and it will accept it. Link to comment Share on other sites More sharing options...
iwato Posted September 15, 2010 Author Share Posted September 15, 2010 The only real difference is that you can call the first one without any parameters and it will still work. If you call foo() then $arg will be initialized as an empty string but if you call bar() you'll get an undefined variable notice.Like Synook said though, you can still pass any type of variable you want to foo() and it will accept it.It is a very good summary of two very good answers to my question.Thank you very much to both ShadowMage and Synook. I now have a much clearer idea about how to work with initialized and uninitialized function parameters.Roddy Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.