0

option 1:

<?php 
function hookRequest($func, $params = array()){
    var_dump($func);
    var_dump($params);
}

hookRequest('func1', array('param1', 'param2'));

option 2:

<?php 
function hookRequest($func, $params){
    var_dump($func);
    var_dump($params);
}

hookRequest('func1', array('param1', 'param2'));

Question:

Both of above scripts can work. But I saw some scripts use this way: $params = array(), so just want to find out what is the difference between $params = array() and $params ?

4 Answers 4

2

If you don't pass anything into option1

hookRequest('func1');

then the $params is now an empty array.

function foobar($something,$foo = 'var')
{
   var_dump($something,$foo);
}

foobar('something');

Output:

string(9) "something" string(3) "var"
Sign up to request clarification or add additional context in comments.

Comments

1

Have a look on the "Function Arguments" basics in

http://php.net/manual/en/functions.arguments.php

Comments

0

The difference is that option 1 makes the second parameter optional, so that you can leave out the second option and the default value will be given to $param.

Option 2 makes the second parameter required, and will return a warning if you don't provide at least two parameters, e.g.

Warning: Missing argument 2 for hookRequest

Comments

0

It's called default parameters in PHP.

When you declare your function, hookRequest($func, $params = array()){... the $paramas = array() tell it to set it as an array when the passed parameter is blank.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.