top | item 401022

Ask HN: How do I go about PHP?

35 points| oltmans | 17 years ago | reply

Hey hackers,

I've a background in C#, Java and JavaScript but don't know a thing about PHP. Couple of questions

a) How do you recommend I should go about learning PHP? b) Any recommended Editors (I've to use Windows)? c) I spent sometime looking for frameworks and CodeIgniter seems promising. Your thoughts?

Any other tips, books, libraries, frameworks and goodies you would recommend? Any help is highly appreciated.

65 comments

order
[+] nuclear_eclipse|17 years ago|reply
As a long-time PHP veteran still in the practice:

- Recommend Editor: "Eclipse PDT" [1] probably has the best tools relating to PHP development, although I just use Vim for all of my PHP work.

- Bookmark the PHP online manual [2], and add their search plugin to Firefox, so you can look up standard library functions just by typing it into the search bar.

- The Zend Framework [3], more of a great library than a traditional framework, is probably the best resource for any projects your may be starting from scratch, although CodeIgniter is definitely a top contender.

Probably the best way to learn PHP itself is to read the (rather well-done) PHP manual, which includes everything from an introduction to the language, it's parsing and syntax trees, all the way up to how its object model works, and complete references for the entire function library, and all standard modules.

Read some source code from the wild. I suggest Mediawiki and Drupal.

Key points to make sure you understand:

- Arrays are the archetypal datatype in PHP, and can be used as lists, dictionaries, or a mix of the two.

- You can do just about anything just by using the standard function library. Frameworks are not necessary, contrary to popular belief.

- Objects are a mishmash of arrays, structures, and standard C++/Java objects. You can add class variables, functions, and such at runtime.

- You can do a lot of "fancy" meta-programming in PHP; you just have to know how to accomplish what you want. The PHP library helps out a lot with this.

- Everything is a template. It may be a bit more verbose than compiled templating dialects, but PHP itself is a really handy templating system in it's own right, just using <?php and ?> tags.

[1] http://www.eclipse.org/pdt/

[2] http://www.php.net/manual/en/

[3] http://framework.zend.com/

[+] senthil_rajasek|17 years ago|reply
"- You can do just about anything just by using the standard function library. Frameworks are not necessary, contrary to popular belief."

Up vote for that bullet, specifically for PHP.

[+] nuclear_eclipse|17 years ago|reply
As an extended note, I think it important that you understand the underlying concepts behind type inference and type coercion, and then to understand how PHP uses those in various circumstances:

- Convert numbers to strings and vice-versa - Compare strings to numbers - Typeless versus typed comparisons (== and ===) - Array keys using '10' (a string) versus 10 (a number)

Once you understand the type inference and type coercion concepts, it should be simple to pick up how it affects your PHP code.

Most importantly, PHP will generally do what's most logical/useful/expected when it comes to these topics, so you generally don't need to think about it, but you should be aware of it in any case.

A good reference can be found in the PHP manual, which gives you tables of comparisons between different data types in different situations: http://www.php.net/manual/en/types.comparisons.php

[+] breck|17 years ago|reply
Agreed about the Frameworks part. PHP is basically one giant framework that gives you most of what you need (and the few things you it doesn't give you whip up on your own in a few hours and eschew the bloat of the frameworks). That being said, Cake is pretty good.

For an IDE my favorite is PHPDesigner by MPSoftware. Eclipse is pretty good too, just a bit bloated if you like something leaner.

[+] kungfooey|17 years ago|reply
I worked with Java for four years, slowly moving to PHP as version 4 came about. Why? Rising demand.

As with any programming language, the best way to get started (in my opinion) is just to build something. A blog, a guestbook: something functional and useful. In the process you'll become familiar with the language fundamentals. I recommend that you at first build something using no framework (to get an idea of what sort of security measures you need to take, handling DB transactions, etc) and then followup with the framework of your choice.

I used vim/Eclipse for Java, and still use the same for PHP. There is an Eclipse build specifically for PHP, called "Eclipse PDT." There are several others out there (Komodo, Zend Studio) but PDT is free and therefore easier to try out.

I use CakePHP as a framework and find it to meet most of my needs. I have also tried symphony and the Zend framework, but neither of them were flexible enough for me. I have not tried CodeIgniter, but I have heard good things about it. There are some articles on IBM DeveloperWorks that are a good introduction to CakePHP.

[+] mlLK|17 years ago|reply
I've been told that JavaServer Pages is greater than any framework out there that PHP has mustered up, I'm wondering if you've ever given this a whirl and how it compares? I don't have a enough experience in Java, which is why I wonder.
[+] ErrantX|17 years ago|reply
I recommend Kohana as a framework to work with (kohanaphp.com). It started as a branch of CI so is similar to that - but was totally rewritten with PHP5 support and lots of new stuff :)

Worth a look.

I always recommend Notepad++ plus a XAMPP installation over any form of IDE. It's simpler in the long run.

As to learning: that is a hugely personal thing. I picked it up over 4 or 5 years (still learning even now though!). If you already have a good grouding in general programming you wont have a problem :)

But with PHP learnign by doing is a good approach :) there are lots of snippet sites out there as well for you to peek through for ideas :D

[+] oakmac|17 years ago|reply
I would also like to show my support for Kohana, Notepad++, and XAMPP.
[+] dezwald|17 years ago|reply
i've been developing in PHP for 6 years now....and i have tried many frameworks. the one i like the most in my opinion is CodeIgniter.

CodeIgniter is very flexible and not as strict when it comes to implementing your own code and (libraries/classes/plugins). It's very simple, implements great separation (in regard of its MVC archt.) which makes it easy to understand and use. The framework has great, easy to follow documentation with great forum support and a growing community.

i say just watch one of their video tutorials of their website and you'll see how easy it is to get started on a project.

As for an IDE editor:

if you're using windows....i HIGHLY RECOMMEND PhpED (by nusphere)

I have tested every possible editor out there and this one by far is the BEST. It's fully featured in every aspect, easily customized and runs fast!.

they have definitely taken programmers needs and dumped it into this application.

it's definitely worth a try at least.

just checkout the features here.

http://www.nusphere.com/products/phped.htm

[+] bprater|17 years ago|reply
I second playing with CodeIgniter first. It's very "liberal" in the way it allows you to set things up, which means you'll be learning PHP and not fighting the framework designer.

You aren't constrained by the framework. If you want to do queries in the controller, go for it.

[+] thorax|17 years ago|reply
Wow, I have the exact same feedback. I should have read this first, but consider his two recommendations as seconded (even with the cost of nusphere).
[+] henryw|17 years ago|reply
phpEd is excellent, Zend's PHP IDE is great too. Both have code completion and search through project and other great features that's not available on Notepad++ or other text editors.
[+] oltmans|17 years ago|reply
Thanks! I think PhpED isn't free :(
[+] mechanical_fish|17 years ago|reply
I didn't set out to learn PHP. I set out to learn Drupal and picked up PHP along the way.

I think that's a pretty good way to go, actually. You want to learn PHP by tinkering with a bunch of (a) working code that (b) has been reviewed by a community of people, at least some of whom are (c) trying to exhibit some taste. The problem with the language is that various sectors of it are poorly thought out, or have been implemented several times, and the best way to avoid stumbling over them is to follow in the footsteps of some expert guides.

Joining the userbase of one or another framework will probably give you much of the same effect.

The language itself is not hard to learn at all, except for the bits that are maddeningly silly. Watch out for the array() and the way it handles keys. Find the handy online table that compares empty() and isset() and is_null() and helps you to figure out that, in many cases, none of them is the thing you really want. ;) Watch out for accidental typecasts and their effect on your logic.

[+] sammcd|17 years ago|reply
I'm just popping in because you mentioned codeigniter. Code igniter is great, if you are forced in PhP 4. However, there is http://kohanaphp.org Kohana started out as a fork of codeigniter, and is very similar, except that it uses php5's object-oriented features to create a much more elegant MVC framework.
[+] noodle|17 years ago|reply
a) i recommend learning by doing. imo, start at the bottom and learn how to hand-code from scratch a single table CRUD app. then go through and clean it up. then build the same thing with a framework (i suggest CakePHP or CodeIgniter). then go through and clean it up. then build something more meaningful.

b) a quality text editor will work just fine. when doing php stuffs, i use jedit, and have used eclipse and netbeans in the past.

c) see a

[+] paulsweatte|17 years ago|reply
The way I've transitioned between languages was to first do a syntax comparison: http://merd.sourceforge.net/pixel/language-study/syntax-acro...

Then take a crash course: http://cs.metrostate.edu/~fitzgesu/courses/ics325/summer04/C...

Then learn about database interfaces: http://www.kitebird.com/articles/php-pdo.html

Then learn about programming patterns: http://www.rosettacode.org/wiki/Category:Solutions_by_Progra...

Then learn about security: http://www.owasp.org/index.php/PHP_Top_5

Then learn about error handling: http://www.w3schools.com/php/php_error.asp

Then learn about debugging: http://www.onlamp.com/pub/a/php/2004/08/12/DebuggingPHP.html

Then learn about idioms: http://www.cybersprocket.com/wiki/index.php/Eric's_PHP_Guide

Then read a comprehensive book: http://www.ubookcase.com/book/Sams/PHP%205%20Unleashed/

Then learn about optimization: http://progtuts.info/55/php-optimization-tips/

I've used PSPad, Komodo Edit, and Aptana Studio Community Edition for PHP editing, and recommend them all.

I've tried CakePHP and Drupal, but had trouble with the documentation and syntax of both compared to pure PHP. Wordpress has great documentation, community support, and readable sourcecode.

[+] texec|17 years ago|reply
a) With your background you should not have any problems to learn PHP. If you start, don't begin with a big complex system, like a blog or a forum. Try small scripts and learn about web secruity and PHP pitfalls. Then you are ready to begin something bigger.

b) If have experiences with Java, try one of the PHP Plugins for Eclipse.

c) CodeIgniter is mostly based on PHP 4. This results in same outdated patterns, especially regarding PHP 5.3. If you want real extensibility (and highly object orientated code) try the Zend Framework. If this is a bit too much, i recommend the Yii Framework, it's lightweight and brings lots of ideas from rails to PHP without copying the rails specific approach.

[+] mlLK|17 years ago|reply
Well, how familiar are you with HTML, MySQL, (or any other db lang), and CSS? In the end, a PHP file is really just parsed HTML, so anything not between <?php and ?> is just good ole markup. It also depends on what you want to get out of it, since PHP covers so much ground, but given that PHP is more attractive from the back than the front, I'll assume you wanna build some server-side applications. So for starters, in order to familiarize yourself with $_POST and $_GET (the two variables you'll be molesting the most) and the basic syntax try doing some simple HTML pages that evaluates some browser/platform condition and slowly work your way into HTML forms and attempt a feedback form. After that you'll work your way into MySQL functions and how to evaluate their output (this is back-back-end stuff [your typical db commands; creates, queries, inserts, etc]), user authentication comes next along with the global variable $_SESSION, which also considers how to handle cookies and different types of users, and lastly, after you've completed all of the following, you could attempt some sort of CMS (keep in my mind the scale since you're still learning), but by creating a basic CMS you'll slowly start seeing what an MVC pattern is in PHP. It's hardly abstract and quite hideous since you're only using MySQL and PHP, but by participating in this process you should uncover the gist of PHP and why it's so easy. After that if you're still sane, install PEAR and reiterate the process using their libraries else if unsatisfied or overwhelmed move onto something in Java and try JavaServer Pages.
[+] ryanwaggoner|17 years ago|reply
I generally recommend PHP newbies start with a framework and I generally recommend they start with CakePHP over CodeIgniter, because CI is much more flexible and therefore forgiving of bad practices which can turn into bad habits. CakePHP is more strict, which probably teaches better design patterns early on.

In your case, though, you sound like an experienced programmer in other languages, so you should be fine starting with a simple CRUD app and then progressing to frameworks; any of the popular ones will do fine.

[+] KrisJordan|17 years ago|reply
Great question.

For editors I highly recommend Eclipse DPT: http://www.eclipse.org/pdt/

With a Java background I would think you'll feel right at home there.

Framework wise stay tuned to (shameless plug) http://www.recessframework.org - it's a framework I've been working on that makes greater use of the OO facilities in PHP5, is RESTful, and has a straightforward ORM. Public preview release soon.

CodeIgniter is written to run in either PHP4 or 5 which means it makes less interesting use of the PHP5 object model. If you're an OO nut I suggest staying in 5 and perhaps looking at Kohana.

[+] drathbun|17 years ago|reply
I see lots of complex answers, but not the answer to actually learning a bit about PHP. My favorite book is "PHP in Easy Steps" by McGrath, published by Barnes and Noble. Very inexpensive.

A good intro to the basics, which will allow you to make use of the online docs, which is the best documentation of a programming language I've ever seen.

After that, the other resources come in to play. For an editor, I use notepad++ on the pc and textwrangler on the mac. With apache, mysql, and php running locally, your development platform is free.

[+] tdonia|17 years ago|reply
a) i haven't used any php related books, but this is invaluable:

http://www.php.net/docs.php

i would suggest downloading a version of it for quick reference. it's worth learning the commonly used functions, but a big chunk of php's value (and the main criticism against it) is a namespace filled with goodies (with varying degrees of good). This means there is probably a lot of useful functionality that's used sporadically enough that it's not worth committing permanent headspace to. Rather, learn to be quick about finding the right function to use. It's usually worth checking to see if there's some strange function/module that does exactly what you're trying to do. and if there's not, you can usually find example code in the comments - though i wouldn't recommend copying that code into production. rather, use it as a starting point for exploring the problem at hand.

i'd suggest learning php by giving yourself a well defined, if simple, project to start with and attacking it the same way you've learned languages in the past. probably with some reading, a lot of doing, occasional breaking, then more reading.

b) notepad++ or homesite are my preferred IDE's

c) codeigniter, cakephp ( http://cakephp.org/ ) and the zend framework ( http://framework.zend.com/ ) are the big frameworks i'm aware of, but i'm sure there are more out there.

enjoy!

[+] oltmans|17 years ago|reply
Thanks. Still confused about the framework. Not sure why CodeIgniter doesn't support PHP 5 :(
[+] floodfx|17 years ago|reply
Take advantage of the object oriented features (classes, inheritence, etc) Write unit tests early and often. Re-read the docs; some of the PHP functions are inconsistently parameterized -- meaning one function expects params in one order and a simliar function expects the params in the opposite order! (Can't think of those functions off the top of my head but there I swear...) Just be careful. PHP is forgiving (almost?) to a fault
[+] mseebach|17 years ago|reply
The worst thing about PHP is how easy it is to leave SQL and HTML all over your code.

Install Doctrine (ORM) and Smarty (Templating engine) or friends and use it religiously. If you do, you can use PHP for what it's best at: Cranking out business logic at high speed.

http://www.doctrine-project.org/

http://www.smarty.net/

[+] rsayers|17 years ago|reply
When I was learning PHP (way back in version 3) I started by building a simple addressbook backed by mysql, I would also recommend as kungfooey did to start by building something and learn as you go.

As far as editors go, I use emacs, but for about 5 years I used UltraEdit and was very pleased with it.

I personally use a framework i wrote a while back, but if I were starting over I think codeignitor would be my pick

[+] showerst|17 years ago|reply
The other advice here is excellent, I'd just like to throw out a vote for the editor Textpad (http://www.textpad.com/).

It's much lighter than eclipse, but still has tons of features (regular expression search & replace is a favorite).

Small download, so it's at least worth checking out.