Summary of various methods of defining variables in PHP

Source: Internet
Author: User
Tags http cookie http post parse error php define phpinfo scalar variable scope
This article mainly introduces the various definition variables in PHP method summary, the need for friends can refer to the following

1. Define constant define ("CONSTANT", "Hello World");

Constants can only contain scalar data (boolean,integer,float and string).

When calling a constant, simply use the name to get the value of the constant, not the "$" symbol, such as: Echo CONSTANT;

Note: Constants and (global) variables are in different namespaces. This means, for example, that TRUE and $TRUE are different.

2. Common variable $ A = "Hello";

3. Variable variable (use two dollar sign ($))

$$a = "world";

Each of the two variables is defined:

$a content is "hello" and $hello content is "world".

Therefore, it can be expressed as:

echo "$a ${$a}" or echo "$a $hello"; they all output: Hello World

To use mutable variables with arrays, you must address an ambiguous question. This is when you write the $ $a [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 problem is to use ${$a [1] for the first case, and ${$a}[1 for the second case).

4. Static variables

Inside the functionstatic $a = 0;

Note: Assigning a value to a declaration with the result of an expression results in a parse error such as static $a =3+3; (error)

Static variables exist only in the local function domain (inside the function), and after the function is executed, the value of the variable is not lost and can be used for recursive invocation

5. Global Variables

Global variables defined in the function body can be used outside the body, the global variables defined outside the function body cannot be used within the function body, and accessing variables globally can be customized $GLOBALS arrays with special PHP:

Such as:$GLOBALS["b"] = $GLOBALS["a"] + $GLOBALS["b"];

A real global variable imported in a function domain with the global statement actually establishes a reference to a global variable.

global $obj;

Note: The static and global definitions for variables are implemented in an applied manner

6. Assign a value to a variable: pass the address assignment (simple reference):

$bar = & $foo; Add & sign to the variable that will be assigned

Changing the new variable will affect the original variable, and this assignment is faster

Note: Only named variables can pass address assignment

Note: if

$bar = &$a;
$bar = &$foo;

Changing the value of $bar can only change the value of the variable foo without changing the value of a (the reference changed)

7.PHP Hyper global variable $globals:

Contains a reference to a globally valid variable that refers to each current script. The array's key is labeled as the name of the global variable. $GLOBALS array exists starting with PHP 3.

$_server: The variable is set by the WEB server or directly associated with the execution environment of the current script. Similar to the old array $HTTP _server_vars array (still valid but opposed to use).

$_get: A variable that is submitted to the script via the HTTP GET method.

$_post: A variable that is submitted to the script via the HTTP POST method.

$_cookie: The variable that is submitted to the script via the HTTP cookie method.

$_files: A variable submitted to the script via an HTTP POST file upload.

File upload form to have enctype= "Multipart/form-data"

$_ENV: Executes the variables that the environment submits to the script.

$_request: The variable that is submitted to the script via the get,post and COOKIE mechanism, so the array is not trustworthy. All the changes contained in the array

The presence or absence of quantities and the order of the variables are defined according to the Variables_order configuration instructions in php.ini. The array does not directly emulate the earlier version of PHP 4.1.0. See Import_request_variables ().

Note: since PHP 4.3.0, the file information in $_files no longer exists in $_request.

$_session: The variable currently registered to the script session.

How to disable Phpinfo ():

In php.ini

disable_functions = phpinfo()

Restart the Web server.

Constants in PHP

Constants can only be used with define (constant name, constant value);

Constants can only contain scalar data (boolean,integer,float and string).

You can simply get the value of the constant by specifying its name, and do not precede the constant with the $ symbol. If the constant name is dynamic, you can also use the function

Constant () to read the value of the constant. A list of all defined constants can be obtained with get_defined_constants ().

Note: Constants and (global) variables are in different namespaces. This means, for example, that TRUE and $TRUE are different.

If an undefined constant is used, PHP assumes that it wants the name of the constant itself, as if it were called with a string (CONSTANT corresponds to "CONSTANT"). An error of E_notice level is issued at this time. See why $w 3sky[bar] is wrong in the manual (unless you define bar as a constant in advance with define ()). If you only want to check if a constant is defined, use the defined () function.
Constants and variables are different:

* Constant preceded by no dollar sign ($);

* Constants can only be defined with the Define () function, not through assignment statements;

* Constants can be defined and accessed anywhere without regard to the rules of variable scope;

* Constants cannot be redefined or undefined once defined;

* The value of a constant can only be scalar.

Defining constants

<? PHP define ("CONSTANT", "Hello World"); Echo CONSTANT; Outputs "Hello world" echo Constant; Outputs "Constant" and issues a notice. ?>

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.