1

In php default function have any solution for below code?

function sum($num1=20,$num2=10)
{
     $sum=$num1+$num2;
     echo $sum;
}
sum('',40);

Here the problem is first default value is not working.For example if I write sum(40); It will give me ans 40+10=50 but I want the ans will be 40+20=60. Need to use 1st default value.

cms
  • 106
  • 8
  • 1
    What is your problem? – Leonardo Nov 28 '13 at 16:28
  • No. But there are rumors of named parameters coming in PHP 5.6 which would help solve this problem. – John Conde Nov 28 '13 at 16:29
  • I don't think I understand your question. What do you want the output to be? – Amal Murali Nov 28 '13 at 16:30
  • I think the issue is that calling a function as sum('',40) sets the value of $num1 as being null rather than the default of 20. Solution would be to use a little class + construct though named params would be handy. – Dave Nov 28 '13 at 16:31
  • What is the rumors of named parameters?Here the problem is first default value is not working.For example if I write sum(40); It will give me ans 40+10=50 but I want the ans will be 40+20=60. Need to use 1st default value. thank you for your comment. – cms Nov 28 '13 at 16:35
  • 2
    @AlimonKarimPito In that case how would you pass in only the first parameter? – Jim Nov 28 '13 at 16:37
  • @AlimonKarimPito, http://stackoverflow.com/questions/690599/any-way-to-specify-optional-parameter-values-in-php – Valery Statichny Nov 28 '13 at 16:56
  • You could use my tiny library [ValueResolver](https://github.com/LapaLabs/ValueResolver) in this case to do it more compact, for example `ValueResolver::resolve($someVariable, $default);` – Victor Bocharsky Jul 09 '15 at 10:47

4 Answers4

2

Arguments in function calls in php are assigned from left to right. So, you can't really skip the first argument and give just the second one. This is the way function calls work in php and you can't change it.

However, you can get around this issue by giving the default values inside the function body.

First check if any argument is null and then assign it a value like this :

<?php

function sum($num1,$num2)
{
    //giving the arguments default values
    $num1==null ? $num1 = 20 : null;
    $num2==null ? $num2 = 10 : null;

    $sum=$num1+$num2;
    echo $sum;
}

sum(null, 40); // echoes 60
sum(40, null); // echoes 50

?>
Kevin
  • 6,539
  • 5
  • 44
  • 54
1

If your question is about the default value of parameters in PHP functions, it doesn't work that way.

function sum($num1=20,$num2=10)
{
     $sum=$num1+$num2;
     echo $sum;
}
sum('',40);

Your function sum requires two parameters. You made these parameters optional by adding default values. These default values are taken into account if there is no parameter.

This means that

sum();

works, and will use both your default parameters. It will echo the result of 10 + 20, ie. 30.

Now you can try

sum(70);

This will echo 80, because your first parameter is defined ( 70 ) and the second isn't, therefore $num2 takes the default value, which is 10.

In addition to that, you can't input a value for the second parameter only and have the first parameter using the default value.

EDIT : I did a quick research and found a good way to make this work : PHP Function with Optional Parameters

Community
  • 1
  • 1
Theox
  • 1,363
  • 9
  • 20
1

This is not elegant, but should work:

function sum($num1=null,$num2=null)
{
    $num1 = ( empty($num1) || is_null($num1) ) ? 20 : $num1;
    $num2 = ( empty($num2) || is_null($num2) ) ? 10 : $num2;

    $sum = $num1 + $num2;
  echo $sum;
}

sum('', 40)
Babblo
  • 794
  • 5
  • 17
  • My problem is I want to use 1st default value. for example if I write sum(40); that will give me ans. 40+10.That means it have used 2nd default value.I want to use 1st value. how can I use 1st de – cms Nov 28 '13 at 16:44
  • `empty($num1) || is_null($num1)` is redundant. This unexplained answer should not be used by researchers. `echo ($num1 ?: 20) + ($num2 + 10);` this does the same thing. But be careful, this overwrites `0` values just like in the answer. – mickmackusa Oct 26 '20 at 05:30
0

This will work just fine:

function sum($num1 = null, $num2 = null)
{
    $nums = array_filter(array($num1, $num2, 20, 10));
    echo array_shift($nums) + array_shift($nums);
}
evalarezo
  • 1,134
  • 7
  • 13