PHP Coding Specification

Source: Internet
Author: User
Tags scalar

PHP Coding Specification


The number of characters per line of code should be controlled within 80

For files that contain only PHP code, omit the end-of-file tag

Indentation should be able to reflect the logical result of the code, use four spaces as much as possible


Data type


Data types are collectively referred to as a set of data with the same characteristics

Data types are used to differentiate between different data, and because the data is stored at different capacities, different data must be allocated to the memory space to be stored, so the data is divided into different data types.


In PHP, data types are involved wherever data is involved. Data types are involved in places such as variables, parameters and return values of functions, properties of objects, and so on.


PHP supports 8 primitive data types and three pseudo-types.

Among the raw data types are:

-Scalar types (scalar type)

Boolean (Boolean)

Integer (integer type)

Float (float type)

String (String)

-Composite types (Compound type)

Array (arrays)

Object (Objects)

-Special types (special type)

Resource (resources)

Null (null value)


Integral type


Integers can be expressed in decimal, 16, octal, or binary notation

-binary digits, must be preceded by 0b in the number

-Eight digits, the number must be preceded by 0

-16 digits, before the number must be added in 0x

If an illegal number is passed to octal, the remaining digits are omitted


Storage range of integral type

Integer word length and platform-related

The usual maximum value is approximately 2 billion (32-bit signed)

The maximum value under a 64-bit platform is usually approximately 9e18

<?phpecho Php_int_max; The maximum value of the output integer echo php_int_size; The word length of the output integer value


Output statement

Output one or more strings can be implemented with the Echo statement

The syntax structure of the ECHO statement

void Echo (String $arg 1 [, String $ ...])

<?phpecho ("Hello World");      Output Hello Worldecho "Hello World"; Output Hello Worldecho "Hi,", "Hello World"; Output Hi,hello Worldecho 3//Output 3, Number 3 has been converted to string "3"

If you want to pass multiple arguments to echo, you cannot use parentheses

All output parameters are converted to string types

If you want to print the structure information for one or more expressions, including the type and value of the expression, you can do so by using the Var_dump function.

The syntax structure of the Var_dump function:

void Var_dump (mixed $expression [, mixed $ ...]

<?php

Var_dump (' php '); Output string (3) "PHP" Var_dump (4); Output int (4)


Using functions


A function is a named stand-alone code snippet. The function is used to perform a specific task and can return a value to the program that called it.

function type has

-system functions, functions provided by the PHP core or a specific library of functions

-Custom functions, functions that the user customizes as needed

Methods for calling functions

return value function name ([parameter,...] )


Integral type Overflow


If the number exceeds the range of integers, it will be interpreted as float

If the result of the operation is outside the integer range, the float is returned

<?php$d = 2147483649;var_dump ($d); Output float (2147483649) $n = 2147483640;var_dump ($n); Output int (2147483640) $t = 88;var_dump ($t); Output int ($f = $n + $t; var_dump ($f); Output float (2147483728)


Floating point Type


Floating point (also known as floating point float, double-digit or real real)

Word length and platform correlation for floating-point (float) data

Typically the maximum value is 1.8e308 and has a precision of 14-bit decimal digits


Boolean type


Boolean uses the keyword True/false representation. Both are case insensitive

Boolean results are typically passed to the control Flow program statement.

<?php$ishot = true; $isDeleted = False;var_dump ($isHot); output bool (TRUE) Var_dump ($isDeleted); output bool (FALSE)


Character type


String strings consist of a series of characters, where no character is equal to a single byte. This means that PHP can only support a 256 character set and therefore does not support Unicode.

The string can be up to 2GB maximum.


How strings are expressed


Single quotation marks

Double quotes

HEREDOC syntax structure

NOWDOC syntax structure

<?php$str1 = ' php '; $str 2 = "String"; Var_dump ($str 1); Output string (3) "PHP" Var_dump ($str 2); Output string (6) "string"


Escape character


\n
\r return
\t horizontal tab
\v vertical tab
\f
\\
\$
\ '
\ "


String type


Declare the character creation type variable and then output

Declare a variable of type string with escape character, then output, and view the results by viewing the browser source code


Quotation marks for escape characters and quotation marks for HTML entities

In PHP code, if you need to print directly to the browser, the double quotation mark/single quotation mark must be represented by the HTML entity, and if the quotation marks need to appear in the source code of the browser, then the escape character must be used.


The difference between single and double quotation marks

Single quotes can only parse \ ' and \ \; Double quotes resolve all escape characters.

Variables within single quotation marks cannot be parsed, and variables within double quotes can be parsed.

It is recommended that the string be represented as single quotation marks. This can increase the resolution speed


Curly brace Syntax

<?php$str = ' app ', Echo <p> the $STR is the abbreviation of application</p> ", echo" <p> I bought an $s Trle</p> ";

Results

The app is the abbreviation of application

I bought an

When the PHP parser encounters a dollar sign ($), the parser tries to combine multiple identities to form a valid variable name. You can use curly braces to clarify the line of variable names.

Correct wording

<?php$str = ' app '; Echo <p> the {$STR} is the abbreviation of application</p> "; echo" <p> I bought an ${str}le</p> ";


The characters in string can be accessed and modified in the form of a 0-based subscript that contains the corresponding number in curly braces.

<?php

$STR = ' application '; Echo $str {4}; Output i$str{4} = ' t '; echo $str {4}; Output T

Writing with an subscript that exceeds the length of the string will lengthen the string and fill it with spaces. Bidding clubs are converted to integers under non-certificate types.


HEREDOC syntax structure

<?php$str = <<< EOD Example of string spanning multiple lines using Heredoc Syntaxeod;? >

The identifier used at the end must be in the first column of the row.

End identifier This line will not contain additional characters except for extra points.

The HEREDOC structure can parse the escape character.

Variables within the HEREDOC structure can be parsed.


NOWDOC syntax structure

<?php$str = <<< ' EOD ' Example of string spanning multiple lines using Heredoc Syntaxeod;? >

The identifier used at the end must be in the first column of the row.

End identifier This line will not contain additional characters except for extra points.

Nowdoc structure does not parse the operation


Composite type

Allows multiple items of the same type to be aggregated to represent an entity.

Composite types can store multiple data items


Classification of composite types

Arrays (Array)

Objects (object)

<?php$arr = Array (34,67,8,25);   Array $obj = new StdClass ();     Object echo GetType ($arr);     Output Arrayecho GetType ($obj); Output object?>


Special types

Provides a type of special purpose that cannot be classified into any other type

Special types include

-null value (NULL)

-Resources (Resource)


Null value

Null indicates that a variable has no value

A variable in one of the following situations is considered null

-Variables with no assigned value

-variable assignment is null

-variables destroyed using the unset function

<?phpvar_dump ($username); Output Null$password = Null;var_dump ($password);    Output Null$age = 26;unset ($age); Var_dump ($age); Output null


Resources

A special data type used to represent a PHP external resource, such as database access, file access, directory manipulation, image manipulation, and so on.

Programmers will never be able to directly declare variables of this type, and must be accessed through specialized functions.


Pseudo type

Essentially not a real data type

A "self-styled" data type to illustrate certain situations, including:

-Mixing (mixed)

-Value (number)

-Callback function (callback)

-void


Mixed

Mixed description parameter/return value can be a variety of different types

Mixed is not necessarily all data types

For example: the GetType () function can receive all types of arguments, and the str_replace () function can receive arguments for string types and array types.

<?php$i=3; $n =0.5; $t = ' 1 '; Echo GetType ($i); Echo GetType ($n); Echo GetType ($t);


Numerical

Numbet Description parameter/return value can be integer or float

For example, the POW () function can accept an integer or float parameter, and the data type of the return value may also be an integer or floating-point number.

The POW function is used to implement a power operation

Syntax: Number pow (Nukber $base, number $exp)

<?phpvar_dump (POW (2,8)); Var_dump (Pow (2.5,2));


callback function

The callback function takes a user-defined function as a parameter. It can also be a method of an object, including a method of a static class.


void

void as a parameter list means that the function does not accept any arguments

void as the return type means that the return value of the function is useless

For example: the Date_default_timezone_get () function does not accept any arguments

The Date_default_timezone_get () function is used to get the time zone used by the DateTime function

Syntax: string date_default_timezone_get (void)

<?phpecho Date_default_timezone_get ();


Data Type Summary

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/85/7E/wKiom1elflKg3_LBAACq69VqUZA579.png "title=" Data type. png "alt=" Wkiom1elflkg3_lbaacq69vquza579.png "/>


Variable handling functions


Variable handling functions are part of the PHP core. That means it's as long as the Web server supports PHP, which supports variable handling functions


Is_int, Is_integer, Is_long functions can detect whether a variable is an integer

Syntax: BOOL Is_int (mixed $var)

<?phpvar_dump (Is_int (23)); BOOL (TRUE) Var_dump (Is_int ("23")); BOOL (FALSE) Var_dump (Is_int (23.5)); BOOL (FALSE) Var_dump (Is_int (NULL)); BOOL (FALSE)


Variable handling functions also include


Is_float, is_double, is_real detect if the variable is floating-point

Is_string detecting if a variable is a string type

Is_bool, Is_boolean whether the detection variable is a Boolean type

Is_scalar detecting if a variable is a scalar type

Is_arry detects if a variable is an array

Is_object detecting if a variable is an object

Is_null detecting whether a variable is a null value

Is_resource detecting if a variable is a resource

Is_numeric detect if a variable is a numeric or numeric string

Is_callback detecting if a variable is a callback function


The difference between is_null, empty and Isset


The Is_null function detects if the variable is null. Returns true if NULL, otherwise false

Syntax: BOOL Is_null (mixed $var)

The empty function detects whether a variable is empty. Empty () returns False if the variable is a non-null or nonzero value; otherwise, returns True

Syntax: bool Empty (mixed $var)

Isset function The Queen detects if the variable is set and is not NULL

Syntax: bool Isset (mixed $var [, mixed $ ...] )


An expression GetType ($n) Is_null Empty Isset
$n Null True True False
$n = ""; String False True True
$n = null; Null True True False
$n = Arrary (); Array False True True
$n = true; Boolean False False True
$n = false; Boolean False True True
$n = "0"; String False True True
$n = 0; Integer False True True









This article is from the "rime" blog, please be sure to keep this source http://kinrey.blog.51cto.com/10492082/1835376

PHP Encoding Specification

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.