PHP coding standards

前端 未结 5 1742
猫巷女王i
猫巷女王i 2020-12-17 20:28

I\'ve been looking for some guidelines on how to layout PHP code. I\'ve found some good references, such as the following:

http://www.dagbladet.no/development/phpcod

5条回答
  •  粉色の甜心
    2020-12-17 20:55

    Use a template engine when you can. If you haven't learned one, or don't want the overhead (which is minimal), use practices that cause you to have quick-and-dirty templating:

    • Functions that do not display anything have no place in a file that does display something.
    • Print variables, not HTML. Whenever outputting HTML, break out of the PHP and write HTML with print statements to handle any small details that are needed (actions for forms, IDs for controls, etc.).
    • Remember, when you include a file that breaks out of the PHP to print content, that will be treated the same as if you do it in the main file. So you can create simple templates that just included PHP files, and those files will print variables in the right places. Your index.php (or whatever) does all the real work, but all the display is done by the secondary "template".

    Many PHP tutorials intermix logic and display code. It took me years to break the bad habits that encouraged in me. In my experience you can't separate things too much in PHP, and entropy will pull you towards intermixed code if you don't fight it.

提交回复
热议问题