Hoang NK
Hoang NK

Reputation: 550

php how to pass parameters in function?

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?

Upvotes: 0

Views: 36

Answers (3)

Deenadhayalan Manoharan
Deenadhayalan Manoharan

Reputation: 5444

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

Upvotes: 2

Danny Broadbent
Danny Broadbent

Reputation: 1209

You can always call like this:

passParams(10, NULL, 1);

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

Upvotes: 0

Sougata Bose
Sougata Bose

Reputation: 31739

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

Upvotes: 1

Related Questions