PHP Code specification (1)

Source: Internet
Author: User

1. Include try not to use short tags such as

<?

#coding

?>

2. Ignore the end tag to prevent other characters or spaces from affecting the code

<?php
$foo = ' Foo ';

Occurs if no compression or cache output is turned on

If other code contains the file, other files are

<?php

?>

An error occurred after the space.

3. Indentation should be able to reflect the logical results of the code, use four spaces as much as possible, and Disable Tab tab, as this guarantees flexibility across the client programmer software.
For example:
if (1 = = $x) {
$indented _code = 1;
if (1 = = $new _line) {
$more _indented_code = 1;
}
}

4. Variable name by type plus keyword

Example: string: $sMyName

$oPersona $resource $arrMyArray

5. Database

Database tables are completely lowercase, stored procedures use prefix PROC_, triggers use Tri_, event uses event_, and views use View_.

6. Magic Numbers

<?php define("GREETING","Hello world!",TRUE); Echo constant ("greeting");? >

Define (tax,1.05);

$mytax =tax* Tax;

7. Line break
UNIX series with \ n
Windows series with \ r \ n
Mac with \ r
PHP can be replaced with Php_eol to improve code-level portability

8. printf (); formatting strings, especially SQL statements

<?php$str = "Hello"; $number = 123;printf ("%s World"). Day number%u ", $STR, $number);? >

9. When a class member method is declared as private, it must begin with a double underscore "__", respectively, and must be preceded by a single underscore "_" when declared as protected; The method is not underlined. For example:
Class Foo
{
Private Function __example ()

    {
// ...
}
protected function _example ()
    {
// ...
}
Public Function Example ()
{
// ...
}
}
10. If we need to define some of the frequently used methods as global functions, then we should define them in the class statically (static). For example:
Class Think
{
// ...
static public Function AutoLoad ($classname)

{
// ...
}
}

11. Variables in variable substitution are only allowed in the form of $+ variable names. For example:
$greeting = "Hello $name, welcome back!"; Allow
$greeting = "Hello {$name}, welcome back!"; Allow

12. Declarations of any class variable must be placed at the top of the class, preceded by the declaration of any function.

13 statically static methods should declare their scope and should not be declared private privately, but should be protected or public, and if they do not want to inherit from the quilt class, they should be declared with final

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.