PHP constants and variables _ PHP Tutorial

Source: Internet
Author: User
PHP constants and variables. PHP constants and variables [constants] can be defined using the define () function. after PHP5.3.0, you can use the const keyword to define constants outside the class definition. Once a constant is set, PHP constants and variables
[Constant]
You can use the define () function to define constants. after PHP 5.3.0, you can use the const keyword to define constants outside the class definition. Once a constant is defined, it cannot be changed or undefined.
Constants can only contain scalar data (boolean, integer, float, and string ). Resource constants can be defined, but should be avoided as far as possible, because unexpected results may occur.
You can simply specify its name to obtain the constant value. unlike variables, you should not add the $ symbol before the constant. If the constant name is dynamic, you can use the constant () function to obtain the constant value. Use get_defined_constants () to obtain a list of all defined constants.
If you only want to check whether a constant is defined, use the defined () function.
Constants and variables are different as follows:
? There is no dollar sign before the constant ($ );
? Constants can only be defined using the define () function, but cannot be defined using the value assignment statement;
? Constants can be defined and accessed anywhere regardless of the scope of variables;
? Once defined, a constant cannot be redefined or canceled;
? The constant value can only be a scalar.
Predefined Constants

Many constants are defined by different extension libraries. they only appear when these extension libraries are loaded, dynamically loaded, or included during compilation. These special constants are case-insensitive, as follows:

Name Description
__LINE__ The current row number in the file.
__FILE__ The complete file path and file name. If it is used in a file to be included, the file name to be included is returned. Since PHP 4.0.2,__FILE__It always contains an absolute path (if it is a symbolic link, it is the absolute path after resolution), and the previous version sometimes contains a relative path.
__DIR__ The directory where the file is located. If it is used in included files, the Directory of the included files is returned. It is equivalentDirname (_ FILE __). Unless it is the root directory, the name of the directory does not include the slash at the end. (Added in PHP 5.3.0) =
__FUNCTION__ Function name (new PHP 4.3.0 ). Starting from PHP 5, this constant returns the name (case sensitive) when the function is defined ). In PHP 4, the value is always lowercase letters.
__CLASS__ Class name (new PHP 4.3.0 ). Starting from PHP 5, this constant returns the name (case sensitive) when the class is defined ). In PHP 4, the value is always lowercase letters. The class name includes the declared region (for exampleFoo \ Bar). Note that _ CLASS _ from PHP 5.4 also applies to trait. When used in the trait method, __class _ is the name of the CLASS that calls the trait method.
__TRAIT__ Trait name (new in PHP 5.4.0 ). Starting from PHP 5.4, this constant returns the name (case sensitive) of the trait definition ). The Trait name includes the declared region (for exampleFoo \ Bar).
__METHOD__ Class method name (new PHP 5.0.0 ). Returns the name (case sensitive) when the method is defined ).
__NAMESPACE__ Name of the current namespace (case sensitive ). This constant is defined during compilation (new in PHP 5.3.0 ).
Variable]
Variables in PHP are represented by a dollar sign followed by the variable name. Variable names are case sensitive. Variable names follow the same rules as other labels in PHP. A valid variable name must start with a letter or underline followed by any number of letters, numbers, or underscores.
By default, values are always assigned. That is to say, when the value of an expression is assigned to a variable, the value of the original expression is assigned to the target variable. This means that, for example, changing the value of one variable when the value of a variable is assigned to another variable does not affect another variable. PHP also provides another way to assign values to variables: assign values to references. This means that the new variable simply references the original variable (in other words, "become its alias" or "point. Changing new variables will affect the original variables, and vice versa. Assign values by reference. &Add the symbol to the variable to be assigned a value (source variable ).
Predefined variables
In PHP 4.2.0 and later versions, the default value of the PHP command register_globals is off. This is a major change in PHP. Setting the value of register_globals to off affects the validity of the predefined variable set within the global range. For example, to obtain the DOCUMENT_ROOT value, you must use $ _ SERVER ['document _ root'] instead of $ DOCUMENT_ROOT, as shown in the following figure, use $ _ GET ['id'] instead of $ id from URL http://www.example.com/test.php? In id = 3, get the id value, or use $ _ ENV ['home'] instead of $ HOME to get the value of the environment variable HOME.
? Hyper-global variables are built-in variables that are always available in all scopes.
? $ GLOBALS-reference all available variables in the global scope
? $ _ SERVER-SERVER and execution environment information
? $ _ GET-http get variable
? $ _ POST-http post variable
? $ _ FILES-variables for uploading HTTP FILES
? $ _ REQUEST-HTTP Request variable
? $ _ SESSION-Session variable
? $ _ ENV-environment variable
? $ _ COOKIE-HTTP Cookies
? $ Php_errormsg-previous error message
? $ HTTP_RAW_POST_DATA-native POST data
? $ Http_response_header-HTTP response header
? $ Argc-number of parameters passed to the script
? $ Argv-parameter array passed to the script

Global keyword
The global variables in PHP are a little different from those in C language. in C language, global variables automatically take effect in functions unless they are overwritten by local variables. This may cause some problems. some may accidentally change a global variable. Global variables in PHP must be declared as global or custom $ GLOBALS arrays in PHP. $ GLOBALS is an associated array. each variable is an element. The key name corresponds to the variable name and the value corresponds to the variable content. $ GLOBALS exists globally because $ GLOBALS is a super global variable.
Static variables
Another important feature of variable range is static variable ).
Variable
Sometimes it is convenient to use a variable name. That is to say, the variable name of a variable can be dynamically set and used. A common variable is set through declaration.
To use variables in an array, you must solve an ambiguous problem. When writing $ a [1], the parser needs to know that $ a [1] is used as a variable, you still want $ a as a variable and retrieve the value of [1] in the variable. The syntax for solving this problem is to use $ {$ a [1]} for the first case and $ {$ a} [1] for the second case.

 "; Echo invalide_value. ""; if (defined ("INVALIDE_VALUE") {echo "INVALIDE_VALUE has been defined ". _ LINE __. "";} // variable $ str1 = 'str1'; $ str2 = & $ str1; // Reference $ str1 = "Changed $ str1"; echo $ str1 .""; echo $ str2. ""; echo $ _ SERVER ['document _ root']. ""; // predefined variable $ gVal = 13; function Test () // global variable {global $ gVal; echo $ gVal. ""; echo $ GLOBALS ['gval ']. "" ;}test (); function test1 () {static $ a = 0; // static variable echo $ a; $ a ++;} // variable class foo {Var $ bar = 'I am bar. '; var $ arr = array (' I am. ',' I am B. ',' I am C. '); var $ r =' I am r. ';}$ foo = new foo (); $ bar = 'bar'; $ baz = array ('foo', 'bar', 'Baz', 'quux '); echo $ foo-> $ bar. ""; echo $ foo-> $ baz [1]. ""; $ start = 'B'; $ end = 'ar '; echo $ foo-> {$ start. $ end }. ""; $ arr = 'arr'; echo $ foo-> $ arr [1]. ""; echo $ foo-> {$ arr} [1]. "";?>








Http://www.bkjia.com/PHPjc/1022400.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/1022400.htmlTechArticlePHP constants and variables [constants] can be defined using the define () function, after PHP 5.3.0, you can use the const keyword to define constants outside the class definition. Once a constant is set...

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.