PHP

Python-like tuple unpacking for PHP

Python provides a neat way for functions to return multiple arguments via "tuple unpacking". For example:

def blah:
  return ('one', 'two')

rval_1, rval_2 = blah()

The same can be done in PHP relatively easily via the list construct:

function blah()
{
  return array('one', 'two');
}

list($rval_1, $rval_2) = blah();

A quick domain HTTP permanent redirect script for Apache/PHP

I mentioned earlier 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 HTTP status 301 (permanent redirect) to anything that tries to access those domains. It would send them to this current domain (samat.org).

And I did. So, behold the script (save as index.php):

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 directive:

<VirtualHost 72.36.165.250:80>
  ServerName tamasrepus.hotnudiegirls.com
  DocumentRoot "/path/to/directory/containing/script"
  ErrorDocument 404 /index.php
</VirtualHost>

PHP and passing non-variables by reference

In PHP 5.0.5, they've now made it a requirement 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's been allowed for all versions of PHP previous. Appararently without even warnings. You'll get an error:

Fatal error: Only variables can be passed by reference ...

So you may think, where is this useful? Consider something short and concise like:

$only_element_we_care_about = array_pop(explode($seperator, $string));

You cannot do this now. The return value of a function is not a "variable" and cannot be passed by reference; a temporary variable needs to be used instead:

$tempory_variable = explode($seperator, $string); $only_element_we_care_about = array_pop($temporary_variable);

Yes, there are other ways to do the above, but that isn'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 this.

Remind me to find another web programming language.

Syndicate content