php variable

naming convention

Common names include variable names, function names, constant names, class names, interface names, and so on. . . .

The naming rules introduced here are relatively general, and there may be some details in some places, and usually try not to involve uncommon situations.

basic rules:

Only use upper and lower case letters, underscore "_" , numbers

Number cannot start

Cannot have the same name as the keyword in the environment (system) (such as if , for , function...

 

Industry rules (hidden rules):

 

The basic rules guarantee the correctness of grammar.

 

The rules of the industry guarantee whether they can be respected by others!

 

In the industry rules, there are the following common naming rules:

 

1 , CamelCase:

 

示例:name,  myNamemyFatherName

 

2 , Pascal nomenclature:

 

示例:Name,  MyNameMyFatherName

 

3 , underscore interval method:

 

示例:name,  my_amemy_father_name

 

Passing values ​​between variables

 

in summary:

 

1. The value-passing method discussed here refers to: one variable to another variable

 

2 , it applies not only to assignment statements, but also to other statements with the same meaning, such as: function arguments to formal parameters

 

3. There are only two ways to pass by value : pass by value and pass by reference

 

4. In php , all variables are passed by value by default.

 

5. To pass by reference, you must use the symbol passed by reference:

 

pass by value

 

It refers to copying the data value (data content) of a variable and assigning it to another variable.

 

code example

 

 

 

The diagram is as follows:

 

After copying, these two variables are not related (independent of each other)!

The result is: v1=1, v2=2

 

 

By reference by value:

It refers to copying the reference relationship of a variable, and then assigning it to another variable, that is, another variable is also determined by the relationship to point to a certain data:

After assignment (passing by value), the two variables point to the same data.

We all know that operations on variables are operations on data.

Then: the operation of the variable v1 , the changed data, that is, the v2 has also changed.

 

variable variable

 

The so-called variable variable is the name of a variable, which is also a variable.

 

The syntax for variadic variables is very specific to php - rarely seen in other languages.

 

$v1 = "abc"; // this is a string variable whose content is the string " abc "

 

$abc = 10; // This is a normal variable whose content is the number 10

 

echo $$v1; // At this time, it is the so-called "variable variable"

 

How to understand:

 

1 , whenever the $ sign appears, it may be understood as a variable

 

2 , where the first " $ " appears, then php will interpret it as a variable, and the variable name is $v1;

 

3 , we know that the value of $v1 is "abc",

 

4 , then, the variable name after the first " $ " symbol is obtained as " abc "

 

5 , that is: what echo tries to output is the variable $abc , which is naturally the number 10

 

In fact, in theory, mutable variables can be nested more, such as: $$$def;

 

 

 

predefined variables

Overview:

1 : Predefined variables are also called super global variables, including:

$_GET, $_POST, $_SERVER, $_REQUEST, $GLOBALS, $_COOKIE, $_SESSION, .......... (about 10 )

2 : Scope issues

In php , the scope of custom variables is divided into two types: global scope (outside the function) and local scope (inside the function).

but:

The scope of predefined variables is called "superglobal scope": the sum of global scope + local scope (both inside and outside the function are available)

3 : Data type problem:

Superglobal variables are arrays!

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325260619&siteId=291194637