Sign up ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free.

I want to pass a parameter to a function as below for clarity. It doesn't give any error. But is this a bad practice. Do I have to avoid this.

test('','','',$d='text','');

function test($a, $b, $c, $d, $e){

}
share|improve this question

closed as unclear what you're asking by Simon, Bart van Ingen Schenau, GlenH7, psr, MichaelT Jan 23 '14 at 1:16

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question.If this question can be reworded to fit the rules in the help center, please edit the question.

1  
Are you asking about the practice of passing parameters to functions, or are you asking about the particular syntax that you used in your function call? –  Bart van Ingen Schenau Jan 22 '14 at 7:20
    
Just want to know whether above syntax is correct and don't make any harm to my program. –  Duli Jan 22 '14 at 9:11
    
As it is a question about specific syntax, it belongs on StackOverflow. I have already marked the question for migration, so there is nothing you need to do to get it there. –  Bart van Ingen Schenau Jan 22 '14 at 11:21
    
I found two very clear answers. Thanks. –  Duli Jan 23 '14 at 8:49

2 Answers 2

up vote 2 down vote accepted

I actually find this question interesting :)

My take is that this is a bad practice today in PHP. That kind of syntax is typical of Python which has a feature called "named parameters" that allows calling a function without using the same order of variable calls.

There is an RFC in the php community to add this syntax to the language: https://wiki.php.net/rfc/named_params

The internal discussion about this feature is not finished yet, personally I would like to have this feature.

As of today (PHP 5.5), you get no benefit at all in using this syntax as you are just evaluating the variable.

share|improve this answer

Is there any reason to assign the $str variable inline like that? Does it improve clarity at all?

I believe it's just evaluating $str = 'test' and passing the result $str to the function call.

$str remains assigned in the current scope so you could continue to use $str after the test() call if you wanted to.

It's the same as

$str = 'test';
test($str);

It's a bit weird looking and may be misleading when reading the code later so I would avoid doing that.

share|improve this answer
1  
Actually I never pass parameters to function like this before. But Today I get an requirement to pass a parameter like this. I just worried about whether this is a unrecomanded code. Thank you for clarification. –  Duli Jan 22 '14 at 9:14

Not the answer you're looking for? Browse other questions tagged or ask your own question.