DerekAllard.com

Learning PHP with CodeIgniter

Every now and then, the question comes up of if its a good idea to learn PHP before one learns CodeIgniter, or after one learns CodeIgniter.

I teach technical training for 2 universities, a college, and do a lot of corporate training, so I often find people who are just starting out and wanting to pursue a tool.  For a long time my knee-jerk advice was “learn the skill first, then come back to the tool.”  That said… the longer I think about it, the more I can see how learning PHP through a framework like CodeIgniter could be really useful. So here is my current position on the topic.

If you don’t know PHP and want to learn, then learn it at the same time as you learn CodeIgniter

One of the major risks of this though, is that it might be easy to confuse what is “native” PHP and what is CodeIgniter.  So you still must learn the basics of PHP, and if you haven’t spent at least a few hours reading websites like php.net, you haven’t paid your dues ;)

After you’ve got the PHP basics down, get yourself a decent PHP book (anything from Apress, or Wrox, or O’Reilly).  Now, before you crack the book, read through the CodeIgniter userguide, and print off the table of contents for your reference, even if you don’t understand it yet.  As you read your PHP book, check the CodeIgniter table of contents to see if what you’re reading about is already something CodeIgniter handles.  Try to implement concepts the book shows through CodeIgniter, then compare it to how CodeIgniter might do something, then ask yourself “why is it doing that?”  For example, if you look at one of those “intro” tutorials, or basic PHP books, they tell you that to read form submissions you should use $_POST[‘form_field’]; and that’s all they say about it.  Then they show some really neat example of creating a guestbook or something and tell you to that you can save your work into the db by using

mysql_query ("INSERT INTO table (fullname) VALUES (" $_POST['fullname'")"); 

and you think “cool”, but in reality that is horribly bad practice, as it leaves you wide open for attacks.  Now let’s compare it to the way CI does something.  CI would never let you get away with something so sloppy.  CI wants to sanitize the input, clean the input, shorten it, take it out for dinner, wine and dine it for 2 months and then maybe, it’ll let it into the database.  Hmm… I wonder why?...  And now we’ve learned a useful lesson that all programmers should learn - namely, never trust input and clean up everything.

So punchline - I think learning PHP alongside CI could work, but you still need to pay attention to the underlying PHP, logic and practices.

If you do decide to start PHP with CI, drop by the forums and say hi!

 

Comments

e-man wrote on

Point well taken Derek. Installed CI on my iMac and have started reading the user guide :)

Jon wrote on

I think that learning PHP at the same time as CodeIgniter is a GREAT way to get a newb’s feet wet!

As it stands now, many tutorials out there have the crap that codeigniter discourages:

$rs mysql_query("select * from table");
echo 
"<ul>";
while(
$get the data{
  
echo "<li>".$datafromdatabase."</li>";
  echo 
"<li>";
}
echo "</ul>"

Of course, you see that embedded in an HTML page called page1.php that contains a single include to your database credentials.  page2, 3, 4, and 5 all have a db connection, SQL, result set loop, and HTML all nicely mixed together into a tables-based layout…

CI is the way to learn :)

Alexander Springmeyer wrote on

Agreed, it is that fine line between keeping up with the new stuff and picking up the old stuff.

As a new programmer, I raised myself on PHP; whereas my mentor had been programming since the card-reader (forget the technical term!) days…

I always wonder if learning a little bit of assembly, and messing around in C/C++ would strengthen my skills in PHP as far as code logic and understanding… (I am most sure it will! Just not much time to do so…).

I have learned alot programming with CI, more specifically from the forums where some of the more experienced programmers actually interact…

Lorenzo wrote on

Actually I don’t think that learning CI before php itself is a good idea.

Maybe with someone like you that explains it really well.

Otherwise plain old php will make it easier to understand what you are doing in CI.

Pat Teglia wrote on

Personally, I have to say that on two different levels I have found learning *with* CodeIgniter to be beneficial.  When using starter books to learn a language, I was always getting stuck trying to warp the way they did their example into my means.  Using your example, I was trying to use guestbook comments code to build a blog.  Not the best way. 


Secondly, going out of the books on straight PHP didn’t produce fast results, which frankly is what keeps me interested enough in a book to keep my momentum.  Learning alongside CI has helped me keep up a very good pace, and helped me actually understand the books I was reading much better, and vice-versa.  Now I can use the more advanced book, and the PHP.net site, as resources that I understand better (as opposed having my brain just shut down from some of the articles, because they were so over my head). 


Also, CI gives a structure, a MVC framework (that’s the point I gather) that you just don’t learn from the books.  They start out all about syntax, and mostly neglect how to organize a program.  CI gets straight to organizing.


A long time ago, when I was programming for a little printer company, they gave me a book called “Code Complete” that teaches you programming from the organization angle rather than from the syntax angle.  Think I should have started re-learning programming (been like 10 years) from that book again.


Great blog Derek, and btw, I like the glass headed one the best.  Do they all have names yet?