Read the PHP manual a little bit (1)

Source: Internet
Author: User
Tags parse error setcookie type null

1. To output large pieces of text, jumping out of PHP parsing mode is usually more efficient than passing text through the Echo or print output (manual)

2. Note: For the distribution and consistency of the code, make sure not to use the short mark, the short mark can only be used by the instruction Short_open_tag in the php.ini configuration file, or the--enable-short-tag option is added when PHP is compiled. After php5.4, the short-form echo tag <?= is always recognized and legal, regardless of the Short_open_tag setting. Manual

3. When converting to Boolean, the following values are considered false: (manual)

    • The Boolean value false itself
    • Shaping value 0
    • Floating-point value 0.0
    • Empty string and string "0"
    • An array that does not include any elements
    • Objects that do not include any member variables
    • Special type NULL
    • SimpleXML object generated from an empty tag

All other values are considered ture (including any resources)

4. If a given number exceeds the range of integers, it will be interpreted as float. Also returns float if the result of the operation is outside the integer range. Usually the maximum value is approximately 2 billion (32-bit sign), 9E18 (64-bit sign).

PHP does not have an divisible operator, and 1/2 produces float0.5. The value can be cast to integer by discarding the fractional part, or better rounded with the round () function.

Converting from a Boolean value, False speaking produces a 0,true that produces 1. (manual)

5. Never trust that the floating-point numbers are accurate to the last, and never compare two-fu TVs for equality.

Some mathematical operations produce a result that is represented by a constant Nan. This result represents a value that is undefined or not expressed in the floating-point budget, any comparison of this value to its value (including itself), and the result is false, which should be checked using Is_nan (). Manual

**6. Unlike double quotes and HEREDOC syntax structures, the escape sequences of variables and special symbols in single-quote strings will not be replaced. More on the use of strings see (manual)

\ n newline (LF or 0x0A in ASCII character set (10))
\ r Enter (CR or 0x0D in ASCII character set)
\ t /td> Horizontal tab (HT or 0x09 (9) in the ASCII character set)
\v Vertical tab (VT or 0x0 in ASCII character set B (11)) (Since PHP 5.2.5)
\e Escape (ESC or 0x1B (27) in the ASCII character set) (from PHP 5.4.0
\f page Break (FF or 0x0C (12) in the ASCII character set) (since PHP 5.2.5)
\ \ Backslash
\$ USD flag
\ " double quotes
\[0-7]{1,3} conforming to the regular expression sequence is a character that is expressed in eight binary
\x[0-9a-fa-f]{1,2} conforming to the regular expression sequence is a character that is expressed in 16 binary

7. The array in PHP is actually an ordered map, and the map is a bar of values associated to the keys type, because the value of the array element can also be an array, so the tree structure and multidimensional arrays are also allowed. Code

You should always enclose the array index in string notation, but not always with the key name, without giving the key a constant or variable quotation marks, or PHP cannot parse them.

8. There is no need to initialize variables in PHP, but it is a good practice to initialize variables, uninitialized variables have default values of their type-the default value for Boolean types is False, the default value for shaping and floating-point variables is zero, the default value of a string variable is an empty string, and the default value of an array variable is an empty array. The default value that relies on uninitialized variables can be problematic in some cases, such as encountering the same variable name when one file is included in the other. In addition, register_globals open a major security risk. Using an uninitialized variable emits a e_notice error, but does not add a unit to an array that is initialized. The isset () language structure can be used to detect whether a variable has been initialized. Code

9. (manual)

<? PHP $a = 1; /*  */function  Test ()       {echo$a/*  */ }test ();? >

This script will not have any output, because the Echo statement refers to a local version of the variable $ A, and within that range, he is not assigned a value.

Global variables in PHP must be declared as global when used in functions, or use special PHP custom $GLOBALSS arrays.

A static variable exists in the local function, but its value is not lost when the program executes away from the scope. (a static variable cannot be assigned a value with the result of an expression, or it causes a parse error, which is actually parsed at compile time.) )

In Zend Engine 1, which drives the PHP4, the static and global definitions of variables are implemented in a reference way, for example, importing a real global variable inside a function domain with a global statement actually establishes a reference to a global variable.

10. Variable variables for arrays, must solve an ambiguous question, such as Write down $$[1], the parser needs to know if you want to $a[1] as a variable, or you want $ $a as a variable and take out the value of the variable indexed to [1], the syntax for solving this method is to use ${for the first case $a [1]}, for the second case with ${$a}[1].

Note that in PHP's methods of functions and classes, a hyper-global variable cannot be used as a mutable variable. $this variable is also a special Envoy variable that cannot be dynamically referenced. Manual

PHP transparently supports HTTP cookies. Cookies are user mechanisms that store data on a remote browser and can track or recognize re-access, and can use the Setcookie () function to set cookies. Cookies are part of the HTTP message header, so the Setcookie function must be called before any output is sent to the browser. The same restrictions apply to the header () function. Cookie data is available in the appropriate cookie data set, more details (manual)

Note A cookie in the browser replaces the previous cookie with the same name, unless the path or domain is different. So for the shopping cart program you can keep a counter and pass it together.

 <? php  if  (isset  ($_cookie  [' Count ' ]) {  $count  = $_cookie  [' Count '] + 1;}  else  {  $count  = 1 ;}  setcookie  (' Count ',  $count , time  () +3600); setcookie  ("Cart[ $count ]",  $item , time  () +3600); 

12. Like Superglobals, constants are scoped to the global scope, and constants can be accessed anywhere in the script without a tube scope. Manual

Constants can be defined with the Define () function, and after php5.30, constants can be defined outside the class definition using the Const keyword. Once a constant is defined, it cannot be changed or undefined.

Note: Instead of defining constants using define (), using the Const keyword to define constants must be at the top of the scope, so this method is defined at compile time. This means that constants can no longer be defined within a function, within a loop, or within an if statement.

several "Magic constants" for PHP
name Description
__LINE__ The current line number in the file.
__FILE__ The full path and file name of the file. If used in the included file, returns the file name that is included. Since PHP 4.0.2, __FILE__ always contains an absolute path (if the symbolic connection is the resolved absolute path), and the previous version sometimes contains a relative path.
__DIR__ The directory where the file resides. If used in the included file, returns the directory where the included files are located. It is equivalent to dirname (__file__). Unless it is a root directory, the name in the directory does not include the trailing slash. (New in PHP 5.3.0) =
__FUNCTION__ The name of the function (PHP 4.3.0 new addition). From PHP 5 This constant returns the name (case-sensitive) when the function is defined. In PHP 4, this value is always in lowercase letters.
__CLASS__ The name of the class (PHP 4.3.0 new addition). From PHP 5 This constant returns the name of the class when it is defined (case-sensitive). In PHP 4, this value is always in lowercase letters. The class name includes its declared action area (for example, Foo\bar). Note since PHP 5.4, __CLASS__ has also worked for trait. When used in the trait method, __class__ is the name of the class that invokes the trait method.
__TRAIT__ Trait's name (PHP 5.4.0 new Plus). From PHP 5.4 This constant returns the name of the trait when it is defined (case-sensitive). The Trait name includes its declared function area (for example, Foo\bar).
__METHOD__ The method name of the class (PHP 5.0.0 new addition). Returns the name of the method when it is defined (case-sensitive).
__NAMESPACE__ The name of the current namespace (case-sensitive). This constant is defined at compile time (PHP 5.3.0 is new).

  

Read the PHP manual a little bit (1)

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.