PHP: function with equal sign as parameter?

Can someone explain to me the declaration of a variable inside the function definition, as shown below. What is the purpose? The coding language I use is PHP.

function parse( $filename=FALSE ) {
//some code
}
+3
source share
5 answers

This is the default value for the function. Therefore, if you call parse (), then $ filename will be FALSE. However, you can also call parse ("/ path / to / my / file") and then $ filename will contain "/ path / to / my / file"

+5
source

This means that when calling a function, the parseparameter is optional; if you did not specify a value, will be used instead FALSE.

php manual " ".

+3

, . $filename , $filename FALSE

function echostring($string = "no string passed") {
echo $string;
}

echostring() // will echo "no string passed"
echostring("hello world") // will echo "hello world"

,

+1

.

, parse . - , $filename FALSE, , $filename .

0

, , , .

, :

function doStuff($var1, $var2 = false) {
 // do stuff
}

Then call the function as follows:

 doStuff("thing1");

Just like calling it like this:

 doStuff("thing1", false);

In addition, a small clarification on your question: the operator is =not an "equal sign". This is actually an "assignment operator". To test equality in PHP, you use ==(or ===, if you want the types to be the same, not just the values).

0
source

Source: https://habr.com/ru/post/1780601/


All Articles