title: Default keyword - optional function parameter
body: <p>When calling a PHP function with an optional parameter, how can I specify a that the optional parameter remain at it's default? Other languages have a <code>default</code> keyword that can be used - <code>substr($str, 0, default)</code>.</p>
  
  <p><strong>Example</strong>: <code>substr($str, 0, null)</code> </p>
  
  <p>I have specified <code>null</code> as the <code>$length</code> parameter, so it will return an empty string, in actual fact I wanted the whole string.</p>
  
  <p>From the <code>substr</code> documentation:</p>
  
  <blockquote>
    <p>If length is given and is <strong>0</strong>, <strong>FALSE</strong> or <strong>NULL</strong> an empty string will be returned.</p>
  </blockquote>
  
  <p>So in this case I cannot specify <code>null</code> as I usually would, I can also not specify a number.</p>
  
  <hr>
  
  <p><strong>Questions</strong></p>
  
  <ul>
  <li>What is the default value of the <code>$length</code> parameter of <code>substr</code>?</li>
  <li>Is there a way to specify the parameter as the default, i.e. a default keyword/operator?</li>
  </ul>
  
  <hr>
  
  <p><strong>Real Example</strong>:</p>
  
  <p>I want to extract the host name from a poorly formatted URL.</p>
  
  <pre><code>$url = 'example.com';
  
  if(!$host = parse_url($url, PHP_URL_HOST))
  {
      //There is no URL protocol so parse_url won't work
      $host = substr($url, 0, strpos($url, '/') ?: null);
  }
  
  echo $host;
  </code></pre>
  
  <p>In the above example <code>$host</code> is an empty string, because <code>$url</code> does not contain any forward slashes (<code>/</code>). If you add a slash to the end of <code>$url</code> then it works fine (<code>$url = 'example.com/'</code>).</p>
  
  <p>I realize that I could merely use an <code>if</code> clause instead of a ternary, but I wondered if there was any way to send a default parameter instead of <code>null</code>.</p>