Pages

Friday, April 17, 2009

Checking PHP Syntax from VIM

I use vim/gvim for my text editor and I found a handy way to quickly check PHP syntax using a couple of keystrokes from within the editor. Add this to your vimrc file:

" Check php syntax on the current file with CTRL+L
autocmd FileType php noremap <C-L> :!/usr/bin/php -l %<CR>

As an added bonus, you can also use a key mapping to run the execute the PHP file as well, though I find this less useful on a daily basis.

" Run php cli on the current file with CTRL+M
autocmd FileType php noremap <C-M> :w!<CR>:!/usr/bin/php %<CR>

You can change the key used to trigger the command, but you have to be careful not to interfere with pre-existing key combinations.

Shorten Your Ifs

Here’s a PHP example for shortening your conditionals and parameter checking. (There is probably a design pattern for this, but I haven’t found a name for it yet. Submit a comment if you know the name.)

This is a typical PHP example.

/* A useful function.

@param $arg1 First parameter.
@param $arg2 An optional array.

@return Returns a useful string.

*/
public function doSomethingUseful($arg1,$arg2=array())
{
    if (isset($arg2))
    {
        foreach ($arg2 as $v) { /* ... */ }
    }
    else
    {
        return '';
    }
}

Here’s a way to shorten and simplify.

/* A useful function.

@param $arg1 First parameter.
@param $arg2 An optional array.

@return Returns a useful string.

*/
public function doSomethingUseful($arg1,$arg2=NULL)
{
    if (is_array($arg2)) { return ''; }
    foreach ($arg2 as $v)
    {
        /* ... */
    }
}

Friday, April 3, 2009

When MySQL Saved the Day

Although I must grudgingly admit that MySQL has improved over the years, I still think it is a toy database. Rants (both valid and unreasonable) on the vagaries of MySQL abound, so I won't duplicate any of those comments here. Instead, I will relate a recent experience on when MySQL "saved the day".

Once upon a time there was an orders database storing order data and ordered items in MySQL without InnoDb. Someone deleted a really large order and all seemed lost. The order itself could be easily recreated but the list of ordered items was extensive.

But MySQL's lack of referential integrity meant that the ordered items were still present in a separate table. A quick query or two and all of the ordered items were available again.

Message to Elancers

I recently placed a project up for bid on elance.com. As guy involved in sales, marketing, and programming, it's hard for me to give up control of a project. But I needed someone else's time and skill and was willing to trade some money for both.

When the bidding had run its course I selected a winning bidder and since I am involved in the same business as the Elance bidders, and I am not the typical project owner on elance.com, I thought I would send those that didn't win a note on how to do better at sales. I am sharing it here in case it has any useful nuggets on sales and bidding in the world of software and websites.

All,

Thank you for your participation. This project is now closed and I have selected a winning bidder. As you may know, I am a software developer running a family-owned shop. My focus is Web development. I do sales, marketing, and most of the development work. It is important that I do all of these things well if I want to pay my bills and take care of my family.

It is important for you as well, and I wanted to take a moment to offer some unsolicited advice for those of you that did not win the job, so that you might be able to win the next job you bid.

1. I have lots of experience in bidding on sites like elance. The typical employer is unorganized, unprepared, and unaware of the complexity and cost of software development. They are also not qualified to know whether you are a good programmer or not. I am NOT the typical employer. But for most prospects (myself included) it simply boils down to this: Which provider presents the least amount of risk to my business? As both an employer and a contractor, I believe I gave you better and more complete information than you are likely to receive from most employers. Yet

- Many of you submitted a standard "proposal" and had obviously not read any of the information I had provided.

- Some of you said you would follow up after reviewing the information and that was the last contact I saw.

- Some of you bid the maximum bid. Keep in mind that when employers select a budget, it is a range of numbers. Bidding the maximum may put you out of the running immediately.

2. Like most employers, I had a limited amount of time to review your bids. So, I was only interested in those providers that:

- contacted me early,

- had obviously read the information I provided,

- asked meaningful questions,

- offered real advice.

3. Lastly, I view sales as a means of helping people rather than making money. "Sales" for me is matching the right solution for the client. It is the Biblical principal of "doing unto others as you would have them do unto you." Clients feel vulnerable in many ways. Show them that you are trying to help and not just trying to make money. I know that bidding on a site like elance is hectic and hard to manage. It's easy to submit some boiler plate text, some examples of your work, and then move on to the next bid. My advice is the "three B's":

- Be helpful. Ask questions and offer advice.

- Be available. For client questions, advice, etc., but take care not to leave everything up to the client. Following up is important.

- Become a partner. Show that you are interested in mutual benefit and relationship.

Been There Developer Selected

I have selected a winning bidder for the location aware application I'm calling Been There. It was a tough decision as there were lots of good bids and developers. It was also interesting to be the customer for once!

Monday, March 16, 2009

About "Us"

Gollumn

Who's "Us", Precious?

I have been in the technology business for a long time and I have seen a lot of "businesses" come and go. The unique and wonderful thing about the early days of the Web, and to some extent it still applies, is that you could just "hang out a shingle" (that is, a Web site), and be as competitive as the next guy. In theory at least.

The idea that the Web "leveled the playing field" was sort of a valid one, though a guy selling computers from his garage was decidedly not on the same playing field as Dell, HP, or Gateway at the time. I say sort of because the playing field was not actually leveled, it's just that everyone had the perception of business equality on the Web. And what was this perception based upon?

Ignorance.

Someone could create a Web site with the appearance of a full-blown clothing store. Of course, a brick and mortar store is assumed to exist, but this is not necessarily so. To this day, Web site visitors have no way of knowing at a glance if a random site belongs to an establish company or is just one guy in a basement.

This ignorance has engendered a verbal fiction, especially with one-man development shops that I find really annoying. About "Us".

To "compete" and "level the playing field", small development shops (and other business types) try to look big, and in doing so, have created some sort of business schizophrenia.

I Yam what I Yam

I am a firm believer in being who you are. It is disingenuous to represent yourself as something that you're not, whether it's in business or in personal life. I recently updated my Web site and though my wife is a big part of "our" business, and I do work with a number of sub-contractors, I decided to continue with the "I" instead of the "web".

Additionally, there advantages to "being small". I have worked in companies much larger than mine and the ability to determine direction and in some cases make simple decisions is greatly hindered by the size of the company.

Anyone interested in more on the topic should great Chapter 3 of Getting Real, starting with "Less Mass". (Getting Real has been required reading for my staff for a couple of years now.)

Monday, March 2, 2009

Have a Blank PHP Page?

So you're coding a PHP page, you switch to your browser and hit refresh, and you get -- nothin'. No error message, blank page, and depending upon your error settings, you may not see anything in the logs. You have display_errors turned on, so what's going on?
If you're using PHP 5.x, it may be a combination of two  things:
  1. You have white space somewhere.
  2. Output buffering is set to 4096 by default.
What's happening is that some sort of error has occurred in your code (or you have whitespace in an include before a header call) but the error output is being buffered by the output_buffering setting new to php.ini in 5.x and the error is being hidden.
If you have logging turned on, you would still see there error there, but this can otherwise be misleading.