Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

I just started learning php (my first programming language). I am reading Oreilly's Programming PHP 3rd Edition.

Can anyone give me any tips?

(please don't suggest other programming language, I have wasted countless hours trying to decide. I will eventually try to learn other languages but decided to start with php for now. My main interest is webapps, so it would seem that php is the most popular - and easy - choice.)



First, resist the urge to put code in your templates. Because the syntax is the same, it's very tempting to just declare a function or do a query in the middle of your HTML. You'll be happier in the long run if you don't ever get into that habit to begin with. PHP actually recognizes both .php and .phtml as valid file extensions.

I personally try to follow a couple basic rules. .php files should be a single <?php ?> block with nothing outside it, and .phtml files shouldn't use anything but if, foreach, and already defined variables. You'll end up with code that's both cleaner and better structured.

Second, if at all possible avoid Wordpress until you've already got a firm grasp of the language. Wordpress itself has a few decisions that made sense in PHP 4 but are terrible practice now (like using global functions instead of classes) and a few others that, while not technically wrong, were questionable even then (the loop). On top of that, the quality of code in third party themes and plugins varies wildly. If you copy the techniques you see there, you'll learn some awesome tricks, but you'll also learn every bad practice in the book. Until you've got enough experience to recognize the difference, you'll do yourself more harm than good.


> First, resist the urge to put code in your templates

Unless things have changed since I last looked, PHP files are all templates. Last time I used PHP, with symphony, I actually thought PHP was a pretty reasonable templating system.


PHP files are all templates, and when used properly they're relatively good at it. The problem is that, since there's no built in concept of separation, you have to enforce it yourself.

A lot of code written by beginners (myself included, once) will put business logic as close to where it will eventually be rendered as possible, so you end up with code that looks like this

    <ul>
      <?php
      // Thirty lines of database calls and string manipulation
      foreach ($results as $result): ?>
        <li><?= $result ?></li>
      <?php endforeach ?>
    </ul>
which makes intuitive sense but quickly leads to an unreadable template. It turns out that, even though you're writing these together, you'll rarely actually want to change them together. So you eventually learn to have template files and nontemplate files.

The .php vs .phtml distinction has no relevance to the compiler. They're only really useful to a human in making that template/code distinction explicit.


so, just choosing a random file off github, is this a template?

https://github.com/symfony/Process/blob/master/PhpProcess.ph...

Or in other words, php files are only templates if you use them that way.


If I have, say, a jinja2 template that only contains logic and outputs nothing, is it a template? I would say it is. By which reasoning, yes that php file is also a template. Even if you've got used to pretending it isn't.

Having said which, I'm confused by the lack of a closing ?>. Maybe PHP's parser lets you get away with that though.


Having said which, I'm confused by the lack of a closing ?>. Maybe PHP's parser lets you get away with that though.

It's completely optional.


...and best practice is to omit it. Since PHP is a templating language any whitespace (other than a single newline) that comes after the ?> will be printed directly to stdout when the file is parsed, which is almost never what the author intends. Letting the parser insert it for you avoids this class of bugs.


As someone who first picked it up in 1998 and has intermittently built apps in it, the biggest tip: use PDO. Many examples online, and what I learned to work with, target the MySQL libs directly, but PDO really saves you a lot of time, and preps you for the concepts of abstraction you find in a lot of platforms.



Absolutely - if you're just starting out then this is a fantastic PHP resource. I would make 2 other suggestions as well:

- Get into the habit of using PHPUnit early. If you haven't come across it yet there are plenty of tutorials on http://net.tutsplus.com/ that cover it's basic use. It may seem pointless to begin with but you'll be thankful later on.

- I know you said you didn't want suggestions for another language and I won't give you any. However alongside PHP I think it would be really beneficial to do a primer on basic computer science. An understanding of concepts such as language paradigms, design patterns and simple algorithms would inform your learning and explain why some of the advanced resources you come across later do things the way that they do.


Can anyone give me any tips?

Without you being more specific, I wouldn't know what to say other than build simple things with it, then more complex things, and keep looking at source of mature projects for inspiration and kicks. Also, modify existing things until they break, then do it again. As long as you're not dealing with sensitive data of yourself or others, don't be afraid of to just poke and pull at things, or to try an idea just to see if it works. Maybe some would advise against that, and maybe they're right, I am not a good programmer by any means. But I have fun doing it, and that's how I learnt.

Learning grammar and vocabulary is obviously the first step, but I think the real fun begins when you learn to express yourself freely, when the path between idea and execution gets shorter, and is less often interrupted by hunting down unexpected bugs. Even though that is possible without knowing all that much of the language, just by getting good at using the bits you do know, it still takes time in any case, and you will have to overcome frustrations. But no matter what language, it's worth it :)


Learn to love php.net, stackoverflow and this page here: https://www.owasp.org/index.php/PHP_Security_Cheat_Sheet, but don't necessarily trust the code you find on php.net and SO.

Comment religiously. You'll thank yourself in six months.

You'll save yourself a lot of time if you learn to use Composer (http://getcomposer.org) to manage dependencies, and a templating system (twig, mustache, smarty, what have you) and class autoloading. PSR-0 (https://github.com/php-fig/fig-standards/blob/master/accepte...) is annoying, but it is really convenient once you get past the overhead. If you're on Windows, it really helps to get PHP running from the command line.


Oh well, get ready for a world of inconsistencies! Php is a bit of a mongrel (mongrels are great though,) don't expect a beautifully designed language. The language has been bolted together piecemeal, with a bit of this and that. Function naming style is inconsistant (some with underscores, some without), some parameter patterns feel back to front, in fact don't bother trying to learn too many - rely on your editor and the documentation.

The bulk of my php coding really sits, in foreach loops, if statements (and comparison operators), and the use of arrays. Get to know arrays, build them, loop through them, echo out values, mutate them, they're your friend.

Learn about namespacing, functions and classes. Avoid globals but use the superglobals!

And as with all web programming, understand the browser/server lifecycle of a request. Start with the basics of HTTP.


avoid XML, make sure to read about JSON and sql databases.

google for REPL, than get one for php, so you can see results instantly. https://github.com/d11wtq/boris


Come join us in #phpmentoring on Freenode.




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

Search: