This post is archived and probably outdated.

Improved getopt() in PHP 5.3

2008-12-22 20:03:00

So PHP 5.3 has lots of new stuff offer, so let's take a look at one change:

Added long-option feature to getopt() and made getopt() available also on win32 systems by adding a common getopt implementation into core. (David Soria Parra, Jani)

PHP's focus is clearly in the Web but using the CLI SAPI you can use it for command line scripts, too. A common task to every command line script is reading parameters from the command line. PHP offers a getopt() function for some time which is based on the getopt() C function provided by the operating system. The problem there is that all of these behave a little bit different and some systems, like Windows, don't offer getopt() at all, so PHP's function is disabled there, too. David now sat down and used an implementation of getopt, which was actually already used y the CLI SAPI itself, and implemented PHP userland getopt() on top of it. The benefits are stated in the above NEWS entry. Examples can, like in the previous articles of this series, found in the fine manual.