Search code examples
phpfunction-parameter

Default NULL in function declaration


Why do we need declare $slug argument as default NULL in the function? What will change if we won't declare it default NULL?

It looks for me like nothing would change:

public function view($slug = NULL)
{
    $data['news_item'] = $this->news_model->get_news($slug);

    if (empty($data['news_item']))
    {
        show_404();
    }
    ...

Solution

  • You have the option of setting a default value (NULL in your case), which then makes that argument optional, rather than required. That is why it looks to you like it is unnecessary, you probably only call this function with an argument.

    You can look this up here.