Monday, January 22, 2007

PHP Functions

I've been using PHP for a little over a year now. I ended up redoing my website (www.chrisbensen.com) entirely with PHP over the last holiday and I couldn't be happier. It is still a work in progress but at this point I haven’t used any PHP packages for a few reasons. Here were my personal reasons for my choice to write basically everything from scratch:

1. Both my wife’s website and my website soon share the same PHP backend, and it had to be dead simple to update the photo gallery.
2. In the past when I have had to change hosts and my website required a particular package to be installed it was a lot of work to migrate everything. I also do my photography on a Mac and that is the easiest place to build my website. I’ve also found that PHP on Windows is lacking. So I wanted to rely on the lowest common denominator. This also excluded PHP 5.
3. Performance. I’ve found that some photo galleries are really slow and really difficult to tie to a shopping cart.
4. Photo galleries and shopping carts are difficult to tie together.

In doing everything from scratch I’ve come across many things that are lacking in the PHP library. One of the functions I’ve come to rely on is ChangeFilePath and ChangeFileExtension. Here are both these functions as I’ve implemented them.


function ChangeFilePath($FileName, $Path)
{
$name = basename($FileName);

if ($Path[strlen($Path) - 1] != '/')
return "$Path/$name";

return "$Path$name";
}

function ChangeFileExtension($FileName, $Extension)
{
$index = 0;

for ($index = strlen($FileName) - 1; $index >= 0; $index--)
{
$c = $FileName[$index];

if ($c == '.')
{
$index--;
break;
}
}

$temp = "";

for (; $index >= 0; $index--)
{
$c = $FileName[$index];
$temp = "$c$temp";
}

return "$temp$Extension";
}


I know they might not be the most optimal, but I searched for 20 minutes and didn’t find anything with Google or php.net so I wrote them both in a few minutes and moved on. I find it odd with such an open source web language that two functions as simple as changing the path or the file extension are so difficult to come by. Does anyone know of a good resource for PHP programmers?

2 comments:

Jason Sweby said...
This comment has been removed by a blog administrator.
Oscar said...

One site I've found very useful is http://www.phpclasses.org/. Give it a try.

Post a Comment