1. Understanding PHP Code Identification
It is very easy to write PHP code in the page, such as the following code:
Just as you can write JavaScript scripting languages that need to be written between tags, you can write PHP code in the page to write between tags, but note that the following?> can be omitted.
You can also calculate an equation:
2. String
A string is a word or a sentence enclosed in double quotation marks, for example: "Hi,jimo! ”。 You can output this string in the PHP language output, like this:
You can also enclose it in single quotes, just like the following code:
You can connect two characters with a connector, like this:
Note: the connector can concatenate multiple strings, and the example above is to split a string into two small strings and connect them with a connector.
In PHP, string connectors are represented by dots (.), which are more specific, and are represented by a plus sign (+) in other languages, such as: Javascirpt, ASP, Java.
3.PHP statement Terminator
Did you notice that there is a semicolon at the end of each line of PHP code?. Yes, note that in PHP programming you need to include semicolons at the end of each statement. Note, however, that the semicolon must be entered in the half-width state .
4. Notes
There are also comment statements in PHP: a double slash (//). In other languages, used in HTML , the/* Comment statement is used in CSS */. As in the following code:
Note Statements can be anywhere in the PHP program (but note statements must be written between them).
4. Naming rules for variable names
Variable names require "$" to be identified , and some rules need to be followed. Just like when you get your driver's license, you have to obey traffic rules. There are three key naming conventions for variable names:
1. Variable names must begin with a letter or underscore "_", such as "$_name", "$name", "$name 2", etc., but "$9name" is not correct.
2, the variable name can only be composed of letters, numbers, and "_", but also contain Chinese characters . such as "$_qq", "$QQ 308", "$my _apple", "such as" $name I Wait ", but" $name * "is wrong.
3. Variable names are not allowed to contain spaces. When the variable name consists of multiple words, it is recommended to use "_" to separate (for example $my _apple), commonly known as the underscore method, or start with a capital letter such as $myApple, commonly known as camel-named (also known as the Hump name).
In particular, it is important to note that in PHP variable names are case- sensitive , such as "$my _book" and "$my _book" represent two different variables.
5. What is a variable?
PHP is a loosely-typed language that does not have to declare the variable's data type to PHP, and PHP automatically converts the variable into an automatic data type, which reduces the threshold of learning PHP to a certain extent.
With the "var_dump" function, the output data type:
"; $string = 9494; Var_dump ($string); echo "
";? >
In variables, because the variables occupy a different space unit (the site size is not the same), but also divided into several data types, like the supermarket packaging bags, there are several different types of goods, different products using various bags. We can get the memory consumed by the current PHP by using "Memory_get_usage".
"; $var _string =" 123 "; echo $m 2 = Memory_get_usage ()-$m 1;echo"
"; $n = 123;echo $m 3 = memory_get_usage ()-$m 1-$m 2;echo "
"; $f = 123.00;echo $m 4 = memory_get_usage ()-$m 1-$m 2-$m 3;echo "
"; $var _array = Array ("123"), echo $m 5 = memory_get_usage ()-$m 1-$m 2-$m 3-$m 4;? >
6. Data types for variables
In PHP, 8 primitive types are supported, including four scalar types, two composite types, and two special types.
1. Scalar Type-Boolean type
2. Scalar type-integral type
3. Scalar type-float type
Floating-point (floating-point, double-precision, or real-number), which is usually said to be decimals, can be represented by a decimal point or by scientific notation. The scientific notation can be used in lowercase e, or in uppercase E.
4. Scalar Type-string (1)
Strings are composed of a series of characters, in php, characters and bytes, that is, a total of 256 different characters of the possibility.
The string type can be defined in three ways: single-quote, double-quote, and Heredoc-structured.
4. Scalar Type-string (2)
What do we do when you include quotes in your string?
"Echo $str _string2;echo"
"Echo $str _string3;echo"
"Echo $str _string4;echo"
";? >
4. Scalar Type-string (3)
When quotation marks meet the dollar? The dollar is not a real dollar, but a variable marked with a dollar sign.
Rules:
When a variable is enclosed in double quotes, the variable is concatenated with the contents of the double quotation mark;
When a variable is included in a single quotation mark, the variable is exported as a string.
"; Echo $string 2;? >
4. Scalar Type-string (4)
What happens when my string is long?
We can solve this problem by using the Heredoc structure form, first using the delimiter to represent the string (<<<), then providing an identifier after "<<<" God, then the string, Finally, the string is terminated with this identifier provided.
Note: at the end of the line, be sure to start another line, and this line except "GOD", and ";" After the end, can not have any other characters, before and after cannot have, including spaces , otherwise it will appear wrong oh. Remember, remember.
5. First special type-resources
Resources (Resource): resources are created and used by specialized functions, such as open files, data connections, and graphical canvases. We can manipulate resources (create, use, and release). Any resources should be released in a timely manner when they are not needed. If we forget to release the resources, the system automatically enables the garbage collection mechanism to reclaim the resources after the page has been executed to avoid the memory being exhausted.
Examples are as follows:
6. Second special type-null type
Null (NULL): null is a null type, is not case sensitive, the null type has only one value, indicating that a variable has no value, when it is assigned to NULL, or has not yet been assigned, or is unset (), the variables are considered NULL in three cases.
Cases:
7. What is a constant
What is a constant? Constants can be understood as a constant amount of value (such as pi), or when a constant value is defined, it cannot be changed anywhere else in the script. Constants in PHP are divided into custom constants and system constants (which are described in detail later).
A custom constant is a constant defined according to the needs of our development, and it is defined by using the function define () in PHP. (Note: function, we can be understood as a concrete mixer, or a sieve, through the entrance to provide raw materials, and then export output results, in the function also allows the entrance does not provide any data, export also allows to return no value. )
The syntax format for the define () function is:
BOOL Define (String $constant _name, mixed $value [, $case _sensitive = True])
It has 3 parameters (i.e. raw material):
The first parameter, "Constant_name", is a required parameter, a constant name, or a marker, and the name of the constant is consistent with the variable, but be aware that it does not have a dollar sign . The second parameter, "value," is a required parameter, which is the value of the constant. The third parameter, "Case_sensitive", is an optional parameter, specifying whether it is case sensitive , set to true to be insensitive, and generally without specifying a third parameter, the value of the default third parameter is False.
( Note: string indicates that the parameter type is a string type, mixed indicates that the parameter type can be accepted as a number of different types, Case_sensitive = True indicates that the default is Boolean type True)
Example: We define PI as a constant "PI" (usually all uppercase) with a value of 3.14:
"; Echo PII;? >
8. System Constants
System constants are constants that PHP has already defined, and we can use them directly, and common system constants are:
(1) file:p hp program filename. It can help us get the current file in the physical location of the server.
(2) line:P HP program file number of lines. It can tell us the current code in the first few lines.
(3) Php_version: The version number of the current parser. It can tell us the current PHP parser version number, we can know in advance whether our PHP code can be parsed by the PHP parser.
(4) Php_os: Executes the current PHP version of the operating system name. It can tell us the name of the operating system used by the server, and we can optimize our code based on the operating system.
Cases:
"Echo __line__;echo"
"Echo Php_version;echo"
"Echo Php_os;echo"
";? >
9. How constants are evaluated
Constants are defined, then constants are used, so how do we get constant values?
There are two ways to take a value for a constant value.
The first is to get the value directly using the constant name, for example to calculate the area of pi, as follows:
The second is the use of the constant () function. It is the same as the direct use of the output of the constant name, but the function can be dynamically output different constants, in the use of flexible, convenient, the syntax format is as follows:
Mixed constant (string constant_name)
The first parameter, Constant_name, is the name of the constant to get, or a variable that stores the constant name. If successful, the value of the constant is returned, and failure indicates that the error message constant is not defined. (Note: Mixed indicates that the return value type of the function is of many different types, string indicates that the parameter type is a string type)
Cases:
10. How to determine if a constant is defined
If the constants are repeatedly defined, the PHP parser will issue a "Constant XXX already defined" warning to remind us that the constant has been defined. So how do we determine if a constant is defined when team development, or the amount of code is great?
The defined () function helps us to determine whether a constant has been defined and its syntax is:
bool Defined (string constants_name)
It only has the parameter constant_name, refers to gets the name of the constant, returns the Boolean type True if present, otherwise returns the Boolean type false; (Note: bool means the return value type of the function is a Boolean type)
Cases:
12. Operators
PHP operators are generally divided into arithmetic operators, assignment operators, comparison operators, ternary operators, logical operators, string join operators, error control operators.
1. Arithmetic operators : +,-,*,/,%
2. There are two types of assignment operators:
(1) "=": assigns the value of the right expression to the left operand. It copies the value of the right expression to the left-hand operand. In other words, the left operand is first requested for a piece of memory, and then the copied value is placed in this memory.
(2) "&": Reference assignment means that both variables point to the same data. It will allow two variables to share a piece of memory, and if the memory stored data changes, then the values of the two variables will change.
Cases:
"; echo $c. "
";? >
3. Comparison Operators
Comparison operators are mainly used for comparison operations, such as: equals, congruent, unequal, greater than, less than. The comparison operators commonly used in PHP are the following table:
4. Ternary operators
("?:") The ternary operator is also a comparison operator, for expressions (EXPR1)? (EXPR2):(EXPR3), if the value of EXPR1 is true, the value of this expression is expr2, otherwise EXPR3.
5. Logical Operators
Logical operators are used for logical operations such as logic and logic, logical XOR, logical negation, and logical operators commonly used in PHP, such as the following table:
6. String Join Operators
The string join operator is to concatenate two strings, and the string join operators provided in PHP are:
(1) Join operator (".") : It returns the string that is appended to the right argument after the left argument.
(2) Connection assignment operator (". ="): It attaches the right argument to the left argument.
Cases:
"; echo $b. "
"; echo $c. "
";? >
7. Error control operators
An error control operator "@" is provided in PHP, and we do not want an error message to be displayed to the customer when there are some expressions that may be wrong during the run, which is unfriendly to the user. Therefore, you can put the @ before a PHP expression, any error message that the expression may produce is ignored ;
If the Track_error (which is set in php.ini) is activated, any error message generated by the expression is stored in the variable $php_errormsg, which is overwritten every time an error occurs, so you must check it as early as possible if you want to use it.
It is important to note that the error control prefix "@" does not mask the parsing of the error information, it cannot be placed before the definition of a function or class, nor can it be used for conditional structures such as if and foreach .
Cases:
13. Structure
Selection and looping structure and other languages are similar, not much introduction, only introduce different:
The Foreach Loop statement for the loop structure
In PHP, the Foreach Loop statement, commonly used to iterate over an array, is generally used in two ways: Do not remove the label, remove the label.
(1) value only, do not remove the label
Cases:
' Make Fox punch ', ' ' 林平 ', ' ' (' + ') ' ' and ' ' Qu Yang ', ' ' ~ ' ~ ' to ' ren Ying ', ' ' ~ ' and ' to ask the day ', ' ' ~ ' and ' Let Me line ', ' ~ ' and ' Chong virtual ', ' "=" and "Founder", ' 2018 ' and ' Yue Qun ', ' 2019 ' and ' Ning Zhong ', ';//////////////////////////////////////////////////// V) { echo $v;//output (print) name echo "
";}? >
(2) Remove the mark and value at the same time
Value) {//Perform the task}?>
Example: The above example:
foreach ($students as $key + $v) { echo $key. ":". $v;//output (print) Number: Name echo "
";}? >