PHP: function with equality sign as parameter?
Can some开发者_如何学Cone explain to me a variable declaration inside a definition of a function, like the one below. What's the purpose? The coding language I use is PHP.
function parse( $filename=FALSE ) {
//some code
}
That is a default value for the function. So if you call parse(), then $filename will be FALSE. However, you could also call parse("/path/to/my/file") and then $filename will contain "/path/to/my/file"
It means that when you call the parse
function the parameter is optional; if you don't provide a value, then FALSE
will be used instead.
You can see more details on the php manual in the section "Default Argument Values".
Put simply, it is the default value of that argument. If the $filename argument is not passed when calling the parse function, then the $filename variable will default to FALSE
function echostring($string = "no string passed") {
echo $string;
}
echostring() // will echo "no string passed"
echostring("hello world") // will echo "hello world"
Hope that helps
It is a function with default argument value.
It means when calling the function parse
passing argument to it is optional. If you don't pass any argument the variable $filename
will take the default value of FALSE
but if you do pass one, $filename
will take the passed value.
It's just a default value for the argument, so if you don't specify it in the call, the function still has a value to use for it.
In other words, if I write this function:
function doStuff($var1, $var2 = false) {
// do stuff
}
Then calling the function like this:
doStuff("thing1");
Is exactly the same as calling it like this:
doStuff("thing1", false);
Also, a slight clarification on your question: The =
operator isn't the "equality sign". It's actually the "assignment operator". To check equality in PHP, you use ==
(or ===
if you want to ensure the types are the same as well, not just the values).
精彩评论