phup 'n stuff

Another Blog About Being a Developer

Posts Tagged ‘PHP’

PostgreSQL arrays and PHP’s str_getcsv()

Friday, March 5th, 2010

Yesterday, while trying to figure out the best way to deal with PostgreSQL arrays in PHP, I came across the new str_getcsv() function in PHP as of 5.3. This function works much the same as fgetcsv to parse a CSV line, except that it works on a string instead of a file.

For quick reference, CSV looks like this:

"My Name",14,"32,000","2009-04-15"

And the return value of a PostgreSQL array looks like this:

{"My Name",14,"32,000","2009-04-15"}

Notice the similarities?

We can use PHP's trim and str_getcsv to turn this PostgreSQL array into a PHP array:

<?php
    $data = str_getcsv(trim($record->value, "{}"));
?>

Simple as simple does. As long as your array has only a single dimension. If you're using multidimensional arrays in PostgreSQL then you're dead to me.

Omitting the Closing tag in PHP Files

Wednesday, February 24th, 2010

At first when I saw the Zend Framework recommendation of omitting the closing tag in PHP files I thought it bizarre and stupid. Afterall, you close every other syntactic character in programming: parenthesis, brackets, HTML tags, ifs, loops, etc. So why wouldn't you close the PHP tag at the end of the document as well?

The Zend Framework reason is, of course, to white space data from being sent to the browser prematurely, which messes up sending header information to the browser. My first thought was "this is lazy and encourages bad coding."

But the more I think about it, the more it makes sense. It isn't laziness, but a fool proof way to prevent a common mistake from happening. And apparently the closing tag isn't required at all, so why not?

I don't think I'll be quick to adopt this practice, but maybe eventually, and maybe as I write new code. We'll see.

The Sluggish Speed of Gentoo

Wednesday, January 20th, 2010

Gentoo, ffs, get PHP 5.3.1 in Portage ASAP. PHP 5.3 isn't even in there, yet. What's the use of your package manager if I just have to manually install stuff?

/rant

User Time Zones in PHP

Friday, January 15th, 2010

When writing applications that are meant to be used by users the world over (not just users within a localized network), it's important to be able to show dates and times relative to the users time zone. If one user posts something at 11:30 EST, and a few minutes later, a user in California checks the post, it would look as if it were made in the future. Not cool.

Lucky, it's quite easy to localize dates and times using PHP's built in date classes.

(more…)

Using Flowplayer and ffmpeg to Stream Video

Sunday, January 18th, 2009

We recently had a client that had a need for hosting and streaming videos from their website. These videos were public domain videos, some found on YouTube, NASA's website, and other resources, and the client was concerned that the videos might be taken down or moved, so they were insistent on hosting the videos on their site. There was also a desire to not require users to have to install Windows Media Player, Quick Time, Flash to be able to view all videos.

(more…)

PHP’s Magic Quotes: Taking Control of Your Data

Sunday, April 15th, 2007

Magic quotes can be the bane of any PHP programmer's existence. The feature is intended to be helpful and ward off possible SQL injections, but when you're developing open-source applications, you never know who's going to have it on. So do you code for magic quotes or not? All politics aside, I'm going to say not. But it is possible to cater to servers with it both on and off.

(more…)