<p>I'm trying to do this (which produces an unexpected T_VARIABLE error):</p>

<pre><code>public function createShipment($startZip, $endZip, $weight = 
$this-&gt;getDefaultWeight()){}
</code></pre>

<p>I don't want to put a magic number in there for weight since the object I am using has a <code>"defaultWeight"</code> parameter that all new shipments get if you don't specify a weight. I can't put the <code>defaultWeight</code> in the shipment itself, because it changes from shipment group to shipment group. Is there a better way to do it than the following?</p>

<pre><code>public function createShipment($startZip, $endZip, weight = 0){
    if($weight &lt;= 0){
        $weight = $this-&gt;getDefaultWeight();
    }
}
</code></pre>

[{'answer_id': 1456, 'author': 'Kevin', 'author_id': 40, 'author_profile': 'https://Stackoverflow.com/users/40', 'pm_score': 5, 'selected': True, 'text': "<p>This isn't much better:</p>\n\n<pre><code>public function createShipment($startZip, $endZip, $weight=null){\n    $weight = !$weight ? $this-&gt;getDefaultWeight() : $weight;\n}\n\n// or...\n\npublic function createShipment($startZip, $endZip, $weight=null){\n    if ( !$weight )\n        $weight = $this-&gt;getDefaultWeight();\n}\n</code></pre>\n"}, {'answer_id': 2213, 'author': 'pix0r', 'author_id': 72, 'author_profile': 'https://Stackoverflow.com/users/72', 'pm_score': 1, 'selected': False, 'text': '<p>This will allow you to pass a weight of 0 and still work properly.  Notice the === operator, this checks to see if weight matches "null" in both value and type (as opposed to ==, which is just value, so 0 == null == false).</p>\n\n<p>PHP:</p>\n\n<pre><code>public function createShipment($startZip, $endZip, $weight=null){\n    if ($weight === null)\n        $weight = $this-&gt;getDefaultWeight();\n}\n</code></pre>\n'}, {'answer_id': 31495, 'author': 'paan', 'author_id': 2976, 'author_profile': 'https://Stackoverflow.com/users/2976', 'pm_score': 1, 'selected': False, 'text': "<p>You can use a static class member to hold the default:</p>\n\n<pre><code>class Shipment\n{\n    public static $DefaultWeight = '0';\n    public function createShipment($startZip,$endZip,$weight=Shipment::DefaultWeight) {\n        // your function\n    }\n}\n</code></pre>\n"}, {'answer_id': 31831, 'author': 'Michał Niedźwiedzki', 'author_id': 2169, 'author_profile': 'https://Stackoverflow.com/users/2169', 'pm_score': 3, 'selected': False, 'text': '<p>Neat trick with Boolean OR operator:</p>\n<pre><code>public function createShipment($startZip, $endZip, $weight = 0){\n    $weight or $weight = $this-&gt;getDefaultWeight();\n    ...\n}\n</code></pre>\n'}, {'answer_id': 56441212, 'author': 'Ivan Castellanos', 'author_id': 800817, 'author_profile': 'https://Stackoverflow.com/users/800817', 'pm_score': 0, 'selected': False, 'text': "<p>Improving upon Kevin's answer if you are using PHP 7 you may do:</p>\n\n<pre><code>public function createShipment($startZip, $endZip, $weight=null){\n    $weight = $weight ?: $this-&gt;getDefaultWeight();\n}\n</code></pre>\n"}]
['https://Stackoverflow.com/questions/1453', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/58/']