Data type Overall Division
- Scalar type: int, float, string, bool
- Composite type: Array, object
- Special type: null, resouce
Integer type int, integer3 integer notation
$n 1 = 123;
$n 2 = 0123;
- Hexadecimal notation: 0x123
$n 3 = 0x123;
Floating-point type float, double, real has two ways of writing:
$v 1 = 1.234; With a decimal point, which is the floating type
$v 2 = 1.234e3;//means: 1.234 times 10 of 3 times, or 1234, with E in the form of floating-point numbers
$v 3 = 1234e3;//The result value is 1234000, but it is also a floating-point number.
Floating-point numbers should not be compared directly to size
Because floating-point numbers are inside the system (CPU level), it is likely that they are not accurately expressed.
To compare, we can only consider the accuracy requirement in our own application and convert it to an integer for comparison.
It is common practice to multiply the number of digits by 10, for example, the precision requires 3 digits, multiplied by 103 times
- When the result of an integer operation exceeds the range of an integer, it is automatically converted to a floating-point number.
The range of integers, under 32-bit systems, about plus or minus more than 2 billion
string-type String
JS, although there are 2 kinds of string expression, but also should be understood as a string:
var str1 = ' Single quote string '
var str2 = "Double quote string";
But in PHP, single and double quoted strings are strings that have different meanings.
In PHP, there are 4 kinds of string forms:
Single Quote string:
Results:
Double quote string:
Results:
See the Manual for more escape characters as follows:
\
Nowdoc (single quote) delimiter string:
.0
The output is:
Heredoc (double quote) delimiter string:
Results:
Boolean type: bool, Boolean
Used to identify a data that has only two state values: true,false--eat not to eat, go not to, there is no ...
In the application, we often (need) directly use a data (possibly a variety of other types) as a Boolean value to judge.
What actually happens at this point is that the data is implicitly converted to a Boolean value.
The most common form of grammar is:
if (a data/variable) {
.................
}
Then, in PHP, the various other data, implicitly converted to a Boolean value, will be treated as false:
0
0.0
Null
“”
"0"
Array (); Empty array
Undefined variables//Of course try to avoid
other data is treated as true to look at it.
See Manual: PHP Manual appendix) type Comparison table) use php functions to compare variable $x
Arrays Type Array
A collection that identifies an "orderly arrangement" of data.
In PHP, an array of subscripts can use integers or strings.
The digital subscript often says "index number",
The string subscript often says "key name".
In fact, in the PHP error system, it is called "index", or offset
Arrays can also be stored in an array, which can form a "multidimensional array".
Array traversal has special syntax in PHP:
foreach (array name as subscript variable $k = = value variable $v1) {
Here is the loop body, you can use two variables $k, $v 1
}
Object Type objects
In PHP, the object and JS have a relatively large difference.
Usually, the object in JS has a custom definition (created), there is also "ready", such as window,location, Tag object.
But:
Objects in PHP usually refer to their own defined objects, which are purely grammatical.
Resource type Resource
- Meaning: Basically all refers to the external data reference. (data not generated by PHP code)
It is not the PHP language that "creates" data in some grammatical form, but rather the external data (such as databases, files, pictures), which PHP simply does with some sort of syntax (or way).
Empty type null
Just a "concept" type in the programming domain of a computer, just a concept created in order to express a particular case of the data stored by the variable--no data saved, no valid meaningful data saved
Type conversion Automatic conversion:
Usually automatic conversion is one of the most basic and convenient features of a weakly-class language: It converts data that is not handled by the operator into data that can be processed in various operations, depending on the needs of the operator. Common scenarios are as follows:
- if (data) {}: converted to bool type
- Arithmetic operators: converting to numeric types
- Join operators: Convert to String type
- Comparison operator: To Boolean type or numeric type
- If there is at least one boolean on both sides, the other side will turn to Boolean and compare
- Otherwise, the comparison is converted to a number
A particularly common conversion, whether auto-cast or cast, is to convert the string to a number:
"5" ==>> 5//integer
"5.5" ==>> 5.5 floating point
"5abc" ==>> 5 integers
"5.5ab" ==>> 5.5 floating point
"Abc5" ==>> 0 integers
"abc" ==>> 0 integers
"" ==>> 0
Forced conversions
is to use syntax to convert a data to another type of data, in the syntax format:
(target type) data;
Note: We cannot use this syntax to convert any type of data to any other type-because conversions between some types are meaningless-there is no rule within the system that defines the type conversion.
-The most common conversions typically occur between basic (scalar) data types.
Type-related system functions
- Var_dump (): The ability to output the full information of a variable.
- GetType (): Gets the type name of a variable, the result is a word (string), SetType (); Sets the type of a variable, syntax: SetType (variable name, target type)
- Isset (), Empty (), unset ();
- Isset () Determines whether a variable has data:
- Empty () to determine whether a data is empty: close to our daily concept (Nothing is empty)
- Unset (): Destroys (deletes) a variable.
- IS_XX type () series function: Determines whether a data is of a certain type, including:
- Is_int (), Is_float (), Is_numeric (), Is_bool (), Is_array (), Is_scalar ():
Is_numeric () to: 3, 3.5, "3", "3.14" is true
Is_scalar (): Determines whether it is a scalar type (that is, Int,float,stirng,bool)
Operator arithmetic operators
There are several: +-*/% + +--
- Note: Pay attention to the remainder operation%, first take the whole, then take the remainder
$v 1 = 7.5 3; The result is: 1
$v 2 = 7.5 3.5; The result is: 1
Compare JS in:--js, no rounding processing
var v1 = 7.5 3;//The result is: 1.5
var v2 = 7.5 3.5; The result is: 0.5
Self-increment decrement operator:
- General: The number is self-added 1 or self-minus 1.
- String: Only self-increment, and the effect of self-increment is "next character"
- Boolean Increment decrement invalid
- Null decrement is invalid, increment result is 1
The difference between pre-Gaga and post-gaga:
- Before + +: The increment operation of the variable is done first, then the value of the variable is taken to participate in other operations.
- Post + +: Store The value of the original variable temporarily, increment the value of the variable, and then participate in another operation with the temporarily stored value.
- Corollary 1: There is no difference between the self-added and the ex-add if the self-adding operation is performed in a standalone statement.
- Inference 2: If the former self-addition is placed in another statement, it will be different.
- Corollary 3: Pre-Gaga is slightly more efficient than post-Gaga (it is recommended to use pre-Gaga in the loop).
Comparison operators:
Includes:> >= < <= = = loosely equal! = = = = = Strictly equal!==
The difference between = = and = = =:
= = loosely equal, compared with two data "type after conversion" is it possible to be equal, and often considered "data content is the same"
= = = Strictly equal, congruent, only two data types and data content are exactly the same, only equal.
Critical Reference manual 〉〉 Appendix 〉〉 Type Comparison table.
Common different types of comparisons (data of the main indicator volume type)-Abnormal comparisons
- Normal comparison-the size comparison of numbers
- If there is a Boolean value, it is converted to a Boolean comparison: rule: true > False
- Otherwise, if there are numbers, they are converted to digital comparisons:
- Otherwise, if both sides are pure numeric strings, convert to a numeric comparison
- Otherwise, the comparison is by string
String comparison rules are: according to the order of the characters of a comparison, found which large, then the overall large, follow-up no longer compare
Logical operator:&& | | !
The premise: All operations are performed on values of Boolean type, and if not Boolean, they are converted to Booleans.
Logic with:
Rule (truth table):
True && true ==> true;
True && false ==>false
False && True ==>false;
False && false ==>false;
Only two of them are true, and the result is true.
As long as one is false, the result is false
Logical OR:
Rule (truth table):
true | | True ==> true;
true | | False ==>true
False | | True ==>true;
False | | False ==>false;
Only two of them are false and the result is false.
As long as one is true, the result is true
Logical Non:
!true ==>false
!false ===>true
The logic and short circuit of short-circuit phenomenon:
In the actual application, the data that participates in the logical operation is often not a direct Boolean value, but a Boolean result value after the calculation.
Roughly as follows:
if (Isfemale ($uName) && isage ($uName) >18) {
... echo "Ladies First"
}
At this point, if the Isfemale () function determines that the result is false, then the subsequent function isage () is no longer called, and naturally no longer more than 18 of the judgment, which is called "short-circuit phenomenon"
The logic or short circuit of a short-circuit phenomenon:
if (Isfemale ($uName) | | Isage ($uName) < 18) {
... echo "Limited care for women or minors"
}
At this point, if the Isfemale () function determines that the result is true, then the subsequent function isage () is no longer called, and naturally no longer is less than 18 of the judgment, which is "or operator short-circuit phenomenon"
String operators:
Only one:.
Derivative one:. =
The data on either side of the operator is converted to a string.
Contrast js:+ (with a double meaning, at which point a certain "judgment" is required)
Assignment operators:
Only one: =
Derived multiple: + = = *=/=%=. =
The basic form is:
The $ variable conforms to the assignment operator data 2;
These derived assignment operators are a simplified form of this operation:
$v 2 = $v 2 [operator] data 2;//The result of a variable after some operation with another data is stored in the variable
Contrast (it is not a simplification of this form):
$v 2 = data 2 [operator] $v 2;//This form should not be simplified
Condition (trinocular) Operator:
A generic operator requires 2 data to participate
There are several operators that require only one data participation: + +,--!
The
A conditional operator requires at least 3 data to participate!
The form is:
Data 1? Data 2: Data 3
Typically, data 1 should eventually be a Boolean value (if not, it will be used as a Boolean value).
Meaning:
If data 1 is true, the result of the operation is data 2, otherwise the result of the operation is data 3
Typical examples:
$score = 66;
$result 1= $score >= 60? "Pass": "Fail"; The result is "pass"
$result 2= $score? "Pass": "Fail"; The result is "pass", but the meaning is completely different, because even if the score is 33, it is also passed. Only a score of 0 is a failure.
The trinocular operator can be converted to an if Else statement to implement:
if ($score >= 60) {
$result 1 = "Pass";
}
else{
$result 1 = "Failed";
}
Flow control Flowchart Basic Symbols:
Start end:
Statement (block):
Judge:
Input and output:
To
Branching structure
If statement:
if (conditional judgment) {
Statement block
}
If Else statement:
if (conditional judgment) {
Branch 1
}
else{
Branch 2;
}
If else if statement
If-else If-else Statement:
Switch statement:
Switch (a data $v1) {//To determine if this V1 variable is equal to one of the following, and if it is equal, enter the corresponding process.
Case Status value 1:
Process 1
[Break;]
Case Status Value 2:
Process 2
[Break;]
Case Status Value 3:
Process 3;
[Break;]
。。。。。。
[Default:
The default process.
]
}
The use of punch, break is usually used, only some special data or requirements, may not be.
If break is not used, the process code in subsequent States will continue to be executed once a state is satisfied, and no longer judged.
Loop structure: While loop:
$v 1 = 10; Initializing a loop variable
while ($v 1〉4) {//Determine the condition of the loop variable
Statement Fast
echo "ABC";
$v;
}
Loop 3 Features:
1, loop variable initialization
2, cyclic variable judgment
3, cyclic variable change
This 3 feature usually applies to all loop procedures.
Do and loop
do{
Loop body
}while (conditional judgment);
Meaning:
The loop body is executed first, then the condition is determined, and if the condition is met, the loop body is resumed and then judged, and so on.
For loop:
Interruption of the Loop
This refers to the interruption that applies to all loops.
There are two types of interrupts for loops:
Break Interrupt: Terminates the entire loop statement, and the statement that jumps out of the loop into the loop structure
Continue interrupt: Terminates the statement in the loop body that is currently executing, and enters into the next process of the loop (change, judge)
The syntax for the interrupt statement is as follows:
Break $n; $n is an integer greater than or equal to 1, indicating the number of loop layers to break;
Continew $n;
The so-called circular layer, refers to a loop and nested in the case of the loop.
Take the current cycle as the "starting point", the first level, upward (outside) number, is the 2,3,4 layer ....
Substitution syntax for partial process Control:
if (...):
Statement block
endif
if (...):
Statement block
Else
Statement block
endif
if (...):
Statement block
ElseIf (...):
Statement block
ElseIf (...):
Statement block
Else
Statement block
endif
Switch (...):
Case ...
Case ...
Endswitch;
while (...):
Statement block
Endwhile;
for (...;;..):
Statement block
ENDfor;
foreach ():
Statement block
Endforeach;
Controlling Script Execution Progress
Die ("Output content")
Meaning: Terminates the operation of the PHP script (subsequent code is no longer executed) and outputs its contents
can also: Die (); Die
Exit is synonymous with die.
Die is a "language structure", not a function, you can not write parentheses.
Echo is also a language construct, not a function:
Echo ("abc");
echo "ABC";
echo "abc", "Def", 123;
Sleep ($n);
Meaning: Let the php script stop for $n seconds and then continue execution.
Array Base Array base
In PHP, the subscript of an array can be an integer, or a string.
In PHP, the order of elements in an array is not determined by the subscript, but by the order in which they "join".
Defined:
$arr 1 = Array (element 1, Element 2, ...). );
Array (1, 5, 1.1, "ABC", True, false); You can store any data at this time as "default subscript",
Array (2=>1, 5=>5, 3=>1.1, 7=> "abc", 0=>true);//subscript can be set arbitrarily (no order, no continuous)
Array (2=>1, 5, 1=>1.1, "abc", 0=>TRUE)//Can be added subscript, also can not add (default subscript), the subscript is: 2,3,1,4,0
Default subscript rule: The maximum number subscript that has been used in the previous +1
Array (2=>1, ' DD ' =>5, 1=>1.1, "abc", 0=>TRUE)//mixed subscript, also follows the default subscript rule
Array ( -2=>1, ' DD ' =>5, 1.1, "ABC", True); A negative subscript is not counted in an integer subscript, but only as a character subscript
Then the best 3 entries are: 0, 1, 2
Array (2.7=>1, ' DD ' =>5, 1=>1.1, "abc", 0=>true);//The floating-point index is automatically converted to an integer and the decimal is erased directly
Array ("2.7" =>1, ' DD ' =>5, "one" =>1.1, "abc", True)//a purely numeric string subscript, treated as a number,
The subscript at this time is: 2, ' DD ', 11, 12, 13
Array (2=>1, ' DD ' =>5, true=>1.1, "abc", False=>true)//Boolean current subscript, True 1,false is 0;
Array (2=>1, ' DD ' =>5, 2=>1.1, "abc", TRUE)//If the subscript follows the previous duplicate, it simply overwrites the value of the previous name
This is equivalent to: Array (2=>1.1, ' DD ' =>5, "abc", True)
Other forms;
$arr 1[] = 1;
$arr 1[] = 5;
$arr 1[] = 1.1; Use [] directly behind the variable to become an array and assign values in turn.
。。。。
$arr 2[' AA '] = 1;
$arr 2[' bbbcc '] = 5;
$ARRR 2[5] = 1.1;
。。。。。。。。
This form of subscript, in fact, is almost the same as using the array syntax structure.
Value: Pass the subscript.
Assignment (same definition):
Classification of arrays from key-value relationships:
Associative array: usually refers to an array that is labeled as a string, and that string can roughly express the meaning of the data.
Example: $person =array (
"Name" = "Floret",
"Age" =>18,
"Edu" = "University Graduate",
);
Indexed arrays:
Usually refers to the subscript of an array is a strict 0-based continuous digital subscript-As with the JS array.
To divide from the array hierarchy:
One-dimensional arrays:
Is the value of each element in an array, which is a normal value (not an array value)
$arr 1 = Array (
"Name" = "Floret",
"Age" =>18,
"Edu" = "University Graduate"
);
Two-dimensional arrays:
Each item in an array is another one-dimensional array.
$arr 1 = Array (
"Name" = = Array (' Floret ', ' small Fang ', ' Xiao Ming ',);
"Age" = = Array (18, 22, 19),
"edu" = = Array ("University graduate", ' Secondary school ', ' Primary school ')
);
Multidimensional Arrays:
etc...
General syntax form for multidimensional arrays:
$v 1 = array name [subscript] [subscript] [...]
Array traversal traversal basic syntax
A foreach ($arr as [$key = =] $value)//$key can be called a key variable, $value can be called a value variable.
{
Here you can do all the possible things for $key and $value-because they are a variable
$key represents every time an index of an element is obtained, possibly a number, or it can be a string
$value represents the value of the element each time it is acquired, possibly various types.
This loop structure iterates through the first item of the array, loops to the last item, and then ends.
}
Exchange principle:
Discussion on the details of foreach traversal
- foreach is also a normal looping syntax structure, and can have operations such as break and continue.
- The default value-passing value of a variable during traversal is a value pass.
- During traversal, value variables can be manually set as reference passes:
- foreach ($arr as $key = & $value) {...}
- Key variable cannot be set as a reference pass
- foreach defaults to traversal on the original array. However, if an array is modified during traversal or some kind of pointer operation (that is, the preceding pointer function), the arrays are copied and the loops continue to be traversed on the copied array.
If a value variable is a reference pass in foreach, it is on the original array anyway
PHP syntax-data type, operator, Process Control