Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I would like to learn PHP for web development but have been drawn back because of comments like the following*:

  • PHP is good but generates spaghetti code
  • PHP is nice but Python is marriage material
  • PHP lacks stuff that you get in other languages like C# or Java

But for PHP5 I have seen some promising comments. So, my question is: How does PHP5 fare with earlier versions of the language and is it good enough now to learn for web development.

* Comments are just for reference not to incite a flame war. No comparison of PHP with other languages is asked for here. Please comment just on PHP5 and how it compares with earlier versions.

share|improve this question
8  
PHP doesn't generate spaghetti code - bad PHP programmers generate spaghetti code –  HorusKol Nov 17 '11 at 22:21
    
Why do you include comments about PHP and other languages and how they incited doubt but don't want any discussion of these in the answers? –  phant0m Aug 9 '12 at 7:05
add comment

4 Answers

up vote 17 down vote accepted

When discussing languages, these two Stroustrup quotes always pop in mind:

Anybody who comes to you and says he has a perfect language is either naive or a salesman.

and

There are only two kinds of languages: the ones people complain about and the ones nobody uses.

Now to the question at hand. Most of the hate is mostly directed at PHP4. PHP5 was first released in July 2004 (beta a year earlier) and the last version of PHP4 is 4.4.9 released on August 2008. There is no valid reason to use PHP4 anymore, unless of course you maintain some legacy app. PHP4 is no longer supported by the PHP group, and we all hope it fades away soon.

The gap between the two versions is bigger than it usually is between two major releases, as the engine behind the interpreter was rewritten from scratch, and included a new object model. The release of PHP5 roughly coincided with the release of MySQL5, a major overhaul of MySQL. PHP is usually coupled with MySQL and the fifth version of both addressed several issues and brought them up to speed with the competition. MySQL is also subject to FUD comments, but, well, it's the database behind Google's AdWords.

PHP 5.3 was a major release in everything but name. A lot of features that where intended for PHP6 found their way in 5.3 and for every 5.2 developer out there I'd strongly advise to migrate to 5.3+ sooner than later. The current release, 5.4, includes some awesome features (amongst others):

Now for your specific comments:

PHP is good but generates spaghetti code

That's absolutely not true. No language generates any kind of code, it's always up to the developer to screw up, spaghetti code is possible in every language out there. What is true though is that PHP has a very low entry barrier compared to other popular languages, hence it's a lot easier for inexperienced developers to write bad (but working) code. But that's a side effect of every easy to learn language.

Furthermore PHP does not enforce any particular style or paradigm, and it's always up to you to decide what to do. I've always thought of this as a good thing, while others see it as a valid point of criticism. You'll have to decide for yourself.

PHP is nice but Python is marriage material

PHP lacks stuff that you get in other languages like C# or JAVA

Yes it does. And general-purpose language X lack a lot of stuff that you get in PHP. Like being portable across operating systems AND across http servers, and support for every imaginable third party library that would be useful in a web development environment. You have to understand that PHP doesn't lack of anything important, if it did it would have died a long time ago. The comparison between general-purpose language X and other multi-purpose languages to PHP is a fallacy, apples and oranges. Although PHP offers some facilities to support the idea that it's general-purpose, it's not, it's a web language.

You have to keep in mind that PHP's evolutionary path is quite different from general-purpose language X: PHP grew and evolved, it wasn't designed. Every time a feature is added to the language is out of actual necessity, not in accordance to some imaginary ideal of what a language should be. Languages are first and foremost tools, and PHP is perfect as such.

I don't have anything against general-purpose language X, I'm all for choosing the right tool for the job. I've always mixed and matched, and I'm coding in Java and Python too, sometimes in parallel with PHP. But PHP is the only one I'd ever consider marrying. :)

If you're considering starting with PHP, you should of course start with the latest stable version, as you would in any other language.

share|improve this answer
    
I would add some incompatibilities between PHP4 and 5. You did forget Garbage collection for PHP 5.3 . –  deadalnix Nov 16 '11 at 16:58
1  
@deadalnix I didn't add the incompatibilities between PHP4 and PHP5, because I think such a list is only useful if you are considering using PHP4. My point is don't use PHP4 for any reason, just because it so old (PHP5 has been around for 3+ years). Also I didn't forget Garbage Collection, it wasn't my intention to create a definitive list of what's new in PHP5.3 (such a list can be found on the manual), just to highlight some of those features in the context of the question. –  Yannis Rizos Nov 16 '11 at 17:06
add comment

PHP has been "good enough" for web development for a long time. As with any programming language, the quality of the code hinges on the skills of the developer writing it.

A common criticism of PHP is that its flexibility makes it easy for inexperienced developers to write bad or "spaghetti" code. This is largely because it tries to simultaneously embrace a number of paradigms that occasionally conflict, are poorly implemented, or do not mesh well.

  • Server-side scripting (PHP) juxtaposed with client-side code (HTML + Javascript) without modularity is common, almost encouraged, to the detriment of clarity & readability.

  • It's commonly written in an imperative style, but provides mechanisms for object-orientation. This easily results in mixed styles and can often lead to bad design.

  • Object-oriented design was (in my opinion) poorly supported in PHP4. This is one area that PHP5 has significantly improved upon (e.g. with access modifiers, abstract classes, improved scope resolution.)

  • PHP's dynamic typing enables run-time errors that could be eliminated with static type-checking. This is inherent to the design of all PHP versions and will not be changing any time soon; the burden of validating object types is left to the programmer.

The changes in PHP5's OOP model do a lot to encourage good design moving forward, but do not add any disincentives to discourage the bad practices mentioned above.

share|improve this answer
add comment

PHP has some significant issues within itself, but those are primarily of the annoyance variety: Its library is inconsistent and somewhat schizophrenic, it lacks strings as first class objects, things like that. It does not force you to write poor code, and it does work fairly well for what it does -- write web pages. Your PHP will be as good as you strive for it to be; it's quite possible to write PHP that's terribly procedural and looks like C code, and it's quite possible to write PHP that's overly Object Oriented and looks like Java. As someone who develops in PHP daily, I like some things about it and hate some things about it and can't wait for some 5.4 functions (like Traits and sane function return dereferencing), but I'd wholeheartedly encourage someone who's thinking about picking it up to do so.

share|improve this answer
add comment

php5 is a much improved version of php4.

Most but not all php4 code works in php5.

What really changed was the much improved Object Model, which fixed a lot of limitations and quirks inherent in the previous OO implementation, but, which is largely incompatible with preceding versions.

Docs here

The other comments have some validity but are not a "no go"

"PHP is good but generates spaghetti code" -- you can generate all sorts of pasta in any language, but, php, does nothing to discourage the inexperienced coder writing a rats nest of unstructured code. Conversly you can write nice code in any language, skilled programmers have no problem writing clean well structured php.

"PHP is nice but Python is marriage material" -- Its a nice language but there are several diverse web frameworks all of which have extremely good points, but, none of which has really taken off, which is all fine, but any Python/framework of your choice combination will always be a niche skill.

"PHP lags stuff that you get in other languages like C# or JAVA" -- php has the most diverse and well implemented library support of any language except perl. There are a few blank spots like statistical analysis, and, vector processing. But generally if you want to do something a quick search of PEAR will locate a library which does it. The language itself is simpler then C# or Java -- but the simplicity of the basic syntax plus "Duck Typing" means you can do anything in php you can do in Java in a more readable and elegant way.

share|improve this answer
    
The statement "PHP has the most diverse and well implemented library..." is debatable. I would argue it's far too bloated and poorly organized. –  robjb Nov 16 '11 at 7:25
    
@robjb -- don't get the "bloated" bit, you don't use what you don't need and surely the more libraries available the better, as for poorly organized , PEAR and PECL are pretty slick - no real equivalents in the C# and Java worlds. The libraries are not "orthogonal" but this is inevitable with many projects contributing over several years. –  James Anderson Nov 16 '11 at 8:35
1  
I'm not denying PHP has some very useful extensions / libraries, but I don't know anything else to call several thousand globally scoped functions but "bloated." Also, a number of functions have similar purposes. Inconsistent naming further obfuscates the core libraries, e.g. strip_tags vs stripslashes. I would argue C doesn't exhibit these issues, yet it's been around quite a bit longer. ;) –  robjb Nov 16 '11 at 15:21
    
The whole "bloatware" -- i.e. an out of the box working, php, installation usable to build a useful web site comes in at about 70MB. This is smaller than some of the JAR files that come with JE. Most developers would regard the global scope of the built-in functions to be rather useful, compared with the verbosity of Java where you need to "import" a jar file before you can do such esoteric things as write a line to sysout. PHP is far from perfect but for small to medium web applications its better than anything else. –  James Anderson Nov 17 '11 at 1:49
    
@robjb -- I don't get the "C" comment, C exhibits the inconsistent APIs, duplicate functionality in spades. Just because the libraries are hard to find and you only ever use a few how many "smart strings" libraries are there out there? –  James Anderson Nov 17 '11 at 1:52
show 3 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.