PHP http://blog.samat.org/taxonomy/term/9/0 en Python-like tuple unpacking for PHP http://blog.samat.org/2008/10/29/python-tuple-unpacking-php <div class="field field-name-body field-type-text-with-summary field-label-hidden"><div class="field-items"><div class="field-item even" property="content:encoded"><p>Python provides a neat way for functions to return multiple arguments via &#8220;tuple unpacking&#8221;. For&nbsp;example:</p> <p><pre><code class="python">def blah: return ('one',&nbsp;'two')</p> <p>rval_1, rval_2 = blah() </code></pre></p> <p>The same can be done in <span class="caps">PHP</span> relatively easily via the <a href="http://php.net/list">list construct</a>:</p> <p><pre><code class="php">function blah() { return array('one', 'two');&nbsp;}</p> <p>list($rval_1, $rval_2) = blah(); </code></pre></p> </div></div></div><div class="field field-name-taxonomy-vocabulary-1 field-type-taxonomy-term-reference field-label-inline clearfix"><div class="field-label">Topic:&nbsp;</div><div class="field-items"><div class="field-item even"><a href="/tag/Programming" typeof="skos:Concept" property="rdfs:label skos:prefLabel">Programming</a></div><div class="field-item odd"><a href="/tag/Python" typeof="skos:Concept" property="rdfs:label skos:prefLabel">Python</a></div><div class="field-item even"><a href="/tag/PHP" typeof="skos:Concept" property="rdfs:label skos:prefLabel">PHP</a></div></div></div> Wed, 29 Oct 2008 21:55:47 +0000 Samat Jain 148 at http://blog.samat.org A quick domain HTTP permanent redirect script for Apache/PHP http://blog.samat.org/2006/03/16/quick-domain-http-permanent-redirect-script-for-apache-php <div class="field field-name-body field-type-text-with-summary field-label-hidden"><div class="field-items"><div class="field-item even" property="content:encoded"><p><a href="/weblog/2006/02/27/changing-domains-again--welcome-to-samat-org">I mentioned earlier</a> that to get my old domains, tamasrepus.hotnudiegirls.com and tamasrepus.rhombic.net delisted from search engines, I was going to write a redirection script that would send an <span class="caps">HTTP</span> status 301 (permanent redirect) to anything that tries to access those domains. It would send them to this current domain&nbsp;(samat.org).</p> <p>And I did. So, behold the script (save as&nbsp;index.php):</p> <p><code type="php"> <? $headers = array(); $headers[] = "HTTP/1.1 301 Moved Permanently"; $headers[] = "Location: http://samat.org" . $_SERVER['REQUEST_URI']; foreach($headers as $header) { header($header); } ?> </code></p> <p>Simple enough. More is needed, though: the web server needs to be told to use this script for all URLs. You can do this easily with Apache and the ErrorDocument&nbsp;directive:</p> <p><code> <VirtualHost 72.36.165.250:80> ServerName tamasrepus.hotnudiegirls.com DocumentRoot "/path/to/directory/containing/script" ErrorDocument 404 /index.php </VirtualHost> </code></p> </div></div></div><div class="field field-name-taxonomy-vocabulary-1 field-type-taxonomy-term-reference field-label-inline clearfix"><div class="field-label">Topic:&nbsp;</div><div class="field-items"><div class="field-item even"><a href="/tag/PHP" typeof="skos:Concept" property="rdfs:label skos:prefLabel">PHP</a></div><div class="field-item odd"><a href="/tag/Apache" typeof="skos:Concept" property="rdfs:label skos:prefLabel">Apache</a></div></div></div> Thu, 16 Mar 2006 09:14:07 +0000 Samat Jain 79 at http://blog.samat.org PHP and passing non-variables by reference http://blog.samat.org/2005/09/14/php_and_passing_non-variables_by_reference <div class="field field-name-body field-type-text-with-summary field-label-hidden"><div class="field-items"><div class="field-item even" property="content:encoded"><p>In <span class="caps">PHP</span> 5.0.5, they&#8217;ve now made it a <em>requirement</em> that things that are passed by reference must now explicitly be a variable. You would think this kind of behavior is obvious, but apparently it&#8217;s been allowed for all versions of <span class="caps">PHP</span> previous. Appararently without even warnings. You&#8217;ll get an&nbsp;error:</p> <p><code> Fatal error: Only variables can be passed by reference ... </code></p> <p>So you may think, where is this useful? Consider something short and concise&nbsp;like:</p> <p><code type="php"> $only_element_we_care_about = array_pop(explode($seperator, $string)); </code></p> <p>You cannot do this now. The return value of a function is not a &#8220;variable&#8221; and cannot be passed by reference; a temporary variable needs to be used&nbsp;instead:</p> <p><code type="php"> $tempory_variable = explode($seperator, $string); $only_element_we_care_about = array_pop($temporary_variable); </code></p> <p>Yes, there are other ways to do the above, but that isn&#8217;t the point. The fix is not difficult, but it is a total complete pain to go back to legacy code and fix things like&nbsp;this.</p> <p>Remind me to find another web programming&nbsp;language.</p> </div></div></div><div class="field field-name-taxonomy-vocabulary-1 field-type-taxonomy-term-reference field-label-inline clearfix"><div class="field-label">Topic:&nbsp;</div><div class="field-items"><div class="field-item even"><a href="/tag/Programming" typeof="skos:Concept" property="rdfs:label skos:prefLabel">Programming</a></div><div class="field-item odd"><a href="/tag/PHP" typeof="skos:Concept" property="rdfs:label skos:prefLabel">PHP</a></div></div></div> Wed, 14 Sep 2005 06:03:00 +0000 Samat Jain 55 at http://blog.samat.org