0

I have a function:

function passParams($a, $b = null, $c)
{
    echo "<pre>"; var_dump($a); echo "<br/>"; die('123');
}

and I want to call it like this:

passParams($a = 10, $c = 1);

How do I have to?

0

3 Answers 3

2

Try this...

<?php
function addFunction($num1, $num2,$test=NULL)
{
  $sum = $num1 + $num2;
  echo "Sum of the two numbers is : $sum";
}
addFunction(10, 20,NULL);
?>

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

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

1 Comment

yes. I know that, but now I want to call the function with 2 params, the rest of param will be set default value when it isn't passed
1

Define parameters with default values at the end of function arguments.

function passParams($a, $c, $b = null)
{
    echo "<pre>"; var_dump($a); echo "<br/>"; die('123');
}

And call it by -

passParams(10, 1);

Then -

$a will be - 10
$b will be - NULL
$c will be - 1

2 Comments

Thanks. so we have to call by order of parameters?
Yup. The order will set them accordingly.
0

You can always call like this:

passParams(10, NULL, 1);

This way 2nd parameter will remain its default value as specified in the function

1 Comment

I want to call the function with 2 params, the rest of param will be set default value when it isn't passed

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.