0

I'm kinda confused about optional parameters, or setting a parameter within the function in general. So I found this example:

<?php
function makecoffee($type = "cappuccino")
{
    return "Making a cup of $type.\n";
}
echo makecoffee();
echo makecoffee(null);
echo makecoffee("espresso");
?>

and I don't understand why echo makecoffe("espresso") returns espresso and not cappuccino. Wouldn't espresso be overwritten when calling the method? In my head $type would first be espresso but would then be set to cappuccino since it happens after calling the method. It's kind of hard to explain what I mean, but does $type = "cappuccino" only get called when there's no parameter or why does this happen? Same with

function makecoffee($type = null)
    {
}

echo makecoffe("espresso");

why would this return espresso and not null even though type would be set to null when calling the function?

Rugo
  • 349
  • 1
  • 6
  • 14
  • 4
    that sets a *default* parameter value. Not it's value – treyBake Sep 20 '19 at 14:11
  • 2
    If a value is passed into the function, then that value is used, no matter if it's a string, integer, null, array, etc. The default value is used **only** when no value is passed in at all. – aynber Sep 20 '19 at 14:14
  • 1
    Think about it like this: What would be the point of adding a function argument that couldn't be changed? If you want that, just define the variable in the function body instead. – M. Eriksson Sep 20 '19 at 14:18
  • 1
    The function will use the default value only when no other value is passed to it. – Otavio de Souza Rocha Sep 20 '19 at 14:25
  • Actually, it's contrariwise. If there is no value - default value is used. If parameter is set - it will override the default value. – muinh Sep 20 '19 at 15:35

2 Answers2

1

Good example to inform you how exactly the php parameters in functions works, would be the func_get_args() which returns all passed arguments to function.

And this will be our example function.

function foo($example = 'default')
{
    print_r(func_get_args());
}

Now we can check if any argument was passed to function.

What happens if we don't pass any argument?

foo();

// Output
Array
(
)

We know that function was called without any argument because output array is empty. And that is the moment where default value is used for our function parameter. So finally the value for $example parameter will be equal to 'default'

Now we will try to pass 2 arguments to function.

foo('example', 'unnecessary');

// Output
Array
(
    [0] => example
    [1] => unnecessary
)

From this example we can deduce that we can pass any amount of arguments without seeing an exception. This is one of php features.

But which was passed to $example parameter?

As php docs informs.

The arguments are evaluated from left to right.

The left side for our example is the value from an array with index 0 and the right side is the last element with (actually index 1)

So, under $example we well have 'example' value.

In defaults, the second argument will be ignored because our function doesn't have corresponding second parameter.

What if we pass null as a argument?

This may be tricky because we can imagine that function called with null as a argument will affect on our function as function called without any arguments.

Nothing more wrong.

foo(null);

// Output
Array
(
    [0] =>
)

Now you should see that. Despite the fact that our argument was null, value was passed into our function and overwrited default 'example' value for parameter. Finally $example parameter will be equal to null.

Maciej Król
  • 382
  • 1
  • 8
0

That's because the $param = 'value' bit in the function declaration is not executed every time the function is called.

It only comes into play if you don't pass a value for that parameter.

Instead of reading it as a literal assignment PHP does something along the lines of the following under the hood whenever it enters your function.

if true === $param holds no value
    $param = 'value'
endif

In other words, $param = 'value' is not a literal expression within the context of the language but rather a language construct to define the desired behaviour of implementing fallback default values.

Edit: Note that the snippet above is deliberately just pseudo code as it's tricky to accurately express what's going using PHP on once PHP has been compiled. See the comments for more info.

Bananaapple
  • 2,984
  • 2
  • 25
  • 38
  • 3
    Just a correction: Passing a `null`-value would give it a `null`-value so it's not like `null === $param`. – M. Eriksson Sep 20 '19 at 14:22
  • Good point! I'll try to update my answer to somehow explain this without drifting off into how that's awkward to check for in php (https://stackoverflow.com/questions/418066/best-way-to-test-for-a-variables-existence-in-php-isset-is-clearly-broken) – Bananaapple Sep 20 '19 at 14:36