This article to share the content is about PHP in the const and global, has a certain reference value, the need for friends can refer to
CONST constant
Description
1. Must be the initial value at the time of definition; 2. no modifier in front; 3. The variable name is generally capitalized; 4. Constants can be inherited by quilt class;
5. A constant belongs to a class, not to an object's
"Action" when certain values are fixed, use const
What is thedifference between the constants defined by the const and the constants defined by the Define ()?
Want a member variable not to be modified such as Pi 3.1415926
definition:Const constant NAME = value; No $ symbol
Access: class Name:: Constant Name or interface name:: Constant Name
and:defined (' TEXT '); Checks whether a constant of a name exists
<?php class a{const tax_rate=0.08; public Function Paytax ($var) {return $var *a::tax_rate;}} $a =new A (); Echo $a->paytax (:?>)
Usage One:const is used for class member variables, once defined cannot be modified,define is used for global constants , is not used for definition of class member variables, const can be used in classes , define cannot.
Usage Two: const defines a constant large lowercase sensitive, while define can specify case sensitivity by a third argument (true for case insensitivity). Defines a constant at run time. Define (' TXE ', 100,true);
Usage Three:const cannot define a constant in a conditional statement, and the Define function can. if ($a >10) {define (' LE ', ' Hello ');}
Global
"Citation: http://www.phptd.com/?action-viewnews-itemid-6147"
First, super global variable $globals
There are many PHP hyper-global variables, such as the following are the Hyper global Variables (superglobal):
$GLOBALS, $_server,$_get,$_post,$_files,$_cookie,$_session,$_request,$_env.
Official Note:
$GLOBALS-References all variables available in the global scope.
A globally combined array that contains all the variables. The name of the variable is the key of the array.
That is, the global variables that have occurred can be obtained by $globals this array.
In the PHP life cycle, the so-called global variables that are defined outside the function body cannot be obtained directly inside the function .
$foo = "Example content"; test (); function test () { $foo = "local variable"; Echo ' $foo in current scope: '. $foo. "<br>"; Echo ' $foo in global scope: '. $GLOBALS ["foo"]. "<br>";}
As on the example above, to access the external $foo must use the $GLOBALS array. This is also true for external global variables that come in through the include file.
In PHP, global also has this function, which differs from $globals in that:
Global in the function produces an alias variable that points to the external variable of the function, not the actual external variable of the function.
$GLOBALS [] The actual call is an external variable, and inside and outside the function is always consistent.
For a member variable in a class, the function in the class must be accessed using $this->, and cannot be used in $globals mode:
The role of global is to define globals, but this global variable is not applied to the entire site, but to the current page, including all files of include or require.
Second, the example explanation
function T1 () { global $var 1, $var 2; $var 2 = & $var 1;} Function T2 () { $GLOBALS [' var3 '] = & $GLOBALS [' var1 '];} $var 1 = 5; $var 2 = $var 3 = 0;t1 ();p rint $var 2. " \ n "; T2 ();p rint $var 3." \ n ";
The result of the execution is:
0
5
Why not 2 5 instead of a 0 and a 5?
Revise the example again:
function T1 () { global $var 1; $var 1 = 2; Unset ($var 1);} Function T2 () { $GLOBALS [' var1 '] = 3; unset ($GLOBALS [' var1 ']);} $var 1 = 1;t1 ();p rint $var 1. "\ n"; T2 ();p rint $var 1. "\ n";
The execution result is entered only one 2;
1,$GLOBALS is an array that is automatically formed by all defined global variables . The variable name is the index of the array. That is, $globals[' var1 '] is the same variable as the variable $var1 outside the function, so when the $globals[' var1 ' is removed, the variable no longer exists and all cannot be output.
Note: $GLOBALS is an automatic global variable. This means that it works in all scripts. You do not need to use the global $GLOBALS in a function or method; To access it.
2,"Global $var 1;" is the alias variable "$var 1" that produces the external $var1 of the function, which is not a true function external variable, he only exists inside the function , so even if the alias variable is deleted within the function, it does not affect the outside variable, but can modify the value of the function external variable.
Perhaps some people always want to know the difference between this or that: in the PHP program, including other programs in the study, self-experiment, according to the results of thinking, sometimes more than the Internet search may come faster, more accurate. Let's take a look at the global scope of PHP access to variables to do?
Example one: Global defines a globally variable.
function Test_global () { global $var 1; $var 1 = ' OK '; Unset ($var 1);} Test_global (); $var 2 = & $var 1;unset ($var 1); Echo $var 2;
Do not give the results first, run the program yourself. Variables inside the function can be accessed. As can be seen from the results, unset just disconnects the variable name from the value of the variable, does not immediately destroy the value of the variable, and the global variable defined inside the function, the actual external only uses the alias inside the function, so we can still access the $var1 outside.
Example two: $GLOBALS a variable defined outside the function to access the function.
$waibu = ' out '; function ff () { echo $GLOBALS [' Waibu '];} FF ();
Using $waibu directly inside a function can be an error.