PHP Super Global variables, magic variables and Magic functions

Source: Internet
Author: User
Tags set cookie setcookie

PHP has pre-defined 9 Super global variables, 8 magic variables, and 13 magic functions, which can be used anywhere in the script without a declaration. These variables and functions are frequently used in PHP development, and these variables and functions can easily help us solve many problems. The following is a detailed explanation of the super global variables, magic variables, and magic functions in PHP.

PHP Super Global Variables (9)

$GLOBALS store variables in the global scope

$_server Getting server-related information

$_request getting the parameters for post and get requests

$_post get the form's POST request parameters

$_get GET request parameters for a form

$_files get the variable to upload the file

$_env getting an array of server-side environment variables

$_cookie Browser COOKIE operation

Set Cookie:setcookie (name, value, expire, path, domain);

Get cookie:$_cookie["user"];

Delete Cookie:setcookie ("User", "", Time ()-3600);//Set Expiration

Operation of $_session server SESSION

Be sure to Session_Start () Start session before using the session

Storage session:$_session["name"]= "King";//Array operation

Destroy Session:unset ($_session["name"]);//Destroy A

Session_destroy () and unset ($_session);//Destroy All SESSION

PHP Magic variables (8)

The current line number in the __line__ file.

The full path and file name of the __file__ file. If used in the included file, returns the file name that is included.

The directory where the __dir__ file resides. If used in the included file, returns the directory where the included files are located.

The __FUNCTION__ constant returns the name of the function when it is defined

The __CLASS__ constant returns the name of the class when it is defined (case-sensitive).

The method name of the __method__ 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).

PHP Magic Functions (13)

__construct () is called when an object is instantiated, and when __construct and a function with a class name are present, __construct is called and the other is not called.

__destruct () is called when an object is deleted or when an object operation terminates.

The __call () object invokes a method that is called directly if the method exists, or calls the __call function if it does not exist.

__get () When the property of an object is read, the property value is returned directly if it exists, and the __get function is called if it does not exist.

__set () Sets the property of an object, assigns the value directly if the attribute exists, or calls the __set function if it does not exist.

__tostring () is called when an object is printed. such as Echo $obj; or print $obj;

__clone () is called when the object is cloned. such as: $t =new Test (); $t 1=clone $t;

__sleep () serialize was called before. If the object is relatively large, want to cut a bit of the east and then serialize, you can consider this function.

__wakeup () Unserialize is called to do some initialization of the object.

__isset () is called when detecting whether an object's properties exist. such as: Isset ($c->name).

__unset () is called when a property of an object is unset. such as: unset ($c->name).

Called when the __set_state () call is Var_export. Use the return value of __set_state as the return value of Var_export.

__autoload () When an object is instantiated, the method is called if the corresponding class does not exist.

PHP supports 8 basic data types four types of scalar:
Boolean (Boolean): This is the simplest type, with only two values, which can be true/true or false/false, not case sensitive. For more information, see: Php Boolean Type (Boolean)
integer (integer type): In a 32-bit operating system, its valid range is:-2 147 483 648~+2 147 483 647. Integer values can be expressed in decimal, hexadecimal, or octal, preceded by an optional symbol (-or +). Octal indicates that the number must be preceded by 0 (0), and hexadecimal indicates that the number must be preceded by 0x. For more information, see: PHP Integer data (integer)
float (floating-point, also called double): The valid range in the 32-bit operating system is: 1.7e-308~1.7e+308. For more information, see: PHP Floating-point type (float)
String (String): Character variables differ from other programming languages in that they have characters and strings, and in PHP, you use character variables to define characters or strings. For more information, see: PHP String type

Two types of composite:
Array (arrays): An array type variable is a special type of variable that will be described in detail in subsequent chapters.
Object (objects): Objects are also a special type of data. To create an object variable, use the New keyword. For more information, see: PHP Object Type

Finally, there are two special types:
resource (resources): A source is a special variable that holds a reference to an external resource. Resources are created and used through specialized functions. For more information, see: PHP Resource Type (Resource)
null (NULL): Indicates that a variable has no value. The only possible value for a null type is NULL.

Note:
You might also read some references to the double-precision type. In fact double and float are the same, because of some historical reasons, both names exist at the same time.
The type of the variable is usually not set by the programmer, and is determined by the context in which PHP is used by the variable at run time. That is, PHP is a weakly typed language, and programmers do not need to declare variable types when declaring variables, and the PHP interpreter automatically recognizes them.

PHP Super Global variables, magic variables and Magic functions

Related Article

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.