RE: [PHP] 'Code Snippets' you couldn't live without
| From: | Pablo Gosse | Date: | Wed, 03 Nov 2004 22:11:36 +0000 |
| Subject: | RE: [PHP] 'Code Snippets' you couldn't live without | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-201090@lists.php.net to get a copy of this message | ||
[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