Writing a PHP command line script that takes options

First of all, we make our file start like this.

#!/usr/bin/env php

This allows us to run the script without prefixing it with the “php” command, and instead we can run it like this:

chmod +x myscript  # This gives execution permissions to the script, do it only once
./myscript --first=option --second --third=option

If we want our script to take options like in the example above, we can use this snippet:

$options = array();
 
foreach ($argv as $arg){
	preg_match('/\-\-(\w*)\=?(.+)?/', $arg, $value);
	if ($value && isset($value[1]) && $value[1]) 
		$options[$value[1]] = isset($value[2]) ? $value[2] : null;
}

The $options array will hold the supplied options. You can then use them like this:

if (!isset($options['somevalue']))
	// show an error
 
if (isset($options['dosomething']))
	// do something

This post has 5 responses

CSS+Javascript power. Fancy menu

Let me introduce you to Fancy Menu:

fancymenu

When it comes to creating the navigation part of your Website, the first thing you might think of is an unordered list that you style as tabs. Lately, such navbars are everywhere, as many people believe they’ll make their site more Web 2.0-compatible. Personally, I just think they’re semantically better and accessible.

In this article I’ll go through the creation of a custom navigation bar with some cute Javascript effects that will certainly impress your friends. Thanks to the great Mootools library, this beauty is contained in 1.5kb. Not only that, but it’s also cross browser (tested on Internet Explorer 6/7, Firefox and Safari) and accessible!

Continue reading

This post has 676 responses

Posted in Projects over 3 years ago