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.

8
  • 1
    What is your problem? Commented Nov 28, 2013 at 16:28
  • No. But there are rumors of named parameters coming in PHP 5.6 which would help solve this problem. Commented Nov 28, 2013 at 16:29
  • I don't think I understand your question. What do you want the output to be? Commented Nov 28, 2013 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. Commented Nov 28, 2013 at 16:31
  • 2
    @AlimonKarimPito In that case how would you pass in only the first parameter? Commented Nov 28, 2013 at 16:37

4 Answers 4

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

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

1 Comment

You could use my tiny library ValueResolver in this case to do it more compact
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)

2 Comments

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
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.
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

3 Comments

How can I make it 70+20=90 That means how can I use 1st default value ?
You could swap your two parameters in your function.
@AlimonKarimPito : I edited my answer with a way that may satisfy you.
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);
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.