05-02 php sorting variable constant data type

variable

Define variables:

Defining a variable (assignment): directly assigning a value to a variable is a definition;

The "define but not assign" syntax in js is not supported .

In other words, the first time a variable is assigned a value, the variable is defined.

Value: In all statements that require a variable value, the value of the variable will be obtained

 

Check if a variable exists isset()

 

Returns true if it exists, false if it does not exist

 

In fact, if the value of a variable is null , it will also return false

 

Modify variable value (assignment):

 

Assign a value to a variable for the second time or later, even if the variable value is modified

 

Delete unset(): It is to disconnect the "reference relationship" between the variable name and the data

Common variable names:

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 use 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)!

 

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:

 

Code example:

The diagram is as follows:

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

 

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

 

Predefined variables are also called superglobal 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!

 

$_GET variable (array):

The word get , often appears here: <form action= ”abc.php” method=”get” >.....</form>

This is called "form submits data in get "

Then the predefined array variable $_GET refers to the collection (array) of all data submitted in this way

Correspondingly, data can also be submitted in post mode

Note: There seems to be no Chinese translation for the word get .

 

$_POST (mostly used for form data submission):

 

 The usage is the same as $_GET. It should be noted that the name value of the multi-select box should be named in the form of an array.

constant

Meaning: An identifier used to store a data that does not change and does not want to change.

The naming rules for constants are the same as for variables, but it is customary to use the "all uppercase" form for the names of constants .

 

Definition form

Use define() function definition

Use the form: define(" constant name ", constant value) ;

It is recommended to use all capitals for common names

Define using const syntax

 

Usage form: const   constant name = constant value ;

 

But the const syntax can only be used in the "topmost" code domain (not in curly braces)

 

That is, the const syntax cannot be used in functions or in conditional statements or other statements that denote a "scope"

Differences between constant variables:

The definition form is different:

Different usage form: constants don't need $ sign

The degree of variability is different: the value of the constant cannot be changed, and the constant cannot be destroyed

Different scopes: constants have super-global scope (both inside and outside the function can be used directly)

Available types are different: constants can only store scalar types : int, float, string, bool

Determine whether the constant exists: defined()

The result of the judgment is a Boolean value.

Common form: if ( defined ( a constant name) == false ){ ..... here you can define the constant; }

In fact, this can be done like this:

$result = defined ( a constant name) ; // result is true or false

 

Predefined constants:

 

In the php language, a large number of constants are predetermined and can be used directly.

 

Reference Manual > Appendix > Reserved Word List > Predefined Constants

 

magic constant

 

A magic constant is actually a constant whose value changes with certain conditions (environment). It is constant in form and usage, but in fact, its value changes.

Reference Manual > Language Reference > Constants > Magic Constants

The result is:

 

 

 

 

type of data

 

Scalar type

 

Integer type int , integer

 

floating point types float , double , real

 

string type string

Boolean type: bool , boolean

 

array type array

 

object type object

resource type resource

empty type null

 

Guess you like

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