1

Is this not meant to work in PHP?

<?php
function shout($mute="", $message="") {
    echo $message;
}

shout($message = "Boo");
?>

I know this is a poor example, but it gets my point across.

3 Answers 3

1
<?php
function shout($mute="", $message="") {
    echo $message;
}

shout(null, "Boo"); //echo's "Boo"
?>

You have to pass in the correct parameters in the correct order.

Sign up to request clarification or add additional context in comments.

Comments

1

No this won't work, function parameter order is strict and cannot be manipulated.

You can either do this:

shout(null, 'Boo');

Or re-factor your function to accept an array:

function shout($args) {
    echo $args['message'];
}

$args = array('message' => 'boo');
shout($args);

1 Comment

($message = 'Boo') == 'Boo'. This means, that he will set the local variable $message, but will pass the value 'Boo' as the first argument ($mute).
0

in php passing function parameters is conventional,

suppose you have a function definition like this function myfunc($arg='something or null' , $arg) this is the wrong way as we have to put our constants on right side of the function like this

function myfunc($arg, $arg='something or null'){}

when you are calling function make sure you are passing correct arguments i.e myfunc('test')

if you described $arg='null' then you dont need to pass anyhing from your function call because null is a value whereas empty string is nothing.

so in your case you must do like this function yourfunction('value1','value2')

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.