Category » Programming


KeyboardPHP's greatest strength is also its greatest weakness. Flexibility. There are an infinite number of ways to perform the same task which PHP will happily do without so much as a peep as to how poor the code really is. Sadly, most developers endure a trial by fire where they only learn from their mistakes after it's too late.

I suggest a more retro-active apprach. Studying, surrounding, and forcing yourself to abide by best-practice coding standards will yield surprising results in your applications despite the fact that it may seem like more work than it's worth.

I've come up with a list of things that I feel are most important to me when it comes to coding. So, without further adieu:

BugsBugs are an inevitable part of any development project that most people loath or at least generally dislike. If you take the time to examine this phase of a project you will find that it's not the bugs that really irk you, but the way they are presented, described, and handled.

Ok, so maybe bugs are not the worst part about my job but they can be very effective at frustrating the hell out of me. After you get over the fact that you are not the world's greatest programmer and are indeed as fallible as the rest of us, you can begin to look at bug reports as an opportunity to better yourself. Learning from your mistakes is a huge part of the job and can be extremely beneficial. However, no matter how vetted any particular programmer may be, most will cringe when they see a bug drop into their inbox.

Site spam sucks, no doubt about it. I was getting tired of fighting it manually in hand-to-hand combat and decided to get some help. I'd heard about different techniques for thwarting comment, and other types of form, spam but none of them seemed to make a big enough dent in the problem for my liking. That is, until I tried Bad Behavior.

One of the most common threads that all programmers share is that of an ego. Some are much worse than others and some have found a way to control or manipulate their ego into a great benefit. Beyond skill-sets and other programming-specific talents I believe the greatest room for personal improvement in programmers as a whole is that of the ego.

A quote by GeraldWeinberg from The Psychology Of Computer Programming.

The idea is that programmers must fight the natural tendency to treat their programs as part of themselves, and therefore to reject all criticism. Rather, they should do their best to treat their designs & implementations as objects independent of themselves, and to view criticism dispassionately on its merits. It's a spiritual discipline that we all fall short of, but that's worth attempting.

xdebugI finally got around to installing xdebug on my development environment and have decided it is the best thing since sliced bread.Installation was a breeze and the information it provides when something has gone wrong is incredibly helpful during debugging. What I didn't know, and hope to help others by documenting it here, was the amount of configuration options Xdebug has. The base install has some irritating limitations that are easily addressed with a few simple lines in the php.ini file.

PHP makes file system manipulation easy with its variety of built-in functions. One thing I always knew, but never got the chance to try, is that many of those same functions worked over FTP instead of the local file system. I finally got my excuse to give it the ole' college try and I found a few things that may help others with the same task.

PHPEvery good programmer should constantly be looking for ways to improve the look and readability of their code. One of my favorite ways to reduce vertical length while maintaining readability is to use the lesser-known ternary comparison operator.

The Ternary Operator is unique when it comes to PHP's available comparison options. Not in the sense that it does something that the other operators don't, but that the functionality it offers is not seen anywhere else.

<?php
  $myvar 
= ($x == $y) ? TRUE FALSE;
?>

PHPNoticing your pages are loading slowly or just don't like using extra cpu cycles when you don't have to? PHP makes caching very easy with their variety of Output Control Functions. In this article I'll go over complete page caching which is the easiest to implement and understand.

Blake over at PHP vs .NET has written up a very nice article to prepare those who are interviewing for a php job soon. He basically gives a bunch of php snippets and asks where the bugs are. The article somewhat morphs into a mish-mash of good-practice/bad-practice comparison and explains why one way is better than the other.

From the article:

Find the errors in the following code:

<?php
function 
baz($y $z) {
    
$x = new Array();
    
$x[sales]  = 60;
    
$x[profit] = 20:

    foreach(
$x as $key $value) {
        echo 
$key+" \"+$value+\"<BR>\";
    }

?>

Coding Horror has a pretty amusing article on the infinite number of names programmers give common ascii symbols. $, %, &, |, etc are seen across a lot of languages but somehow they have accumulated more names than the entire ascii library combined.

ASCII Names

A lot of the gripes about PHP as a language come down to the fact that there are a million ways to accomplish the same task, but only a couple are truely 'correct' - as in significantly better for one reason or another. Out of all those reasons script efficiency and speed are the driving force behind all experienced programmers.  That being said it's hard to know what the right way to do things are. Sure there are 100 ways to do something but all but 2 or 3 of those take an obscene amount of time. Normally, during development, you won't notice long load times because it's not getting hammered by thousands of users requesting the same page.

Chris Vincent has set up The PHP Benchmark to help visually show how some methods are more effecient than others.

PHPBench.com was constructed as a way to open people's eyes to the fact that not every PHP code snippet will run at the same speed.

A few highlights:

<?php
foreach($aHash as $val);
// VS
while(list(,$val) = each($aHash));
?>

  • foreach($array as $value) is the fastest way to loop through an array.
  • foreach($array as $key => $value) is slightly slower by about 10-15% so use it only when necassary.
  • Using while() and for() loops dramatically increases the server load especially when used in conjuction with list() and each().

PayPalIntegrating PayPal into your site may seem intimidating to those who have never put together a serious project before. It might seem like everything changes when you start dealing with money. You always hear about someone else who has been scammed or in some kind of nightmare involving some sort of online transaction. When you realize how easy PayPal makes it to utilize their services it really puts any anxiety you have to rest.

Knowing the basics of cookies and sessions is essential to any successful PHP programmer. It is useful to store pieces information on the users computer for later use. Things like when they laste visited, language of choice, age, etc. Cookies and Sessions are the perfect solution to our needs.

  Browser needs Cookies Enabled? Can User Edit Information?

Information Lasts Between Browser Sessions?

(Leaving site and coming back) 

Information Location 
Cookies
 Yes  Yes, easily
 Yes  User's Browser
Sessions
 No
 No*  No  Server, except for session ID

The first thing I wanted to know after learning the basics of PHP was how to store my data on a more permanent basis. PHP is all well and good but without the ability to store data between browser sessions it has limited functionality. After learning how to store and retrieve data, your PHP scripts really come to life.