Reputation: 4116
First of all, I can't recall the name of this process, but it looks something like this:
function test($alter = FALSE){
//do stuff
return $alter;
}
Making $alter = FALSE right in the function declaration
What is that called? How does this work? What happens in the following circumstances?
$result = test();
$result = test(TRUE);
Upvotes: 0
Views: 85
Reputation: 39
"<?php
echo"welcome";
function a($b=false){
echo"<br /> b: ".$b;
}
a(true);
a();
a("some text");
a(false);
?>
result :
welcome
b: 1
b:
b: some text
b:
"
it seems that if its false/null/empty it does not print anything.. and what ever you pass to that method string/boolean it does print as long as not null/empty.
Upvotes: 1
Reputation: 1420
Nothing to add except: The term that you probably might be remembering is "function overloading" but this isn't a real embodiment of this (it's just PHP's "default parameter" is perhaps similar)
Upvotes: 1
Reputation: 4046
FALSE defined in method header is the default value (if nothing is added to parameter while calling) - test()
otherwise it behaves like a normal parameter.. so if you call test(TRUE)
value will be TRUE
Upvotes: 1
Reputation: 71384
FALSE
is defined as the default value if no other value is passed.
In the case of you examples the results (in order) would be:
FALSE
TRUE
Upvotes: 4