PHP coding standards

2019-01-25 09:14发布

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/phpcodingstandard/

and this question on SO.

However, none of that quite gets to what I'm specifically wondering about, which is the integration of HTML and PHP. For example:

  1. is it OK to have a PHP file that starts out with HTML tags and only has PHP inserted where needed? Or should you just have one section of php that contains everything?
  2. If you have a chunk of PHP code in the middle of which is a set of echo's that just output a fixed bit of HTML, is it better to break out of php and just put the HTML directly?
  3. Should functions all be defined in dedicated php files, or is it ok to define a bunch of functions at the top of a file and call them later on in that same file?

There's probably other questions I'd like to ask, but really I'm looking for someone to point me at some kind of resource online that offers guidance on the general idea of how HTML and PHP should be combined together.

6条回答
贪生不怕死
2楼-- · 2019-01-25 09:56

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

查看更多
Root(大扎)
3楼-- · 2019-01-25 09:57

There's really not a single, common standard for these things. Most languages are more restrictive than PHP in this sense.

In the later years, a lot of so-called frameworks have emerged, and amongst other things they define a set of rules for everything from naming over where to place files and to which style your code should follow. There are several frameworks around, so you can't really pick one and call it the standard. However, most frameworks have a subset of commonality. For example, most follows some variant of the PEAR Coding Standards.

查看更多
beautiful°
4楼-- · 2019-01-25 10:04

Combining programming code and output data (including HTML) is IMHO a very bad idea. Most of the PHP gurus I know use a template engine such as Smarty to help keep the two things separate.

查看更多
时光不老,我们不散
5楼-- · 2019-01-25 10:10

I usually try and follow the standards that are set by the language's core libraries.... oh wait.

Seriously through - you should try and follow the MVC pattern in any web application as it is pretty much standard practice regardless of language. In PHP this can be achieved in a quick-and-dirty way by treating index.php as your controller and separating data logic and presentation by file. This small step will at least let you move your code to a full featured framework when and if you choose.

查看更多
我欲成王,谁敢阻挡
6楼-- · 2019-01-25 10:14
WHAT ARE CODING STANDARDS?

Coding standards (also sometimes known as ‘Coding Conventions’ or ‘Coding 
Rules’) are a set of guidelines that a group of developers stick to, to ensure 
that they are all essentially “singing from the same hymn sheet”. It means 
that they all code in the same format and therefore, regardless of who is next 
to work on a particular bit of code, the syntax and formatting used will be 
instantly familiar.
Naming conventions

Use of proper naming conventions is considered good practice. Sometimes programmers tend to use X1, Y1, etc. as variables and forget to replace them with meaningful ones, causing confusion.

In order to prevent this waste of time, it is usually considered good practice to use descriptive names in the code since we deal with real data.

Example: A variable for taking in weight as a parameter for a truck can be named TrkWeight or TruckWeightKilograms, with TruckWeightKilograms being the more preferable one, since it is instantly recognisable. See CamelCase naming of variables.

查看更多
霸刀☆藐视天下
7楼-- · 2019-01-25 10:19

Coding standards should be more than how to layout your syntax, but sadly that's what they tend to be in PHP.

FWIW, phc will pretty print your code in the Zend style.

查看更多
登录 后发表回答