: This article mainly introduces imoocPHP study notes (getting started). If you are interested in the PHP Tutorial, please refer to it.
Variable
The defined variable is used to apply for space from the server's memory to store data.
For example:
$ Var_name = "apple ";
"=" Indicates the variable name on the left and the variable value on the right. After defining the variable, ;Tell the server that this line of command has ended. The variable name must pass $Symbol id;
Variable naming rules
1. the variable name must start with a letter or underscore "_", for example, "$ _ name", "$ name", "$ name2", etc, however, "$ 9name" is incorrect.
2. the variable name can only contain letters, numbers, and "_" and contain Chinese characters. For example, "$ _ qq", "$ qq308", "$ my_apple", and "$ name and I", but "$ name *" is incorrect.
3. variable names cannot contain spaces. When the variable name is composed of multiple words, we recommend that you use "_" to separate them (for example, $ my_apple), commonly known as the underline method, or start with an uppercase letter, for example, $ myApple, it is also known as the camel naming method ).
Note that the variable names in PHP are case sensitive. for example, "$ my_book" and "$ my_Book" indicate two different variables.
Boolean type
Boolean: there are only two values, one being TRUE and the other being FALSE, which can be understood as yes or no. It is case insensitive, that is, "TRUE" and "true" have the same effect.
Integer
Integer: similar to common integers. It can be specified in decimal, octal, and hexadecimal notation. Decimal is the number used in daily use. in octal notation, "0" must be added before the number (this 0 is an Arabic digit 0, not an English letter "ou"); hexadecimal format, "0x" must be added before the number ".
Floating point type
Float (floating-point number, double-precision number, or real number), that is, decimals, can be expressed by decimal points or scientific notation. Scientific notation can use lower-case e or upper-case E
String
A string is composed of a series of characters. in PHP, the character is the same as the byte. that is to say, there are a total of 256 different characters.
1.
String type can be defined in three ways: single quotes, double quotes, and Heredoc.
2.
When your string contains quotation marks ,:
Solution 1: Insert double quotation marks into single quotes, as shown in row 3;
Solution 2: insert single quotation marks into double quotation marks, as shown in row 3rd;
Solution 3: Use the escape character "\"
3. When double quotation marks contain variables, the variables are connected with the content in double quotation marks;
When a single quotation mark contains a variable, the variable is output as a string.
Special type-resource
Resource: resources are created and used by dedicated functions, such as opening files, connecting data, and drawing canvases. You can create, use, and release resources ). Any resources should be released in time when they are not needed. If we forget to release the resources, the system will automatically enable the garbage collection mechanism to recycle the resources after the page is executed to avoid memory consumption.
Example:
Special type-null type
NULL (NULL): NULL is a NULL type and is case insensitive. The NULL type has only one value, indicating that a variable has no value. if it is assigned NULL or has not been assigned a value, or unset (). in these three cases, the variable is considered NULL.
Constant
Constants in PHP are divided into custom constants and system constants.
1. Custom constants are defined based on our development needs. they are defined by using the define () function in PHP.
The syntax format of the define () function is:
bool define(string $constant_name, mixed $value,[$case_sensitive = true])
It has three parameters:
The first parameter "constant_name" is a required parameter. it is a constant name, that is, a flag. the naming rule of a constant is the same as that of a variable. Note that it does not contain the dollar symbol. The second parameter "value" is a required parameter, which is the value of a constant. The third parameter "case_sensitive" is an optional parameter, which specifies whether it is case sensitive. if it is set to true, it indicates no sensitivity. generally, if the third parameter is not specified, the default value of the third parameter is false.
2. the system constant is a defined constant in PHP. we can use it directly. common system constants include:
(1) _ FILE _: php program FILE name. It helps us to obtain the physical location of the current file on the server.
(2) _ LINE _: Number of PHP program files. It tells us the number of lines in the current code.
(3) PHP_VERSION: version number of the current parser. It tells us the version number of the current PHP parser. we can know in advance whether our PHP code can be parsed by the PHP parser.
(4) PHP_ OS: name of the operating system for executing the current PHP version. It tells us the name of the operating system used by the server, and we can optimize our code based on the operating system.
3.
There are two methods to obtain a constant value. The first is to directly obtain the value using the constant name;
The second is to use the constant () function.
4.
The defined () function helps us determine whether a constant has been defined. Its syntax format is:
Bool defined (string constants_name)
Only the constant_name parameter indicates the name of the constant to be obtained. if so, true is returned; otherwise, false is returned;
Operator
1.
PHP operators include arithmetic operators, value assignment operators, comparison operators, ternary operators, logical operators, string concatenation operators, and error control operators.
Arithmetic operators are mainly used for arithmetic operations, such as addition, subtraction, multiplication, and division operations. Common arithmetic operators in PHP correspond to the following table:
2.
There are two assignment operators for HP:
(1) "=": assigns the value of the right expression to the number of operations on the left. It copies the expression value on the right to the number of operations on the left. In other words, I first applied a piece of memory for the number of operations on the left, and then put the copied value in the memory.
(2) "&": reference value assignment, meaning that both variables point to the same data. It will make the two variables share a piece of memory. if the data stored in this memory changes, the values of the two variables will change.
3.
Comparison operators are mainly used for comparison operations, such as equal to, full, unequal, greater than, or less. The following table lists common comparison operators in PHP:
4.
("? : ") The ternary operator is also a comparison operator. for an expression (expr1 )? (Expr2) :( expr3). If the value of expr1 is true, the value of this expression is expr2; otherwise, it is expr3.
Logical operators
Logical operators are mainly used for logical operations, such as logical and, logical or, logical XOR, and non-logical operations. The following table lists commonly used logical operators in PHP: (click to see the big picture)
Among them, the or and xor ratios are lower than &, |.
String concatenation operator
The string concatenation operator is used to connect two strings. the following string concatenation operators are provided in PHP:
(1) concatenation operator ("."): it returns the string obtained after attaching the right parameter to the left parameter.
(2) join value assignment operator (". ="): It attaches the parameter on the right to the parameter on the left.
Error control operators in PHP
PHP provides an error control operator "@". for expressions that may encounter errors during running, we do not want to display error messages to customers when errors occur, this is unfriendly to users. Therefore, you can place @ before a PHP expression. any error information that may be generated by this expression is ignored;
If track_error is activated. set in ini). any error information generated by the expression is stored in the $ php_errormsg variable. this variable is overwritten every time an error occurs, therefore, if you want to use it, you must check it as soon as possible.
Note that the error control prefix "@" does not block parsing error information. it cannot be placed before the definition of a function or class, or be used for condition structures such as if and foreach.
Note: ceil method: double ceil (double x); function: return the minimum integer header file greater than or equal to the specified expression
If... Else...
If (condition) {// assign A server task A} else {// assign A server task B}
Note: date_default_timezone_set ('Asia/Shanghai'); sets the default time zone.
Date (format, timestamp)
Switch... Case...
The "switch... case..." syntax in PHP is as follows:
The while loop statement is as follows:
Note: $ num = rand (); // random number
Do... while loop statement syntax:
The for loop statement structure is as follows:
Foreach loop statement in PHP loop structure
In PHP, the foreach loop statement is often used to traverse arrays. There are two ways to use it: no subscript and no subscript.
(1) only values, not subscript
(2) obtain both the subscript and value.
Value) {// the task to be executed}?>
The above introduces imoocPHP study notes (getting started), including some content, and hope to be helpful to friends who are interested in PHP tutorials.