PHP Notes 01, phpnotes01
■ PHP's basic syntax (PHP references the C language to a large extent in terms of syntax) PHP scripts are executed on the server and then sent back to the browser for pure HTML results. The default file extension for PHP files is ". php ". PHP files usually contain HTML tags and some PHP script code. PHP script with <? Php, starting with?> End. <? Phpecho 'Hello world';?> <? = $ Variable name?> Only one variable is displayed. ■ PHP supports three Annotations: <? Php // This is a single line comment (single line comment in the C language style ). # This is also a single line comment (Shell-style single line comment in Linux ). /* This is a multiline comment block that spans multiple lines (c-style multi-line comment ). */?> ■ The Data Types of variables in PHP are not fixed. The data types of PHP variables are determined by the runtime context. When a variable is not assigned a value, the system does not allocate storage space for it, that is, Null. That is to say, PHP is a [Weak data type] programming language.★In PHP, variables will be created when they are assigned a value for the first time: $ variable name = variable value; (the naming rules for PHP variables are the same as those for C)★Display the Data Type of the variable: Var_dump ($ variable name );★Note: variable names in PHP are case sensitive. Function names, method names, and class names are case-insensitive. (☆In C language, both the variable name and function name are case sensitive .) ■ Three assignment values of PHP ① pass Value assignment: in PHP, when a variable is assigned a constant value, a variable container is generated. The type and value of the variable are stored in the variable container. For example: $ a = 5; $ s = "new string"; ② Reference Value assignment:★Referencing in PHP means accessing the same variable content with different names. It is like a hard link. For example, $ a = & $ B; indicates that the address (pointer) of $ a is the same as that of $ B. That is, they all occupy the same storage space in the memory. Unset ($ a); // does not affect $ B. PHP references do not have any relationship with the pointer variable of C semi-cents. Their working principles are as follows: [image: PHP references and c .png] ③ reference count assignment:★Similar to the value assignment between two pointer variables in C language, they pass the address (pointer) of a data ). For example: $ a = new man (); in this case, the value of $ a is an address and an object address ($ a may think of the pointer variable of C at this time, because their values are pointers ). That is to say, $ a points to this object. In fact, the value of $ a is very rich, not only the address of this object, but also a "reference counter". This "reference counter" is used to calculate how many variables point to this object. Assume that the value of $ a is 0X00FD (1), 0X00FD is the object address, and (1) is "reference counter ". If: $ B = $ a; at this time, the value of $ B is 0X00FD (2), that is, the $ a value assignment operation on $ B passes the object address (pointer ), this assignment method is [reference count Assignment ]. In PHP, there are two types of data that assign values to the reference count, which are object and resource types respectively.★PHP garbage collection mechanism: in fact, in PHP, each variable has a "reference counter", that is, "refcount ". "Refcount" is used to indicate the number of variables (also known as symbols (symbol) pointing to the variable container. The variable container is destroyed when "refcount" is changed to 0. When any variable associated with a variable container leaves its scope (for example, function execution ends), or the function unset () is called for the variable, "refcount" is reduced by 1. -- Source: http://php.net/manual/zh/features.gc.refcounting-basics.php ■ PHP Data Type basic type: integer, float type, String, Boolean Type (true and false) construction type: array, object (object) Special Type: null type, resource type such as: $ con = mysql_connect ("... ","... ","... ");(★The PHP resource type is "channel ". Resources can be obtained along the "channel .) In PHP, the Data Type of the variable is not fixed, and the Data Type of the PHP variable is determined by the context of the runtime. When a variable is not assigned a value, the system does not allocate storage space for it, that is, Null. That is to say, PHP is a [Weak data type] programming language. Display the Data Type of the variable: Var_dump ($ variable name );★Note: data is stored in binary format in computers. When floating point data is converted from decimal to binary, there is an error. If you want to achieve high precision, such as the amount of records, you should try to avoid using floating point numbers for storage. We can use integer storage, for example, "Use minute-to-minute" for storage, that is, 0.01 yuan (that is, 1 minute) and 1 storage. Divide the display time by 100. PHP contains string variables, for example, $ s = "Hello World! "; PHP has a boolean type: true is true, false is false (Case Insensitive ).★The following values are treated as false: ① boolean type false itself; ② integer value 0 (0); ③ floating point value 0.0 (0); ④ null string, and the string "0"; ⑤ does not include any element array; ⑥ does not include any member variable object (applicable only to PHP4.0 ); 7. Special Type null (including variables not assigned ). Tip: "If it is zero, it is empty, it is false." "If it is non-zero or not empty, it is true ."★Forced type conversion: like the C language, PHP can also directly add (type) before the variable to force type conversion on the variable. The format is as follows: (type) $ other types of variable names can be converted to integer types through intval (variable. Other types can be converted to floating point type through floatval (variable. Other types can be converted to strings through strval (variable.★Note that the data type conversion in PHP is totally different from that in C: in PHP, for example, (int) 'A' is equal to 0 [Forced data type conversion ]; in PHP, for example, 'A' + 3 is equal to 3 [automatic data type conversion]; in C, for example: (int) 'A' is equal to decimal 97; in C, for example: 'A' + 3 is equal to decimal 100. In PHP, 'abc' + 3 is equal to 3. ■ The dynamic name of PHP is in PHP, variable, function, object, and class names are flexible. You can even use variables to represent the above names. For example, the dynamic variable name: $ laoDa = 'Liu bei '; $ lao2 = 'guan Yu'; $ lao3 = 'zhang fei'; $ who = 'laoda'; echo $ who; // output: Liu Bei $ shui = 'who '; echo $ shui; // also output: Liu Bei dynamic class name, for example: $ class = 'human ', 'dog', or 'cat'; then new $ class can dynamically obtain the human object, dog object, or cat object. ■ Meaning of constant constants in PHP: Once defined, it cannot be changed (not allowed to be assigned a value, not allowed to be redefined, or cleared ). The general form of defining constants: define ('constant name', constant value); PHP5.3 and later support the keyword const to define symbol constants. The general form is: const constant name = constant value; for example: define ('pie', 3.14); $ r = 5; echo 'Perimeter ', $ r * PIE * 2, '<br/>'; // perimeter echo 'Perimeter ', $ r * PIE,' <br/> '; // Area echo 'Perimeter ', $ r * PIE * 4/3; // volume note: ① once a constant is defined, it cannot be assigned a value, be redefined, or be cleared. ② Constants are globally valid. Therefore, they can be directly referenced in pages, functions, classes, and even arrays without considering the scope. ③ The constant value cannot be an array, object, or resource type. Dynamic constant name: You can use the constant function to read the constant value. The general form is: constant (constant name) to implement dynamic constant name example: define ('laoda', 'Liu bei '); $ who = 'laoda '; echo constant ($ who); // output: Liu Bei ■ PHP operator note ☆in C language, 3/2 will get 1, because the integer is divided by the integer, and the result is also an integer. In PHP, 3/2 equals 1.5. Note: in PHP, the positive and negative results of the modulo operator (remainder operator) % are only the same as the number on the left. For example:-3%/x, regardless of x, the result is a negative number.★Front ++ and back ++: $ B = $ a ++; // equivalent to $ B = $ a; $ a = $ a + 1; $ B = ++ $ a; // equivalent to $ a = $ a + 1; $ B = $ a; equality ===: the values and types must be equal. Incomplete! ===: The values or types are not equal.★PHP logical operators: & and (and) | or! Non-xor exclusive or: If either party is true or the other party is false, true is returned.★The short-circuit feature of logical operators: (C, PHP, and JAVA all have this feature) the [combination] of logical operators is from left to right. If the content on the left (Front) can be used to determine the result of the entire expression, the content on the right (back) will not be executed. For example: $ a = 1; $ B = 0; if ($ a | $ B ++) {echo $ B; // output: 0} because of the logic or operator | after determining that $ a is true, it can be concluded that the result of the entire expression is true and there is no need to continue running, and all subsequent content is not executed. Therefore, the value of $ B is still 0. For example, $ a = 0; $ B = 0; if ($ a & B ++) {echo $ B; // output: 0} since the logic and operator & judge $ a as false, the result of the entire expression can be obtained as false. It is not necessary to continue running and all subsequent content is not executed. Therefore, the value of $ B is still 0.★String OPERATOR ). Purpose: concatenate strings. (String concatenation is much easier than c) Type Operator: instanceofinstanceof is used to determine whether a variable belongs to an instance of a class. Usage format: $ variable instanceof class name ☆bit OPERATOR: hexadecimal integer data. It starts with 0x during editing and is automatically converted to hexadecimal during output. For example, echo 0x123; // output: 291 integer data in hexadecimal format. The value starts with 0 during editing, and the output value is automatically converted to decimal format. For example, echo 0123; // output: 83 ● bitwise AND & function: Calculate the two numbers involved in the operation by "and" for each binary bit. Example: $ a = 12 & 5; 12: 1100 5: 0101 & ----------- 0100 is 4 echo $ a; // output: 4 ● by bit or | function: calculate the two numbers involved in the operation by "or" each binary bit. For example: $ a = 12 | 5; 12: 1100 5: 0101 | ----------- 1101, that is, 13 echo $ a; // output: 13 ● bitwise OR ^: calculate the two numbers involved in the operation in an "XOR" manner based on each binary bit. Example: $ a = 12 ^ 5; 12: 1100 5: 0101 ^ ----------- 1001 that is 9 echo $ a; // output: 9 ● non-~ by bit ~ Purpose: Calculate this number based on each binary bit for "inverse. Example: $ a = ~ 12; 12: [0000 0000] [0000 0000] [0000 0000] [0000 1100] ~ --------------------------------------------- [1111 1111] [1111 1111 1111] [1111 1111] [0011] [NOTE: it is a supplemental Code]-13 echo $ a; // output: -13 ■ return value of expression 1 + 2 the value of this expression is 3 2*3 the value of this expression is 6 so what is the value of a = 5? Does the value assignment expression have a value?★As long as it is an expression, it will inevitably return a calculation result (that is, the return value ).★The calculation result (return value) of the value assignment expression is the value of the variable on the left of the value assignment operator (that is, "=. For example, echo ($ a = 5); // output: 5 the return value of a relational expression (an expression calculated using a relational operator) is Boolean (true or false ). For example: var_dump (5> 2); // the return value of the logical expression bool (true) is Boolean (true, false ). For example: var_dump (99 & 0); // output: bool (false) ☆in PHP, two integers are separated, and floating point numbers may be obtained. This is different from "strong data type languages" such as C and java. Example: echo 3/2; // output: 1.5★Echo is not a function (it is a language structure ). Interview question-which of the following statements is the fastest? echo "Hello". "". "world "."! "; Echo 'Hello'.''. 'World '.'! '; Echo 'hello', '', 'World ','! '; Answer: the third statement is the fastest. Because single quotes are faster than double quotes, all the second statements are faster than the first statement. Because "." is a string operator, operations are required. In the third statement, "," only outputs three consecutive strings without computation. All the third statements are faster than the second statement. ■ Control structure Sequence Structure Selection structure: if, else if, switch loop structure: for, while, do-while, foreach switch ($ today) {case 'mon ': echo 'Monday today, monkey wear a new yie'; break; case 'tue ': echo 'tuesday today, monkey serves as a kid'; break; case 'wed': echo' today, Wednesday, monkeys go hiking; break; case 'thu': echo 'Monkey is doing well today thurs'; break; case 'fri': echo 'today Friday, monkey is hitting Tiger'; break; case 'sat ': echo 'Saturday, monkey missing'; break; case 'sun': echo 'Sunday, monkey off '; break; default: // The placement of the default language does not affect the result. Echo 'Monkey doesn't understand what you are talking about '; break ;}★Differences between if statements and switch statements and application scenarios: if statements are used to determine a specific range, while switch statements are used to determine a specific value. ☆The value of a computer has a loop.★While and do-while: for example, there is a big brother who wants to pay for your debt. While is to first ask you not to pay back the money. Also, he will let you go. If you don't pay back, he will beat you. Do-while is to hit you first and ask you not to pay back the money. Also, he will let you go. If you do not pay back, he will continue to beat you. (That is to say, do-while is at least a hit no matter you don't pay back) break statement: Used to end the current for, while, do-while, switch statement. It can be followed by a number to indicate the number of floors to exit. For example, break 2; // indicates that the client exits to Layer 3. The default value is break 1. The continue statement is used to skip the remaining code in this loop and start executing the next loop. You can follow a number to skip multiple cycles. For example, continue 2; // indicates that duplicate loops are skipped once. The default value is continue 1. The goto statement is usually used to exit the loop structure or switch statement. It can replace multiple levels of break statements. Example: goto a; a: echo 'a ';