5 Bad Habits of PHP Developers (and How to Break Them)

When you code in PHP, do you still feel like a beginner?

Get more confident about your PHP skills with 5 learning strategies.
Get the free PDF now.

27 thoughts on “5 Bad Habits of PHP Developers (and How to Break Them)”

  1. Excellent post and you hit the mark with points 2 and 3. I would come across a problem that I set aside to “come back to later”. What happened is I finished another part of the project and was so happy to be (what I thought) finished that I completely forgot about the original problem.

    Comments. Yes. Comments are critical, especially to beginners and folks working on multiple projects. It is so easy to forget what a complex block of code does (complex in the eyes of the beholder). I mean, this is your code and you can’t remember what it’s doing.

    You’re cooking with gas in these posts. Keep up the great work.

    Reply
  2. Hi Alex!
    Thanks for making out time to give out these awesome tips FOR FREE; I really appreciate.
    … Hhhhhhmmmmmmm, I am guilty of four (4) of those aforementioned bad habits. I’m making serious adjustments in my codes and coding skills henceforth.
    Once again, thanks for sharing!

    Reply
  3. I think lack of comments is not a bad habit. It depends. Code should be broken down to smaller pieces that are self explanatory.

    Variable, method and class names should be logical and in charge of one thing only. I think that more than the lack of comments, lack of separation of logic is a bad habit. Cramming everything in one class.

    Sure, comments on parts with heavy logic or where something really specific happens. But I prefer clear and logic method names that do only what they are supposed to do.

    Great article!

    Reply
  4. THANK YOU, THANK YOU, THANK YOU.
    Being a NEWBIE the very first bad habit mentioned hit me right in the face. I will now begin to make sure that I understand each and every line and what every function is doing.
    The second bad habit that really got to me is writing comments. I can’t write good comments if I don’t understand everything the snippet is actually doing.

    Reply
  5. Bad habits are hard to break and even harder if you don’t realize that what you’re doing is undermining your work. If you know but don’t care—that would be the worst. But you’re here, aren’t you?

    Reply
  6. Thank you sir, about scaling web apps does it’s depends on the database coding or something else bcux most posts av come across keeps relying the web app scaling on database architecture or something can u please brief me about this issue

    Reply
    • Both the database design and the PHP code play a role in scalability.

      Optimizing the database design is a complex topic. It’s about choosing the proper table indexes, query optimization, views, and so on.

      PHP code optimization is somehow easier. Avoid creating loops with huge memory consumption, slow operations (especially on big arrays or large strings) and try implementing efficient algorithms when possible.

      Reply
  7. The most challenging bad habits now is TDD (Test Driven Development). Since we just write (amazing) code and never test using Unit test cases. As a developer we only consider the working part of the code and never think of any failure at run time.

    It’s better we should start practice for writing PHP unit test cases for each piece of code we write.

    Reply
    • Yes, test-driven development is by far the safest and most secure programming paradigm.
      Being relatively difficult and time demanding it’s not used often in web development, but I agree with you that it would lead to much higher quality code.

      Thanks for sharing your thoughts.

      Reply
  8. I have some questions for an application involving multiple classes and scripts.
    1. Files. I’ve historically defined file paths at the start of each script. Now I’m thinking I should have one class that defines every file path that everything in the app uses. ??
    2. Error checking. Should every method ensure the validity of all its input every time?
    Seems really repetitious. ??
    3. When do you rely on built-in functions throwing exceptions, like PDO or file_put_contents, and when do you write your own exception handlers. ??

    Reply
    • Hi John,

      About files: if you have many classes or “inc” files you always include, then it’s definitely a good idea to create a single script with all the include (or, better, require) statements. Better use absolute paths (__DIR__ is very helpful for that).

      About validity checks: it depends on your app workflow. Functions dealing with untrusted data must always validate their input; on the other hand, functions dealing with already validated data can safely avoid that step.
      I suggest you write specific validation functions and use them for input validation every time is necessary.

      About exceptions: if you are ok with the error details provided with build-in exceptions, it’s ok to use them. User-defined exceptions are useful when you need to pass more information about the error to the caller, or when you want every exception to use a user-defined Exception-inherited class.

      Reply
  9. Great article. Thank you for it!

    I really like the advice about writing comments – first comments, then code. I usually do it in the opposite order.

    I usually have one more issue that lowers my productivity: Watching YouTube videos while programming. Especially those with words or clips. I sometimes want to look at it just for a second, but then, five minutes later I realize I am watching it for quite some time.
    But on the other hand, there are videos on YouTube that help me concentrate, not only when I programme, but also when I learn something for school. Those are usually songs without video and words, the only melody. One of my favourites is Kurzgesagt soundtrack.

    Reply
    • Thanks for your comment, Jan!

      I suggest you avoid distractions while working. It’s perfectly fine to listen to some music (as you said, just music without words) which often helps concentration, but shifting focus from your work (such as programming) to other things like YouTube, social media and so on, is a productivity killer.

      The best thing is to do only one thing for a slice of time (30 minutes, 1 hour, 2 hours…) and do just that, without interruptions of any kind.

      Look into “Deep Work” by Cal Newport, it’s a amazing book on this topic.

      Reply

Leave a Comment