PHP 7:php definitions of variables and constants

Source: Internet
Author: User
Tags http cookie http post php define variable scope
This chapter is about the definition of variables.
If you have a definition of variables and constants, how many things do you notice? You might think:
How do you define a variable, and how does it differ from languages like C #? Is the variable case-sensitive? Are there any other important variables in PHP?
Do constants and variables have the same definition? Tell it separately.
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:

$var = ' Jim ';
$VAR = ' Kimi;
echo "$var, $VAR";//Output "Jim,kimi"
?>

You may also be concerned about the naming of variables, which are in fact the same as 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:

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.
3. Other important points of PHP
Pre-defined variables
Pre-defined variables are an important concept in PHP. PHP provides a large number of predefined variables. Because many of these variables depend on the version and settings of the server that is running, and other factors, no detailed documentation is provided. Some of the predefined variables do not take effect when PHP is run as a command line.

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.

Starting with PHP 4.1.0, PHP provides an additional set of predefined arrays that contain data from the Web server (if available), the runtime environment, and user input. These arrays are very special and they take effect automatically in the global scope, for example, automatically in any scope. This is often referred to as an automatic global variable (autoglobals) or a Hyper global variable (superglobals). (There is no mechanism for users to customize hyper-global variables in PHP.) The hyper-global variables are listed below, and you will notice that the old predefined arrays ($HTTP _*_vars) still exist. From PHP 5.0.0, long-form PHP predefined variables can be masked by setting register_long_arrays.
The following table is a hyper-global variable for PHP:
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. $GLOBALS array exists starting with 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 array $HTTP _server_vars array (still valid but opposed to use).
$_get A variable that is submitted to the script via a URL request. Similar to the old array $HTTP _get_vars array (still valid but opposed to use).
$_post A variable that is submitted to the script via the HTTP POST method. Similar to the old array $HTTP _post_vars array (still valid but opposed to use).
$_cookie A variable that is submitted to the script via the HTTP cookie method. Similar to the old array $HTTP _cookie_vars array (still valid but opposed to use).
$_files A variable that is submitted to the script via an HTTP POST file upload. Similar to the old array $HTTP _post_files array (still valid but opposed to use)
$_env Executes the variables that the environment submits to the script. Similar to the old array $HTTP _env_vars array (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 the old array $HTTP _session_vars array (still valid but opposed to use)
Scope of application of variables
Each variable has a range of applications, so how does PHP define it? Let's take a look at the following code:

1 2 $var = 0;
3 function test ($INDEX)
4 {
5 $var = $var + 1;
6 echo "the". $index. "Number is". $var. "
" ;
7}
8 test (1);
9 Test (2)
Ten?>

What do you think the above code will show?
If you think it's the following:
Result 1:

The 1 number is 1
The 2 number is 2

Sorry, your result is wrong.
In fact, the correct result should be:
Results 2

The 1 number is 1
The 2 number is 1

So what did you find out about it? We can know that although the code in line 2nd is defined outside, the variable of line 5th is not the same as it. The variables in line 5th are only used in this function. Further, if I want to invoke the first row of the variable and display the result 2. The code can be as follows:

1 2 $var = 0;
3 function test ($INDEX)
4 {
5 Global $var;
6 $var = $var + 1;
7 echo "the". $index. "Number is". $var. "
" ;
8}
9 test (1);
Ten Test (2)
?>

What is the difference between this code snippet and the above code snippet? Notice the 5th line, with one more global keyword. You got it.
So is there any other way? The answer is yes.
The code is as follows:

1 2 $var = 0;
3 function test ($INDEX)
4 {
5
6 $GLOBALS ["var"] = $GLOBALS ["var"] + 1;
7 echo "the". $index. "Number is". $GLOBALS ["var"]. "
" ;
8}
9 test (1);
Ten Test (2)
?>

Is there anything special about the code? That's the use of $GLOBALS this super-global variable.
PHP also has static variables to say. However, static variables are generally used in functions and can only be local variables. Let's look at the following code:

1 2 function Test ()
3 {
4 static $a = 0;
5 echo $a. "
" ;
6 $a + +;
7}
8 Test ();
9 Test ();
Ten?>

Result is

1
2


PHP also has a pretty exciting feature: mutable variables
A variable variable name can be dynamically set and used.
Take a look at the following example:

1 2 $a = "Hello";
3 $hello = "World";
4 echo $a. " " . $ $a;
5?>

The result of the output is actually hello,world. It's amazing. $ $a is actually $hello, because the value of $ A is hello.
That's the number of variables. Here's a look at constants.

Constant
Are the constants in PHP preceded by const? Let's take a look.
No. The PHP must be defined in the following way.
BOOL Define (string name, mixed value [, BOOL case_insensitive])
Name is a constant name and value is a constant. Case_insensitive] is case sensitive. The default is sensitive. For example:

1 2 define ("CONSTANT", "Hello World");
3 echo CONSTANT; Outputs "Hello world."
4 echo Constant; Outputs "Constant" and issues a notice.
5
6 Define ("greeting", "Hello you.", true);
7 echo greeting; Outputs "Hello you."
8 echo greeting; Outputs "Hello you."
9
Ten?>


Constants and variables are different:

The constant is preceded by a 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;

Once a constant is defined, it cannot be redefined or undefined;

The value of a constant can only be scalar.

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