Jump to content

Initialized and Uninitialized Function Parameters


iwato

Recommended Posts

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

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

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...