Php constants and variable usage

Source: Internet
Author: User
The complete path and FILE name of The _ FILE. If it is used in a file to be included, the file name to be included is returned. From PHP4.0.2, __file _ always contains an absolute path (if it is a symbolic connection, it is an absolute path after resolution), and a previous version sometimes contains a relative path.

Variable:

Variables are used to store values, such as numbers, text strings, or arrays.

Once a variable is set, we can reuse it in the script.

All variables in PHP start with the $ symbol.

The correct method for setting variables in PHP is:

$ Var_name = value;

PHP beginners often forget the $ symbol before the variable. In this case, the variable will be invalid.

Let's try to create a variable with a string and a variable with a numeric value:

<? Php

$ Txt = "Hello World! ";

$ Number = 16;

?>

1. how to define a variable? what is the difference between it and C?

Variables in PHP are represented by a dollar sign followed by the variable name. Variable names are case sensitive. For example:

<? Php

$ Var = 'itluren ';

$ VAR = 'php;

Echo "$ var, $ VAR"; // output "itluren, php"

?>

You may also care about variable naming, which is actually the same as most languages.

2. are variables case sensitive?

As stated in 1, it is case sensitive.

Note: Since PHP4, the concept of reference assignment has been introduced, which is similar to that of most languages. However, I think C/C ++ is the most similar. because it also uses the "&" symbol. For example:

<? Php

$ Foo = 'itluren'; // assign 'itluren' to foo.

$ Bar = & $ foo; // reference through $ bar. Note & symbol

$ Bar = "My name is $ bar"; // modify $ bar

Echo $ bar;

Echo $ foo; // $ foo is also modified.

?>

Like other languages, variables with variable names can only be referenced.

Now you should have a rough understanding of the variable. now let's look at the indirect reference of the variable and the string connection.

① Indirect reference of variables: Let's look at an example first.

<?

$ A = "B ";

$ A = "123 ";

Echo $ B;

?>

The output above is 123

We can see that there is another $ in the second line of code, and the variable is accessed through the specified name, the specified name is stored in $ a ("B, change the value of $ B to 123. Therefore, such $ B variables are created and assigned values.

By adding the $ tag before the variable, you can increase the number of references at will.

② String connection: Let's look at an example first.

<?

$ A = "PHP 4 ";

$ B = "powerful ";

Echo $ a. $ B;

?>


Note that 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? Id = 3, or use $ _ ENV ['home'] instead of $ HOME to get the value of the environment variable HOME.

We can see the third line of the code, the English (sentence) number, which can be used to connect strings and convert them into merged new strings.

Hyperglobal Variable Description

$ GLOBALS contains a reference to a variable that is valid globally for each current script. The key name of the array is the name of the global variable. $ GLOBALS array exists from PHP 3.

$ _ SERVER variables are set by the web SERVER or directly associated with the execution environment of the current script. Similar to the old $ HTTP_SERVER_VARS array (still valid, but not used ).

$ _ GET variables submitted to the script through URL requests. Similar to the old $ HTTP_GET_VARS array (still valid, but not used ).

$ _ POST variables submitted to the script through the http post method. Similar to the old $ HTTP_POST_VARS array (still valid, but not used ).

$ _ COOKIE variables submitted to the script through HTTP Cookies. Similar to the old $ HTTP_COOKIE_VARS array (still valid, but not used ).

$ _ FILES variables submitted to the script by uploading the http post file. Similar to the old array $ HTTP_POST_FILES array (still valid, but not used)

$ _ ENV: variables submitted to the script in the execution environment. Similar to the old $ HTTP_ENV_VARS array (still valid, but not used ).

$ _ REQUEST is submitted to the script variables through GET, POST, and COOKIE mechanisms, so this array is not trustworthy. All the existence or not of the variables contained in the array and the Order of the variables are defined according to the variables_order configuration instructions in php. ini. This array does not have a corresponding version before PHP 4.1.0. See import_request_variables ().

$ _ SESSION: the variable currently registered to the script SESSION. Similar to the old array $ HTTP_SESSION_VARS array (still valid, but not used)

Constant:

A constant is the identifier (name) of a simple value ). As its name implies, this value cannot be changed during script execution (except for so-called magic constants, they are actually not constants ). Constants are case sensitive by default. Constant identifiers are always capitalized.

Constant names and any other PHP labels follow the same naming rules. A valid constant name starts with a letter or underline followed by any letter, number, or underline. The regular expression is like this: [a-zA-Z_x7f-xff] [a-zA-Z0-9_x7f-xff] *

① It is the data that cannot be changed during program execution, and the scope of the constant is global.

② The constant name is similar to the variable, except that it does not contain the dollar symbol "$ ". A valid constant name must start with a letter or underline followed by any number of letters, numbers, or underscores.

③ In PHP, constants are uppercase letters and are classified into system constants and custom constants.

The system constants will be introduced later.

1. the default constant _ FILE _ refers to the FILE name and path of the PHP program;

2. the default constant _ LINE _ refers to the number of lines in the PHP program;

3. _ CLASS name;

Custom constant: defines a constant through the define () function,

Syntax format: bool define (string $ name, mixed $ value [, bool case _ $ insensitive])

Name: specifies the constant name.

Value: specifies the value of a constant.

Insensitive: specifies whether the constant name is case sensitive. If it is set to true, it is case insensitive. if it is set to false, it is case sensitive. If this parameter is not set, the default value is false.

<? Php

// Valid constant name

Define ("FOO", "something ");

Define ("FOO2", "something else ");

Define ("FOO_BAR", "something more ");

// The constant name is invalid.

Define ("2FOO", "something ");

// The following definition is valid, but it should be avoided: (do not start with _ as a custom constant)

// Maybe one day, PHP will define a magic constant of _ FOO _.

// This will conflict with your code

Define ("_ FOO _", "something ");

?>

Several PHP "magic constants"

Description

The current row number in the _ LINE _ file.

The complete path and FILE name of The _ FILE. If it is used in a file to be included, the file name to be included is returned. Starting from PHP 4.0.2, __file _ always contains an absolute path (if it is a symbolic connection, it is an absolute path after resolution ), earlier versions sometimes contain a relative path.

The directory where the _ DIR _ file is located. If it is used in included files, the Directory of the included files is returned. It is equivalent to dirname (_ 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 name (new in 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 name (new in 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.

_ 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)

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.