PHP related knowledge

Learning objective: PHP language

learning target:

1. Learn the php language and summarize the commonly used functions and their parameters;
2. compare the similarities and differences of echo, print, and var_dump;
3. understand bash and shell.

Learning Content:

1. Introduction to
php 1. Definition of php: the "hypertext preprocessor" is a script/programming language executed on the server side, especially suitable for web development and embedded in HTML. PHP syntax utilizes C, Java, and Perl. The main goal of the language is to allow web developers to quickly write dynamic web pages.
2. Use of php: dynamic website development
2. Website
1. Static website
①The content of static website is relatively stable, so it is easy to be retrieved by search engines;
②The website is more secure, HTML pages will not be affected by Asp related vulnerabilities; and it can be reduced Attack, prevent SQL injection. When a database error occurs, it does not affect the normal access of the website;
③It reduces the burden on the server and reduces the workload, which also reduces the cost of the database;
④The interactivity of static web pages is poor and has greater limitations in terms of functions.
2. Dynamic website
① A dynamic website does not refer to a website with animation functions, but a website whose content can be dynamically changed according to different situations. Generally, a dynamic website is structured through a database;
② A dynamic website can realize interactive functions, such as users Registration, information release, product display, order management, etc.;
③Dynamic web pages contain server-side scripts, so page file names often have asp, jsp, php, etc. as suffixes. But you can also use URL static technology to display the web page suffix as HTML. Therefore, the suffix of the page file cannot be used as the only criterion for judging the dynamic and static of the website;
④ There is a symbolic symbol in the dynamic webpage URL-"?".
3. The basic concept of the website
① Server:
<1> The server is also called a server, which is a device that provides computer services. Since the server needs to respond to service requests and process them, in general, the server should have the ability to undertake and guarantee services;
<2> The composition of the server includes processor, hard disk, memory, system bus, etc., and is similar to the general computer architecture, but due to the need to provide reliable services, it is in terms of processing power, stability, reliability, security, and scalability. , Manageability and other aspects have higher requirements;
<3> In the network environment, according to the different types of servers provided by the server, it is divided into file servers, database servers, application servers, and WEB servers.
②IP concept: Internet Protocol, interconnection protocol between networks. The interconnection protocol between networks is also a protocol designed for computer networks to communicate with each other.
③Domain Name is the name of a computer or computer group on the Internet composed of a series of names separated by dots (www.itcast.cn), which is used to identify the electronic location of the computer during data transmission.
Examples: IP: 127.0.0.1 Domain name: localhost
④DNS: (Domain Name System, domain name system), on the Internet as a distributed database that maps domain names and IP addresses to each other, enabling users to access the Internet more conveniently.
Domain name resolution: The user enters the domain name, and DNS will convert the domain name to IP to find the server.
4. Web program access process
Insert picture description here
① The user enters the request URL in the browser address bar to initiate the request.
②Resolve the IP address through the DNS server and find the corresponding host.
③According to the type of script:
If it is a file ending in .html, it will be directly returned to the browser.
If it is a file ending in .php, you need to execute the PHP script first.
In addition, during the execution of the PHP script, you may need to connect to the database to obtain some data information.
After all the code is executed, Apache sends the execution result to the browser for display.
Three, php basics
1. Preliminary php syntax: php is a scripting language that runs on the server and can be embedded in HTML.
2. PHP code tag: Script tag: <script language="php>php code
Standard tag: <?php php code?>
3. PHP comment:
① Line comment: comment one line at a time //
② Block comment: comment multiple lines at a time /* in the middle until */
4. PHP statement separator
Statement separator: In PHP, the code must pass the end of the line by the line unit system. The end is usually a symbol: semicolon ";" (in English Semicolon)
Special instructions:
1. The tag end character in PHP?> comes with the effect of the statement end character, the last line of PHP code can have no statement end character;
2. In fact, many codes in PHP are not embedded in HTML. Does it exist alone. It is generally not recommended to use tag end characters in writing habits?>
Insert picture description here
Insert picture description here
5. Variable
PHP is a scripting language for dynamic website development. Dynamic language is characterized by interactivity and data transmission, and PHP acts as an "middleman". Need to transfer data, the premise of the transfer is that PHP can store data by itself (temporary storage)
①Variables are used to store data;
②Variables have names;
③Variables are accessed by names;
④Variables can be changed .
6, using the variables
① definition: increase the variable name corresponding to the (memory) in the system
② assignment: may assign data to a variable name (may be done at the same definition)
③ by a data variable name to access the stored
can ④ variable Delete from memory.
Insert picture description here

7. Variable naming rules
①The variable name in PHP must start with the "$" symbol;
②The name is composed of letters, numbers and underscore "_", but cannot start with a number;
③Chinese variables are also allowed in PHP itself (not recommended ).
8. Pre-defined variables
Pre-defined variables: pre-defined variables, system-defined variables, store a lot of data that needs to be used (pre-defined variables are arrays)
$ _GET : get all the data submitted by the form by get
$ _POST : POST The submitted data will be saved in this
$ _REQUEST : GET and POST submissions will be saved
$GLOBALS: all global variables in PHP
$ _SERVER : server information
$ _SESSION : session session data
$ _COOKIE : cookie session data
$_ENV: environmental information

9. Variable variable
Variable variable: If the value of one variable is just the name of another variable, you can directly access one variable to get the value of another variable: add a $ sign in front of the variable.
$a ='b';
$b ='bb';
$$a->bb
Insert picture description here
10. Variable pass value
① Definition: Assign one variable to another variable: Variable pass value

②There are two ways of variable value transfer: value transfer, reference transfer
<1> value transfer: assign a value to the variable saved, and then save the new value to another variable (the two variables are not related)
Insert picture description here
<2> Pass by reference: pass the memory address where the value of the variable is stored to another variable: two variables point to the same memory space (two variables have the same value)
$new variable = & $old variable; (&address symbol)
Insert picture description here

11. Memory
In the memory, there are usually the following partitions.
Stack area: the part of the memory that the program can manipulate (no data is stored, and the program code is run), and the small but fast
code segment: the memory part of the stored program (only the code is stored without executing the code) )
Data segment: store common data (global area and static area)
Heap area: store complex data, large but low in efficiency.
12. Constant
①The basic concept of
constant: constant : constant/constant, which cannot be changed during program operation
Once the quantity (data) constant is defined, usually the data cannot be changed (user level)
②Constant definition form
There are two ways to define constants in PHP (only two after 5.3)
<1>Use the function to define constants: define('constant Name', constant value);
<2> Only after 5.3: const constant name = value;
③The naming rule of constant name
<1>Constant does not need to use the "$" symbol, once it is used, the system will consider it as a variable;
< 2>The name of a constant is composed of letters, numbers and underscores, and cannot start with a number;
<3>The name of a constant is usually a capital letter (to distinguish it from a variable);
<4>(The second point is not completely correct :) The naming rules of constants are looser than variables, and some special characters can be used. This method can only use define to define
constants. Values ​​must be assigned during definition.
13. System constants
System constants: constants defined by the system help users, users can directly use

Several commonly used system constants
PHP_VERSION: PHP version number
PHP_INT_SIZE: integer size. (1 byte 8 bits. 32 bits 4 bytes, 64 bits 8 bytes)
PHP_INT_MAX: the maximum value that an integer can represent (integers in PHP are allowed to appear negative numbers: signed)
14, data type
① definition: data type : Data type, in PHP refers to the type of the stored data itself, not the type of the variable. PHP is a weakly typed language, and the variables themselves have no data types.
②Classification:
simple (basic) data type: 4 sub-categories
<1> integer type: int/integer, the system allocates 4 bytes for storage, indicating the integer type (with preconditions)
<2> floating point type: float/double, The system allocates 8 bytes for storage, indicating that a decimal or an integer cannot be stored (such as a 32-bit integer that cannot be stored)
<3>String type: string, the system allocates according to the actual length, indicating a string (quotation mark)
<4 >Boolean type: bool/boolean, which means Boolean type, only two values: true and false

Compound data type: 2 small classes
<5> Object type: object, storage object (object-oriented)
<6> Array type: array, storage multiple data (one-time)

Special data types: 2 sub-categories
<7> Resource type: resource, storing resource data (PHP external data, such as databases, files)
<8> Empty type: NULL, only one value is NULL (cannot be calculated)
15. Type conversion
①Definition: Type conversion: Under many conditions, the specified data type needs to be converted to the target data type, and external data (data obtained by PHP) is required.
There are two types of conversion methods in PHP:
②Classification <1> Automatic Conversion: The system judges and converts itself according to the needs (more used, the system judges the required type by itself, and the efficiency is low)
<2>Forced (manual) conversion: It is considered to be converted according to the required target type. The
forced conversion rule: before the variable Add a bracket (), and then write the corresponding type: int/integer.... Among them, the NULL type uses unset().
In the conversion process, the more used ones are the conversion to Boolean type (judgment) and conversion to numeric type (arithmetic Operation)
Converting other types to boolean types: true or false. In PHP, fewer types will become false.
Description: Description of other types converting values
1) Boolean true is 1, and false is 0;
2) String conversion value has its own Rule
3) A character string starting with a letter is always 0;
4) A character string starting with a number is taken until it encounters the character string (will not contain two decimal points at the same time)
16. Operator
① Assignment operation: The symbol is " =”, which means that the result on the right (which can be the result of variables, data, constants and other calculations) is saved to a certain location in the memory, and then the memory address of the location is assigned to the variable (constant) on the left.
② Arithmetic operation: basic arithmetic operation
+: perform data accumulation
-: Data subtraction
: There is no multiplication symbol on the keyboard. Use instead. Multiply two numbers.
/: Forward slash instead, means dividing two numbers.
%: Remainder (modulo) operation, division of two numbers (integer) ,Reserve the remainder
When performing division or remainder operation, the corresponding divisor (the second number) cannot be 0
③Comparison operation: compare the size of two data, or whether the two contents are the same, the returned result is Boolean Type: returns true if satisfied, false if not satisfied

: The left is greater than the right, and the return result is true
=: the left is greater than or equal to the right
<: the left is less than the right
<=: the left is less than or equal to the right
==: the left is the same as the right (the same size)
!=: the left is different from the right ( Different size)
===: All equals, the left side is the same as the right side: the size and data type must be the same
Insert picture description here
Insert picture description here

④Logical operation: match different results. Return true if the condition is met, false if it is not met
&& and and: logical AND, the condition on the left and the condition on the right are established at the same time (both results are true)
|| and or: logical OR, there is only one condition on the left or on the right Just be satisfied
! : Logic negation, invert the existing condition, it is true, the result of the inversion is false.
Insert picture description here
Logic AND logic or short-circuit operation: If the first expression result has met the condition, then it will not run The expression after the logical operator: When writing the code, try to put the expression with the highest probability of occurrence (that can directly determine the result) in the first place to improve the efficiency of the operation.
17, the connection operator
connection operation: in PHP A symbol for concatenating multiple strings
.: Connect two strings together
. =: Compound operation, connect the content on the left with the content on the right, and then re-assign to the variable
A on the left .= b ⇔ A = A. b
Insert picture description here
18. Self-operation operator
Self-operation: Operate your own operator
++: +1
on the original value –: -1 on the original value

$a = 1;
$a++; // $a = $a + 1;

In PHP, self-operators can be placed before or after variables: pre-self-operation and post-self-operation
$a = 1;
a + +; + + a++; ++a++;+ + a; //If the front or rear only has self-operation and does not participate in other operations (self-operation at the same time), then the effect is the same. But if the self-operation is also involved in other operations, the effect is different
$a = 1;
$b =a + +; / / a++; //a++;/ / A ++ cause $ A = A + $. 1;A = 2;, above a = 2 ;, abovea=2;, On the surface of B. 1 =
C = C = ++ + +c=+ + a; //++a will cause a will causea meetinga = $ a Tasu 1;a = 2;, a = 2 ;,a=2;c = 2;

Post-self-operation: first save the value you saved, and then change yourself, the value you give to others is the original value;
pre-self-operation: first change yourself, and then give the changed value to the
c language Similar to
19. Summary of commonly used functions
1. Output function
print(): Similar to the content provided by echo output, it is essentially a structure (not a function), and returns 1, without using parentheses (because it is a structure not a function)
print_r() : Similar to var_dump, but simpler than var_dump, it will not output the type of data, only the value (array printing is used more)

and var_dump() is to judge the type and length of a variable, and output the value of the variable, if the variable has Value, the output is the value of the variable and the data type is returned.
The echo function is actually not a function, it can output multiple variables continuously, but print can only output one at a
time. 2. The time-related function
Insert picture description here
date(): According to the specified format, the corresponding timestamp (calculated from 1970 Greenwich Mean Time) Seconds) into the corresponding format. If no specific time stamp is specified, then the current time stamp is interpreted by default.
time(): Get the timestamp corresponding to the current time
microtime(): Get the time in microseconds
Insert picture description here

3. Mathematical function
max(): specify the largest value in the parameter
min(): compare the smaller value of the two numbers
rand(): get a random number, a random integer in the specified interval
mt_rand(): same as rand , But the underlying structure is different, and the efficiency is higher than rand (recommended)
round(): rounding
ceil (): rounding up
floor(): rounding down
pow(): finding the specified exponent of the specified number The result: pow( 2,8) == 2^8 == 256
abs(): absolute value
sqrt(): find the square root
4. Function of related functions
function_exists(): determine whether the specified function name exists in the memory (help users not to use A non-existent function makes the code safer)
func_get_arg(): Get the parameter corresponding to the specified value in the custom function-actual parameter position
func_get_args(): Get all the parameters (array) in the custom function-
-All actual parameters func_num_args(): Get the number of parameters of the current custom function-Number of actual parameters
Insert picture description here
5. String related functions
1) Conversion functions: implode(), explode(), str_split()
implode(connection method, array): Connect the elements in the array into a string
explode (split character, target string) according to a certain rule : split the string according to a certain format into an array
China|Beijing|Shunyi == array('中国', 'Beijing','Shunyi');
str_split(string, character length): split the string according to the specified length to get an array

2) Intercept function: trim(), ltrim(), rtrim()
trim(string[,specified character]): By default, it is used to remove the spaces on both sides of the string (not in the middle), but you can also specify which to remove The content is to remove the content on both sides according to the specified content cycle: until it encounters a character that is not the target character
ltrim(): remove the left
rtrim(): remove the right
3) interception function: substr(), strstr()
substr( String, the starting position starts from 0 [,length]): The string is intercepted at the specified position, and the specified length can be intercepted (not specified to the end)
strstr (string, matching character): From the specified position, intercepted to the end ( Can be used to take the file suffix)
4) Size conversion functions: strtolower(), strtoupper(), ucfirst()
strtolower: all lowercase
strtoupper: all uppercase
ucfirst: first letter uppercase
5) Search function: strpos(), strrpos()
strpos (string, matching character): Determine the position of the character in the target string (first time)
strrpos (string, matching character): Determine the position of the last character in the target string
6) Formatting function: printf( ), sprintf()
srintf/sprintf (the output string has placeholders, the order of the content...): format the output data
7) Other: str_repeat(), str_shuffle()
str_repeat(): repeat a string N times
str_shuffle(): randomly shuffle strings

Guess you like

Origin blog.csdn.net/weixin_53549425/article/details/112911319