PHP constants, variable usage detailed introduction _php tutorial

Source: Internet
Author: User
Tags http cookie http post
In the past rarely so detailed to introduce the variables in PHP, constants and the use of magic constants and reference tables, this article for beginners have a need to understand the help of friends can refer to.

Variable:

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

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

All variables in PHP start with the $ symbol.

The correct way to set variables in PHP is:

The code is as follows Copy Code
$var _name = value;

PHP beginners tend to forget the $ symbol in front of the variable. If you do that, the variable will be invalid.

Let's try to create a variable that holds a string, and a variable that has a value:

The code is as follows Copy Code
$txt = "Hello world!";
$number = 16;
?>

1. How do I define a variable, and how does it differ from languages like C #?
Variables in PHP are represented by a dollar sign followed by a variable name. Variable names are case-sensitive. For example:

The code is as follows Copy Code
$var = ' Jim ';
$VAR = ' Kimi;
echo "$var, $VAR";//Output "Jim,kimi"

?> You may also be concerned about the naming of variables, in fact, as in most languages.
2. Is the variable case-sensitive?
As in 1, it is case-sensitive.
Note that the concept of reference assignment has been introduced since PHP4, but it is similar to most language references, but I think the most similar is C/s + +. Because it also uses the "&" symbol. For example:

The code is as follows Copy Code
1 2 $foo = ' bob ';//Assign the value ' Bob ' to Foo
3 $bar = & $foo; Referenced by $bar. Note & Symbols
4 $bar = "My name is $bar"; Modify $bar
5 echo $bar;
6 echo $foo; $foo has also been modified.
7?>

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


Okay, now that you have a general idea of the variable, let's look at the indirect reference to the variable and the string connection.

Indirect references to ① variables: Let's take a look at an example.

The code is as follows Copy Code
$a = "B";
$ $a = "123";
Echo $b;
?>

The above output is 123.

We can see a $ in the second line of code, and access the variable by the specified name, the specified name is stored in $ A ("B"), and the value of the variable $b is changed to 123. Thus, such a $b variable is created and assigned a value.

By adding an additional $ tag to the front of the variable, you can arbitrarily increase the number of references.

② string Connection: First look at an example.

The code is as follows Copy Code
$a = "PHP 4";
$b = "powerful";
echo $a. $b;
?>

It is important to note that the default value for PHP directive register_globals is off in PHP 4.2.0 and subsequent versions. This is a major change in PHP. Letting the register_globals value off will affect the validity of the set of predefined variables at global scope. For example, in order to get the value of Document_root, you would have to use $_server[' document_root '] instead of the $DOCUMENT _root, as in the case of the $_get[' ID ') instead of the $id from the URL http://www.exa Get the ID value in mple.com/test.php?id=3, or use $_env[' HOME ' instead of $HOME get the value of the environment variable HOME

We see the third line of code, the English (sentence) number, which can concatenate strings and become merged new strings.

Super Global variables Describe
$GLOBALS Contains a reference to a globally valid variable that refers to each current script. The key name of the array is the name of the global variable. An array exists starting with PHP 3 $GLOBALS .
$_server Variables are set by the Web server or directly associated with the execution environment of the current script. Similar to an array of old arrays $HTTP_SERVER_VARS (still valid but opposed to use).
$_get A variable that is submitted to the script via a URL request. Similar to an array of old arrays $HTTP_GET_VARS (still valid but opposed to use).
$_post A variable that is submitted to the script via the HTTP POST method. Similar to an array of old arrays $HTTP_POST_VARS (still valid but opposed to use).
$_cookie A variable that is submitted to the script via the HTTP cookie method. Similar to an array of old arrays $HTTP_COOKIE_VARS (still valid but opposed to use).
$_files A variable that is submitted to the script via an HTTP POST file upload. Similar to an array of old arrays $HTTP_POST_FILES (still valid but opposed to use)
$_env Executes the variables that the environment submits to the script. Similar to an array of old arrays $HTTP_ENV_VARS (still valid but opposed to use).
$_request The variable that is submitted to the script via the get,post and COOKIE mechanism, so the array is not trustworthy. The presence or absence of all variables contained in the array and the order of the variables are defined by the Variables_order configuration instructions in php.ini . This array does not have a direct corresponding version before PHP 4.1.0. See Import_request_variables ().
$_session The variable currently registered to the script session. Similar to an array of old arrays $HTTP_SESSION_VARS (still valid but opposed to use)

Constant:

A constant is an identifier (first name) of a simple value. As the name implies, the value cannot be changed during script execution (except for the so-called magic constants, they are not constants). Changshime is considered to be case sensitive. Usually, constant identifiers are always uppercase.

The constant name and any other PHP tags follow the same naming conventions. A valid constant name begins with a letter or underscore followed by any letter, number, or underscore. This is expressed in regular expressions: [a-za-z_x7f-xff][a-za-z0-9_x7f-xff]*

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

The ② constant is named similar to the variable, except for the dollar sign "$". A valid constant name begins with a letter or underscore, followed by any number of letters, numbers, or underscores in the newspaper.

③ generally in PHP, constants are uppercase and are divided into system constants and custom constants.

System constants We'll probably say, this knowledge in the back will be introduced.

1, __file__ Default constants, refers to the PHP program file name and path;
2, __line__ Default constants, refers to the number of PHP program lines;
3, the name of the __class__ class;

Custom constants: Define a constant by using the Define () function,

The syntax format is: BOOL Define (String $name, mixed $value [, BOOL case_$insensitive])

Name: Specifies the names of the constants.
Value: Specifies the values of the constants.
Insensitive: Specifies whether the name of the constant is case sensitive. True is not case-sensitive, and is case-sensitive if set to false. If this parameter is not set, the default value of False is taken.

A valid constant name
Define ("FOO", "something");
Define ("FOO2", "something Else");
Define ("Foo_bar", "something More");

Illegal constant name
Define ("2FOO", "something");

The following definitions are legal, but should be avoided: (Custom constants do not start with __)
Maybe someday php will define a __foo__ magic constant.
This will conflict with your code.
Define ("__foo__", "something");

?>

several "Magic constants" for PHP
name Description
__line__ The current line number in the file.
__file__ The full path and file name of the file. If used in the included file, returns the file name that is included. From PHP 4.0.2,__file__ always contains an absolute path (if it is a symbolic connection, the resolved absolute path), and the previous version sometimes contains a relative path.
__dir__ The directory where the file resides. If used in the included file, returns the directory where the included files are located. It is equivalent to dirname (__file__). Unless it is a root directory, the name in the directory does not include the trailing slash. (New in PHP 5.3.0) =
__function__ The name of the function (PHP 4.3.0 new addition). From PHP 5 This constant returns the name (case-sensitive) when the function is defined. In PHP 4, this value is always in lowercase letters.
__class__ The name of the class (PHP 4.3.0 new addition). From PHP 5 This constant returns the name of the class when it is defined (case-sensitive). In PHP 4, this value is always in lowercase letters.
__method__ The method name of the 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 new)

http://www.bkjia.com/PHPjc/629213.html www.bkjia.com true http://www.bkjia.com/PHPjc/629213.html techarticle in the past rarely so detailed to introduce the variables in PHP, constants and the use of magic constants and reference tables, this article for beginners have no small help to have to know friends can ...

  • 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.