Archive for the ‘PHP’ Category

Are There Steps to Writing Code?

Tuesday, June 17th, 2008

A student sent me email to ask: “In a general sense, when writing code, is there a sequence of steps or a pattern to the logic when writing the code?”

It’s a good question. Experienced computer programmers have learned these steps—either through intuition or experience—but beginning programmers are often mystified. Once you learn the language syntax, how exactly do you use it to approach a programming problem? Especially if you want to try to use good programming techniques and to solve the problem in a smart and efficient way.

The answer to the question “Are there steps to writing code?” is “Yes”. After the jump, I’ll give three ways to develop the logic necessary to write good code.

(more…)

Fractions

Sunday, March 30th, 2008

Fractions

It’s been a while since my last post. I’ve been busy on a few projects with upcoming deadlines. Harder still, I am working on both a Ruby on Rails and a PHP project at the same time and switching mindsets is challenging. (I can’t count the number of times I type self when I mean $this and vice versa.) It is like traveling between two foreign countries several times a day. I speak French to the German baker and German to the French butcher. (Or, most often, I am speechless in both cases!)

For the PHP project, I need to work with fractions of an inch in a few places. Not as decimals, but as fractions (1/5, 5/12, 3/64, etc.). Fractions are not an uncommon problem for developers. Since there seem to be very few discussions online about the subject, I thought it would be worthwhile to review some of the options. My example code will be in PHP since that’s the “country” I’m in right now, but the Ruby code is easy to extrapolate.

(more…)

Taming Leopard for PHP

Wednesday, December 19th, 2007

I recently upgraded to Mac OS X Leopard. After the upgrade, MySQL was still working but PHP was not working at all. (The MySQL preference pane doesn’t work but I usually start and stop it from the command line anyway.) I discovered that there are three problems. I will briefly describe how to resolve them in case you have the same problem.

(more…)

php|works 2007

Wednesday, September 26th, 2007

I’ve been lax about blogging over the last few weeks. September is always a busy month. Everything that was put on hold during August vacations suddenly shifts into high gear. My back log of blog-worthy posts is intimidating, so I’ve decided to handle it expeditiously and keep posts short and sweet until I’m caught back up.

php|works 2007 was two weeks ago in Atlanta. Below are some of the session notes and online reviews I’ve collected.

Session Notes

Reviews and Commentary

Let me know if you find more and I’ll add them to the list.

Post-Vacation Link Dump

Tuesday, September 4th, 2007

Here’s some of the exciting things I didn’t blog about while I was on vacation last week.

PHP with MySQL Essential Training on CD-ROM

Friday, August 24th, 2007

PHP with MySQL Essential Training

I already announced the online release of PHP with MySQL Essential Training. I’m really happy that it’s been getting tons positive user feedback. It seems to be helping a lot of people understand PHP as well as web development in general and that’s very gratifying.

Now you can also purchase the CD-ROM version. It’s $149.95 for 10.75 hours of training on 2 CDs. You can purchase it from lynda.com’s store or from Amazon.com.

The nice thing about the online version is that you can access it anytime, anywhere and you’ll also get access to all the other great lynda.com titles (including my Ruby on Rails Essential Training).

Why might the CD-ROM be right for you? A few reasons come to mind: No internet connection is required so you can watch offline or even when you travel. You’ll have speedier access to higher-quality video and sound. It includes the tutorial files so you can work along with me. You’ll have it on your shelf to refer to anytime, even if you don’t maintain your lynda.com subscription (but why wouldn’t you want do that?). You can share copies around your office or school. For large organizations, lynda.com can work out multiple-copy, site license deals too. And some people like having both the offline and online versions—one for the office, one for home or on the road.

If you’ve already watched either the PHP or Ruby on Rails training found it helpful, I hope you’ll consider taking a minute to write a review on Amazon.com with your opinion. Amazon.com shoppers don’t always know Lynda.com’s great reputation and your review will help them trust that the training can be helpful to them too.

PHP 5.2.4 Errors Will Be 500 Errors

Thursday, August 23rd, 2007

Considering how old and popular PHP is, it’s hard to believe that this issue hasn’t been addressed before now…

Gregory Szorc just noticed that one of the changes to PHP 5.2.4 is that PHP errors, which used to result in an unhelpful blank page with an HTTP 200 status code, will now send an HTTP 500 status code instead. Then your web server can handle the 500 error with a default “Sorry, a server error occured” page. It’s not only much cleaner but it is the way HTTP status codes are meant to be used.

PHP 5.2.4 isn’t out just yet, but should be very soon. It’s currently being tested as a final release candidate.

Namespaces Come to PHP

Monday, August 20th, 2007

Back in July, I wrote that the PHP internals team was discussing implementing namespaces in PHP 6. Though still subject to change, the results have been commited to the official version.

Dmitry Stogov and Stanislav Malyshev’s idea is still the leading candidate (because they stepped up and fully implemented it) and but there’s been some discussion over a) whether it’s the right way to go, b) whether it is a “namespace” or actually a “package”. I don’t want to walk into the firing line of either discussion, but as I noted before I’ll cast my vote for any implementation that allows namespaces to envelope blocks of code with curly-braces. (Theirs doesn’t currently.)

If you want to better understand where namespaces may be headed there are two blog posts that can help. The first is “PHP Namespaces (Part 1: Basic usage & gotchas)” by David Coallier. The second is “Namespaces FAQ” by Stanislav Malyshev himself. I suggest reading Coallier’s first to get the basic idea.

The Zend/PHP Conference & Expo is October 8-11 in San Francisco. Both Stogov and Malyshev will be speaking. Even though there isn’t a session about it, I’d expect namespaces to be a major and lively topic of discussion at the event. Maybe a consensus and resolution will come out of it.

Lessons from the Facebook Leak

Tuesday, August 14th, 2007

Facebook Logo

If you haven’t heard already, a few pages of PHP source code for Facebook were leaked online. The site wasn’t hacked so much as a back door was left open.

Here’s what Facebook had to say:

“A small fraction of the code that displays Facebook web pages was exposed to a small number of users due to a single misconfigured web server that was fixed immediately. It was not a security breach and did not compromise user data in any way. Because the code that was released only powers the Facebook user interface, it offers no useful insight into the inner workings of Facebook. The reprinting of this code violates several laws and we ask that people not distribute it further.”

Some geeks were elated, some horrified, and some just shrugged. I fall into the third category.

It did offer a glimpse behind the wall of one of the most hyped companies of the moment and it’s always interesting to see how the industry leaders are doing things. While the code could provide details that might compromise the security of their other servers and pages, I’m guessing they are already re-engineering their setup to make any clues useless.

But this leak doesn’t mean that PHP is flawed. It doesn’t mean that any server hardware/software choice was to blame. It was misconfigured. It was human error. (“This sort of thing has cropped up before and it has always been due to human error.”)

Instead it is a cautionary tale about being thorough and vigilant. It should remind web developers and users not to assume sites are secure just because the code is good or the company is reputable. A website is only as secure as it’s weakest link. You can only take a limited amount of security for granted.

If you want to try to improve some of those weak links on your site… Nik Cubrilovic offers four tips to help prevent your server from doing the same thing. (Though a lot of people have blasted his assertion that PHP is known to sometimes return source code…) Vidyut Luther lists three more tips that can help.

PHP and mysqlnd

Monday, July 30th, 2007

Ulf Wendel at Internet Super Hero has explained what the mysqlnd driver for PHP is and why you might (or might not) need it. He also provides a nice history of the drivers and APIs that PHP uses to connect to MySQL.

In short, mysqlnd is a MySQL native driver for PHP that works like libmysql. It’s a replacement for libmysql but they can co-exist so that it doesn’t break existing applications. Primarily, mysqlnd offers improved speed, persistent connections, mysqli_fetch_all() and performance statistics calls.

In my opinion, if you are using ext/mysqli then you’ll want to check it out and see if the feature/performance trade-offs make sense for you. It’s primarily designed for advanced users who need to get every drop of performance out of PHP that they can. If you are using PDO/MySQL then you’ll want to avoid it because PDO isn’t supported yet. If you are using ext/mysql for general use (or don’t know what “ext/mysql”, “ext/mysqli” and “PDO/MySQL” are) then stick with what you’ve got—you’ll never notice the difference.