Many PHP programmers today fall into two camps: those that are self-taught and learned PHP to make a website, or those coming out of university Comp Sci courses heavily grounded in strictly typed OO (I.e Java).
This is, of course, not absolute, there are camps of reformed Perl sys-admin monkeys that haven’t been seduced by Python. And of course a rare breed of ex systems programmers versed in C and C++.
It is actually this last group of coders that tend to get on with PHP the best.
This makes sense, because PHP was originally designed to be a template engine to produce HTML with a core C backend doing the grunt work. This seems a ridiculous idea now, as CPU cycles are much cheaper than man hours. But in the late 90s native code ran circles around dynamic, interpreted alternatives.
These days C is a little bit like ASM was in the 90s. Being proficient in it is handy, but not essential. I would argue though, for PHP coders it is an important environment in which to be familiar.
Many of PHP’s eccentricities can be traced to it’s C roots. The strange and inconsistent function parameter orders, right down to the sometimes peculiar way it handles memory and references.
The file and string functions marry up almost 1:1 with standard C. Experience of and understanding of how these low level functions work will immeasurably improve your application of their PHP counterparts.
Networking is another area where PHP and C are very similar. Indeed you can write a simple tcpclient in PHP, get rid of all the $ variable identifiers and have a recognisable C fragment.
The biggest benefit though of learning a bit of C or C++, is to get an “appreciation” of memory management. C is unique among the programming environments of today in that it does not manage memory for you. Arguably this is what makes C so painful to develop in. But similarly, a sometimes useful characteristic that makes the environment still relevant today.
By way of example, strings in C are represented by an array of chars, terminated by a NUL ‘\0’.
In C, you would declare a string like this: char str;
This declares a string of 20 characters, and str itself points to an address in memory where 20 bytes have been reserved for itself. Now if we try to write 21 characters to this string, C wont autoexpand the array to fit, or do anything so helpful as to warn you that you’ve gone out of the bounds of your allocated storage. No, C will do what you tell it, and in this case it means overwriting someone else’s data.
This is quite a powerful characteristic and as such requires a programmer to be responsible.
Generally if you have a 20 char string defined but want to store a 25 char string, you need to reallocate memory. You could declare a new array or do a concatenation operation. Either way, this costs CPU time.
So when in PHP you are spamming ‘.’ concat operators everywhere, you can appreciate that PHP is doing a lot of memory re-allocation under the hood to provide that syntactic sugar.
As mentioned above, PHP was originally intended to be a purely templating language for C web applications. Which is where PHP modules / extensions come in. These originally were where your business logic was to go. PHP extensions are compiled to native code, and as such run FAST. You can then access these functions in PHP just like the other core functions and classes. Yahoo, Facebook, use PHP in this way.
So can you. A great way to dabble in a bit of C is to write your own PHP extension. The benefit of getting your hands dirty in this way is the nice speed boost for the functions in your extension. But the more important benefit is how it can greatly improve your understanding of what PHP does for you under the hood. This lets you make informed design choices when writing your regular PHP code.
As a starting point, I can thoroughly recommend starting out with the Zend engine hacker’s guide on the main PHP site. Becoming familar with the structure of the internals is the first step to appreciating all the of the magic PHP does for you under the hood.