RE: 'Code Snippets' you couldn't live without

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



[snip]Extending PHP to have "shorthand" functions? Was that
irony?[/snip]

It most certainly was irony, Klaus.

I was merely trying to illustrate the point that writing a custom
function which does EXACTLY the same thing as a built in function is
redundant.  A number of functions within php do have aliases (sizeof()
is an alias count() for example) but these are throwbacks to equitable
functions in other languages.

As I stated earlier, if your function does more than the built in
function, then yes, you are increasing efficiency, but if all you're
doing is creating a custom alias of which only you will know the useage,
then you only increase obscurity.

As for my favorite piece of reusable code, I hate preparing dates for
insertion into a database when I have to deal with null values, etc.  So
I use a function which takes an array of date field names and a regular
expression, then validates $_POST['field name'] against the regular
expression and returns an array, $dates, each field of which contains
either a date prepared for insertion (wrapped in single quotes) or
simply the string 'null'.

function _formatPostDates($dateArr,$regex)
{
	$dates = array();
	foreach ($dateArr as $date) {
		$dates[$date] = preg_match($regex,$_POST[$date]) ?
"'{$_POST['release']}'" : "null";
	}
	return $dates;
}

On a page where I have to deal with multiple date fields, this greatly
increases the efficiency of validating the dates and makes things safer
and easier when creating queries.

Cheers,
Pablo

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux