: This article mainly introduces php basic learning notes (12). If you are interested in the PHP Tutorial, refer to it. Php Basic syntax
Variables and constants
In php, the variable starts with a $ symbol. A variable is usually directly assigned a value (if it is the first assignment, it is defined ).
Constant: Set a Data Identifier (data name) whose value should not be changed, such as the mathematical "pi ".
$ Pi = 3.14; // This is a variable
Define ("PI", 3.14); // This is the definition of a constant named "PI" with a value of 3.14, which is similar to a value assignment here
Constant definition is also a value assignment, and only this one-time!
Differences between constants and variables:
- No dollar sign before a constant ($)
- Constants can only be defined by the define () function, but cannot be assigned by the value assignment statement.
- Constants can be defined and accessed anywhere, regardless of the variable range rules.
- Once defined, a constant cannot be redefined or undefined.
- Constant values can only be scalar (I .e. integer, floating point, string, Boolean)
Data type:
Basic type (scalar type ):
Boolean (bool, boolean): only two data types: true false
Character type (string): it can be single quotation marks, double quotation marks, or "delimiters" format.
Integer type (int, integer ):
$ V1 = 10;
$ V2 = 012; // a number starting with 0 represents an "octal" integer, which is equivalent to 10 in decimal format.
$ V3 = 0x1F; // A hexadecimal integer starting with 0x, which is equivalent to 31 in decimal format.
Floating point type (float, double)
$ V4 = 12.34;
$ V5 = 12.34e5; // 12.34x10 to the power of 5
Composite type:
Array type:
Object Type:
Special type:
Resource Type: indicates an external data type.
Null: only one data type is null"
Differences between the three string types:
$ Str1 = "abc \ ndefg1"; // The following escape characters can be recognized in double quotation marks: \ "\ n \ r \ t \ $ str2 = 'a \ bc \ ndefg2 \ '; // only the following escape characters can be recognized in a single quotation mark string: \'\\. "\" Is usually used only for the last character. // The following is the separator string, where "ABCD" is a random name. This symbol is used to end a string. // The following escape characters can be recognized in the delimiter string: \ n \ r \ t \\\$ str3 = <
Script alert ("dddd"); script ABCD; // Note: only the name and a semicolon can appear at the end of the preceding line of the separator string, there cannot be any other content, such as spaces and indentation (tab character) // Note: in double quotation marks and delimiters, you can directly identify a variable and fill it with its value. // If you do not want to be recognized, use "\ $" to escape the $ symbol $ I = 5; echo "$ I =". $ I;
Value transfer method:
Value Transfer (assign value): Copy the value of a variable and assign the value to another variable. at this time, the values of the two variables are equal (the same ), however, the two variables are completely independent and unrelated.
By default, in php, simple data types, arrays, and null types are passed with values.
Reference transfer (reference transfer value): copy the "address" represented by multiple variables and assign the value to another variable, which is equivalent to pointing the two variables at the same time (corresponding) an identical address-this address represents the data location. At this time, when the values of the two variables are equal, they also indicate that the two variables actually refer to a common data.
By default, in php, object types and resource types are passed by reference.
However:
We can also manually use the data passed by the default value to use the reference to pass the value. Syntax:
Variable 1 = & Variable 2;
Example:
$ V1 = 10;
$ V2 = & $ v1; // $ v1 and $ v2 represent "one data content"
$ V1 ++;
Echo $ v2;
Comparison js:
In js, only the default value passing method is available, and no manual setting is available.
In js: The default values are passed in the following types: numbers, characters, Boolean, and two special types.
Default reference value types include: Array, object
Operator:
L arithmetic operator: +-*/% + + --
- + Is used only for "arithmetic operations" and has no double meanings.
- ++ -- The meaning and usage of the symbol are exactly the same as those of js.
L written before the variable, then: first auto-increment (auto-subtract), then other operations (such as value assignment)
L after the variable is written, perform other operations (such as assigning values) and then auto-increment (auto-subtraction)
- Only the integer is obtained and calculated. if it is not an integer, it is automatically converted to an integer and then the remainder is obtained.
L comparison operator: =! ==>=<<=== (All equals )! = (Not all equal)
- = Usually indicates "fuzzy equality", = indicates exact equality (only when the data type and value are the same)
- For more information about fuzzy equivalence, see Appendix> type comparison table.
L conditional operator: (expr1 )? (Expr2): (expr3), for example:
- Meaning: evaluate the expression exp1. if the result is true, the result of the entire expression is exp2. Otherwise, the result of the entire expression is exp3.
- $ V1 = $ fs> 60? "Pass": "retake"; // v1 has two conditions based on the fs value.
- Additional knowledge: unary operators include :!, ++ ,--,~,, Binary operators are the most commonly used operators.
L logical operators: & (and) | (or )! (Not)
- Logic and &: The result is true only when both data are true.
- Logic or |: The result is true if one data is true.
- Non-logical! : Obtain the opposite value of the logical data.
L string operator:. (that is, English periods). = (that is, similar to "+ = ")
- . = Is just a "shorthand", $ v1 = $ v1. "abc"; è $ v1. = "abc ";
L-bit operator: & (and) | (or )~ (Not) ^ (exclusive or) <(left shift)> (right shift)
- Bitwise operators are used for binary numbers.
- Bitwise AND &: The result is 1 only when the numbers on the two corresponding bits are 1.
- Bitwise OR |: as long as the number on the corresponding digit is 1, the result is 1.
- By-bit non ~ : Reverse, that is, 1 is changed to 0, 0 is changed to 1.
- Bitwise OR ^: when the numbers on two corresponding bits are different, the result is 1.
L value assignment operator: = + =-= * =/= % =. =
Operator priority: basically the same as js.
Data type conversion
In js, there is basically no concept of data type conversion, and the actual conversion is "automatic"-default conversion.
Js has two "similar" type conversion functions: parseInt (....) ParseFloat (...)
In php, both default conversion and forced conversion are supported.
Default conversion example:
$v1 = 10 - “3”; //7; $v2 = 10 + “3”; //13; $v3 = 10 + “3abc”; //13; $v4 = 10 + “3.5abc”; //13.5; $v5 = 10 + “abc3”; //10; $v5 = 10 + “abc”; //10 $v5 = “10” + “abc”; //10 $v5 = “10ABC” + “5abc”; //15 $v5 = “ABC10” + “abc5”; //0 $v5 = “ABC” + “abc”; //0
...................... Any operation using arithmetic operators will be regarded as a number. if it cannot be converted into a number, it will also be calculated as 0.
Forced conversion:
Syntax: (target type to be converted) data; example: $ v1 = (int) "3.5abc"; // 3; int type $ v1 = (float) "3.5abc "; // 3.5; // float type $ v1 = (string) 3; // "3"; $ v1 = (bool) "3.5"; // true
-- In particular, it is recommended to convert a data type to the bool type: query Manual: Appendix> type comparison table> compare the variable $ x> if ($ x) column
The above introduces the basic learning notes for php (12), including some content, and hope to be helpful to friends who are interested in PHP tutorials.