Hacker News new | past | comments | ask | show | jobs | submit login
Ask HN: How do I go about PHP?
35 points by oltmans on Dec 17, 2008 | hide | past | favorite | 65 comments
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.




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/


"- 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.


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


When it comes to metaprogramming, PHP's SPL is very handy: http://www.php.net/~helly/php/ext/spl/


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.



Thank you.


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.


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.


I've used both, and while I'm no fan of PHP I think it's absurd to even compare the two. Quite simply, JSP is an exercise in masochism. These days JSPs are only used for the view layer and most of the work is done in a servlet or a controller in whatever framework you're using so they really serve as little more than a templating tool.

Even when used that way they are a royal PITA because of the bean conventions and the fact that many of the classes in the Java API (Collections cough) don't adhere to them. Unless you use a scriptlet you can't directly call a method on an object. You reference properties like myObject.prop1 and in the background it would call the method getProp1(). This convention only works with get, set and is* methods. If you want to get the size of a list (List.size()) you're SOL since lists don't adhere to the bean naming conventions. You either have to write a wrapper class or use a scriptlet. It's some kind of cruel joke.

PHP isn't pretty, but it's got a huge library of functions, it's easy to get it running and it doesn't enforce a bunch of questionable rules in order to protect you from yourself.


I didn't think that JavaServer Pages would be considered a 'framework' in the MVC sense.


Thanks!


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


I would also like to show my support for Kohana, Notepad++, and XAMPP.


Notepad++ is nice.


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


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.


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).


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.


Thanks! I think PhpED isn't free :(


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.


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.


CakePHP is a technical disaster. if you want a proper PHP framework, try Symfony



Details? Why?


I'm not the poster but the last time I looked into Cake it's internal code was just an utter mess. A lot of the mess is from it's continued support of PHP4 and PHP5 which have a lot of incompatibilities in their object models.

I haven't used Symphony in ages, but it was originally a fork of a project called Mojavi (which I used to work on). It was designed as a pure PHP5 MVC framework with a heavy emphasis on being easy to override or modify any of the components. I don't know how true it's held to that these days, but the original code base was pretty slick.

That said. I don't use either, I've got a little 300 line OOP MVC wrapper I wrote that serves my purposes. But if I were going to look at a framework today it would be Zend or Symphony.


CakePHP has changed ALOT, and for the better to boot! As a 6 year php coder and a 2 year cakephp developper, I HIGHLY recommend it.

Yes, everyone thinks the docs aren't good. They're currently pretty decent actually. Also, the community is amazing and all of it (core code, docs and community) is just getting better.

It's a really good, VERY flexible framework and most important; incredibly portable, which I think is the reason why I love it.. I haven't had a hiccup with it since the latest versions have come out (1.2 RC3?). I've also done ALOT of client work using it, rarely on my on web host. It is the Madlib to rails' Timbaland.

That being said, cakephp 1.1 was REALLY bad. I can't stress that enough. This is probably why it gets so much bad press. There's still ALOT of information written on 1.1, alot of technical articles, howtos, etc.. and an upsetting amount of reviews and performance tests based on 1.1 or early beta 1.2 releases.

I capitalize ALOT. I'm REALLY sorry. I just really like cake...


The debug bar in symfony is a big help to understanding what your application is doing and which bits of it are running slowly.

The symfony model layer is also much nicer. Cake's models are thin, making it difficult to place much logic in the model and to reuse and extend model methods. Symfony uses propel which has an excellent way of passing criteria for a db request around, making extension easier and reducing code repetition.

The symfony askeet/jobeet tutorials are well worth working through, they do a great job of showing you how to build an app.

Since first trying symfony I've found myself replicating its features and style in other php frameworks. For example I'm currently looking at if I can insert the Propel ORM used in symfony into cake to replace its model layer.


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


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.


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.


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.


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.


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.


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.


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!


Thanks. Still confused about the framework. Not sure why CodeIgniter doesn't support PHP 5 :(


From here: http://codeigniter.com/user_guide/overview/at_a_glance.html

"CodeIgniter is written to be compatible with PHP 4. Although we would have loved to take advantage of the better object handling in PHP 5 since it would have simplified some things we had to find creative solutions for (looking your way, multiple inheritance), at the time of this writing PHP 5 is not in widespread use, which means we would be alienating most of our potential audience. Major OS vendors like RedHat are moving slowly to support PHP 5, and they are unlikely to do so in the short term, so we felt that it did not serve the best interests of the PHP community to write CodeIgniter in PHP 5.

Note: CodeIgniter will run on PHP 5. It simply does not take advantage of any native features that are only available in that version."


Hmmm, interesting. I didn't read that. Thank you, thorax.


if you are looking into Code Igniter, i suggest that you look into http://kohanaphp.org/home - it is a fork of CodeIgniter.

CodeIgniter does support PHP5. But you will have to turn off warnings, etc.



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


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/


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


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.


Late to the game here, but I have to recommend Komodo Edit for your IDE - I've been using it for about a year now and it's just so solid. I've tried others (most recently Netbeans PHP, which is a CLOSE contender), but they just don't do things that Komodo does that I've grown very accustomed to.


a) Probably start trying to write a site you want and learning from there-- with a background in other languages it's more about "I know the language can do X-- let me search for something like it". When using PHP you can often just google "PHP equivalent of " whatever function you know in C#/Java/JS and there will be some corresponding example to use.

b) I like NuSphere PhpEd even though it isn't free. Some people like Netbeans IDE for PHP.

c) I love CodeIgniter. Very fast, MVC, some of the best user guides for any framework, just very solid all around. Go with it initially-- I know many PHP developers who got started there and have grown to be some of the most productive web coders I've ever seen.


Personally I would forget the framework - you don't need it. Any sufficiently advanced framework is indistinguishable from php.

OK, maybe not exactly, but many of them suffer from second system syndrome where they try to re-implement everything php offers.

Just use php directly.


Yeah, I think this is bad advice. One of PHP's drawbacks is that it is such a loose language. If you don't use a framework, you will most likely fall into the trap that many PHP coders fall into, which is being quite lax with your code and consistency.

Using a framework really helps you maintain some consistency throughout your code. If you don't like what other people have made, build your own.


Really? While I agree that a lot of frameworks are a bit on the heavy side I wouldn't advocate against using one. Frameworks do have something to offer that stock PHP doesn't, structure. I can't imagine going back to doing PHP without so much as my little 300 line MVC wrapper to work in. It's certainly possible to write good PHP without any sort of a framework, but it's infinitely easier to write really bad PHP that way too.


As someone who has been called in several times to support existing custom-built PHP sites, I very much disagree. A framework will at a the very least ensure a clean organization. PHP is extraordinarily easy to make a mess of, frameworks help.


I'll have to say I disagree as well-- PHP with a good framework is a better language, hands-down. I don't enjoy coding raw PHP. Almost all of the frameworks are open source, and none of them forbid you from using raw PHP when you need to do so.

So the net recommendation is, get a framework that makes a lot of secure/clean decisions for you and diverge when you think they're reinventing the wheel too much.


CakePHP. The documentation is great. Performance is great. The learning curve isn't too bad. It's a framework that will take care of you.

Every negative criticism about CakePHP is about two years old and no longer relevant.


Though you may find some sections rudimentary, this is actually a great primer/refresher: http://devzone.zend.com/node/view/id/627


You can jump into PHP quite quickly. The syntax is not very different from other languages such as C++ or Java. What takes a little while is learning the built-in functions.


After 7 years of exp I found that

1. Zend Framework - best framework, CodeIgniter is next. 2. Zend Studio - best IDE 3. Smarty - don't reinvent the wheel


I worked with cakephp, but because of the lack of documentation i'm turning to codeigniter. It just works.

Welcome to php! :)


a)Depends on what you consider learning PHP. Depending on the framework you choose, you may or may not actually need to know much PHP, aside from basic syntax. More on this in point c.

b)Until yesterday I've been using eclipse PDT, but you'll soon have a love/hate relationship with it, and start looking for alternatives from time to time. That said, I started to use Netbeans PHP yesterday, so far so good.

c)With your background I recommend you check out NOLOH (Not One Line of HTML) http://www.noloh.com, it's currently in public Beta, and is slated for general release in January. One of things you'll notice from the NOLOH website and it's tutorials is that you really don't need to know PHP, other than the syntax, check out the Hello World and Threaded Comments videos to see what I mean.

Full Disclosure: I'm one of the co-founders of NOLOH.


Anyone here care to give some opinions about Zend PHP Certificate?


I'm not sure how much value certificates have. I just see them as another revenue stream for Zend Corp. I think a quality portfolio (commercial, side-projects, open source) has much more value.


but to point out the from the first comment left by kungfooey

you're best way to start in any language is to develop a small app without a framework.....great advice by him/her.


EDITED TO ADD:

Most web hosts support PHP4 or PHP5? Any ideas on this one?


I run a little web hosting business myself and from what I know about the industry most hosts should offer both at this point (with PHP5 as the default). If PHP4 is the default on your host, you can try adding this to an .htaccess file in the root directory of your site:

   AddHandler application/x-httpd-php5 .php
If your host is running the latest version of CPanel, you should be able to access your control panel and change the default version yourself: Software Services -> PHP Configuration.

If that doesn't work, complain to your host and demand PHP5!


a) Starting with tutorials is a logical first step, however they may be some bad advice in there. Writing some simple applications or pages to accomplish stuff is good to get a handle on the breadth of built-in functionality.

Please also note that PHP 5 has much better OO functionality than PHP 4. PHP 4 is end-of-lifed, however CodeIgniter is still compatible with PHP 4, thus limiting it in some respects.

I wouldn't focus on too much open source PHP apps, much of them are kind of sloppy-the frameworks will probably be a better code base to look at. Just like any other language you can write sloppy code, but you can also exert some effort and make a well-organized codebase.

The last thing I would recommend is to read about PHP security issues. (Disclaimer, this is my own site) http://flourishlib.com/docs/Security has an overview of security issues, however some of it is focused on using Flourish to fix issues.

b) I would recommend looking at:

  - PHPEd (commercial, http://nusphere.com) <- I use this
  - Netbeans 6.5 (open source, http://netbeans.org)
  - Aptana with the PHP 1.0 plugin (open source/commercial, http://aptana.com/php)
Komodo (commercial, http://www.activestate.com/Products/komodo_ide/) was alright but I haven't tried the new 5.0 release. Eclipse PDT 1.x (http://eclipse.org/pdt/) was ok, but very much a pain to configure-I hear 2.0 is coming out soon.

c) In terms of frameworks, here are a few of the big ones in addition to CodeIgniter, however if performance is a concern, please do check out http://paul-m-jones.com/?p=315.

  - http://cakephp.org
  - http://framework.zend.com
  - http://symfony-project.org
http://kohanaphp.com may be of interest since it was originally based on CodeIgniter, but has been rewritten to use PHP 5 functionality.

Definitely learn to use http://php.net for lots of reference. The comments on function pages can often include a good amount of insight. All functions are accessible via http://php.net/function_name.

If you are looking for less of a framework, but more of a library, I've been working on Flourish (http://flourishlib.com) for a while. I've focused a good amount on security, documentation and usability.


Thanks!




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: