Pregunta

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);

¿Fue útil?

Solución

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

Otros consejos

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

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)

"<?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.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top