title: PHP - What's the best way to handle empty function/method arguments?
body: <p>I have the function/method below inside a class that I'm creating and I'm just wondering what's the best way to handle empty/null arguments.</p>
  
  <p>For example, in the following example, if I wanted to just set just the category when calling the function, I would need to use:</p>
  
  <p><code>$data = $class-&gt;get_top_headlines( null, 'technology' );</code></p>
  
  <p>Is there any way of calling the function more efficiently? I know I could pass the arguments as an array instead, but just wondered if there's any way of doing something like: </p>
  
  <p><code>$data = $class-&gt;get_top_headlines( $category='technology' );</code> and automatically leaving the other arguments as their default of <code>null</code>?</p>
  
  <pre><code>public function get_top_headlines( $query=null, $category=null, $country=null, $sources=null, $page_size=null, $page=null ){
      $url = $this-&gt;api_url . $this-&gt;endpoint_top_headlines;
      $params = array();
      if ( $query !== null ){
          $params['q'] = urlencode( $query );
      }
      if ( $category !== null ){
          $params['category'] = $category;
      }
      if ( $country !== null ){
          $params['country'] = $country;
      }
      if ( $sources !== null ){
          $params['sources'] = $sources;
      }
      if ( $page_size !== null ){
          $params['pageSize'] = $page_size;
      }
      if ( $page !== null ){
          $params['page'] = $page;
      }
      $params['apiKey'] = $this-&gt;api_key;
  
      $url_query = http_build_query( $params );
      $url = $url . '?' . $url_query;
      echo $url;
      $obj = $this-&gt;get_response( $url );
      return $obj;
  }
  </code></pre>