C vs PHP – Pointers in C vs No Pointers in PHP

cPHPpointers

Both languages have the same syntax.

Why does C have the weird * character that denotes pointers (which is some kind of memory address of the variable contents?), when PHP doesn't have it and you can do pretty much the same things in PHP that you can do in C, without pointers? I guess the PHP compiler handles this internally, why doesn't C do the same?

Doesn't this add unneeded complexity in C? For example I don't understand them 🙂

Best Answer

Like so many things, the answer is of the form "Because X and Y are different things with different purposes".

In this case, the designers of both languages assumed that the users of their languages had a very different set of goals. For C, the primary use case was "portable assembly language", which really means getting down into the nitty-gritty of how the computer is actually managing its resources. There's no practical way to avoid memory address manipulation at the lowest level of abstraction, and so C has robust support for it.

PHP was intended to make dynamic web page content as flexible and painless as possible. This is quite a few steps removed from the super-low level of the C world; managing memory is, for the purposes PHP is intended to address, much too low level to be of much interest. Any kind of automatic memory management would be fine, so long as it is robust and reliable, and stays out of the way. That's exactly the situation you see in PHP; objects are allocated automatically, when needed, and garbage collected when they are no longer useable, and it all happens without the intervention of the PHP programmer.

It's perhaps of some interest to observe that PHP is itself written in C! the nitty gritty of memory management is written in C, which provides the tools needed to do that kind of thing, so that the language created, doesn't require the programmer to do much themselves.