php basic syntax

丄偙de爷ル
Release: 2020-04-29 10:40:24
Original
47 people have browsed it

PHP basic syntax

1. Overview of php

1. What is phpPHP (Hypertext Preprocessor), Chinese translation is: hypertext preprocessing.

1. Hypertext: Better than text (.txt), the file suffix is ​​.php.

2. Preprocessing: The file needs to be processed in the server before being accessed by the browser. PHP is a general-purpose scripting language executed on the server side. PHP is the best programming language in the world.

PHP official website: http://www.php.net

2. What php can do 1. Web application development

3. Advantages of php

                                                                                                                                                                                                                                                                              since

          1. php file The default extension of php file is .php. PHP files usually contain HTML tags and some PHP script codes.​​​​​​​​ 2. PHP tags When parsing a PHP file, the PHP parser will look for the PHP start tag and end tag in the file, and parse the PHP code in the tags. Note: Anything outside the opening and closing tags will be ignored by the parser. 2.1 Standard style (recommended) 2.2 Short tag (not recommended) Note: php.ini needs to be configured with short_open_tag = On2.3 Hello World2.4 Omit the end tag If the php file is all php code from the beginning to the end of the page, you can omit the end of php Mark?>. This avoids accidentally adding spaces or newlines to the PHP closing tag, which would cause PHP to output these spaces. 3. PHP execution process 3.1 Compiled type and interpreted type (understanding) Compiled type: For c, since it only needs to be compiled once in the early stage, it will not be compiled again after compilation. It only needs to be executed and it will be ok, so the loss is: 1 time Compile 100 times and execute interpreted type: For PHP, it must be compiled and executed every time, so the loss is: Compile 100 times and execute 100 times. If the executable code is saved, it is a compiled language, and if the executable code is not saved, it is an interpreted language 3.2 PHP code parsing process PHP is an interpreted language. The PHP code is interpreted into opcode and then handed over to the Zend engine for execution. 4. PHP code embedded in HTML4.1 PHP code embedded in HTML tags PHP files can contain PHP code or HTML tags, so you can use PHP tags to embed PHP code into HTML tags. 4.2 PHP code embedded in HTML execution process The client sends a request to the server: 1. If the request is HTML, the server PHP will not parse the page content, and all the HTML code of the page will be sent to the client browser, and the browser will parse it again. 2. If the request is PHP, the server will execute PHP to parse the php code. After the execution is completed, it will generate standard HTML code, and then send the HTML code to the client php01/three.php5. Single-line comments in php: // Comments Content or # Comment content Multi-line comments: /* Comment content */ Note: Comments will enhance the readability of the code. Comments are divided into: file comments, variable comments, function comments, code block comments, etc. A good programmer must learn to comment on code. php01/comment.php6. Summary PHP is a scripting language embedded in HTML that is executed on the server. 3. Variables in PHP 1. Literal ·Literal is the data value used directly in the program. php01/literals.php2. Statement program is composed of a series of programming statements. Statements in PHP need to be ended with ;. php01/statement.php3, Variable 3.1 Variable defines the container used by variables to store data. That is to use *variables* to store data (direct quantities) or identify variables. They will be created when they are assigned a value for the first time. Use = to assign values ​​to variables. Variable rules in PHP: 1. Variables start with the $ symbol, followed by variables. 2. The variable name must start with a letter or underscore 3. The variable name cannot start with a number 4. The variable name can only contain alphanumeric characters and underscores (A-z, 0-9 and _) 5. The variable name is case-sensitive ( $name and $nameE are two different variables) Thinking: Where are the variables and their values ​​stored? Answer: In the memory, run the php script, and all the variables in the script will be parsed and processed by php and stored in the memory! Thinking : How are variables and their values ​​stored in memory? Answer: Space will be allocated in the memory to store variables and values ​​respectively. The structure is: Variable---->Value Address--->Value 3.2 Variable Naming Method 1. Try to use complete *English* names for variable names. Do not use Chinese Pinyin, please use Youdao Translation for words you don’t know 2. When defining variables, do not be greedy for brevity, but use descriptive names to define variables. If the variable name contains multiple words, you can use the following naming method: (1 ) Underline nomenclature, _ distinguishes multiple words in variable names (2) Camel case nomenclature, the first letter of the first word is lowercase, and the first letter of other words is uppercase php01/variable_naming.php3.3 The output echo of the variable can output a The above string or multiple variables can be output at the same time. Print can only output one string or one variable, and always returns 1var_dump. Output content (data or variables), data type, and data length. Note: echo is slightly faster than print because it does not Return any value. 3.4 Assignment of variables Assignment between variables: It is to pass one variable to another variable. 1. Assignment by value: It is an ordinary assignment, making a copy of the value of one variable and passing it to another variable. What is passed is the value of the variable. If one variable changes the value, the value of the other variable remains unchanged. php01/variable_assign.php2. Assignment by address: Use the & symbol to assign the value, and pass the address of the value of a variable to another variable. The two variables are used together to give the address of the same value. When one changes the other will change. php01/variable_assign.php3.4 Variable variables store the variable name in another variable.3.5 Destruction of variables 1. After the PHP script is parsed, all variables are destroyed by PHP's garbage collection mechanism and the memory is released. 2. Manual destruction, variables can be destroyed through unset(). php01/unset.php3. Garbage data. If a piece of data is not referenced by a variable, the PHP garbage collection mechanism will determine that the data is garbage data. What unset() destroys is the variable. After the variable is destroyed, the value of the variable is no longer referenced, and the garbage collection mechanism destroys the value. php01/unset.php4, php variable type PHP is a weakly typed language. When declaring a variable in PHP, you do not need to declare the variable type. PHP will automatically convert the variable to the correct data type based on its value. In strongly typed programming languages, such as C and C, we must declare (define) the type and name of the variable before using it. PHP supports 8 primitive data types: integer type, floating point type, string type, Boolean type, array type, object type, NULL type, resource type 1, scalar type 1.1 Integer type 1, integer type: integer or int2, including Positive integers, negative integers, 0. Value range: range -2 31 ~ 2 31 -1. Values ​​beyond this range will be treated as float floating point type. 3. The integer type occupies 4 bytes. 4. To determine whether the data or variable is an integer type, use is_int() . Return: true |falsephp0/integer.php1.2 Floating point type 1. Floating point type: float2. Floating point type is decimal type data. 3. The number of significant digits after the decimal of floating point type is 14 digits. 5. Determine whether the data or variable is of floating point type. Use is_float() to return: true |falsephp01/float.php1.3 String type 1. String: string2. Any character enclosed in single quotes or double quotes is a string. 3. Determine whether the data or variable is of string type. Use is_float() to return: true |falsephp01/string.php1.4 Boolean type 1. Boolean type: bool or boolen2. There are only two values ​​of Boolean type: true or false 4. Determine whether the data or variable is of bool type. Use is_bool() to return: true | false5. The bool type is generally used as a judgment condition to return the result. Use php01/bool.php2. Composite type 2.1 Array type 1. Array: array2. A set of data 3. print_() is specially used for printing Output array. 4. Determine whether the data or variable is of array type. Use is_array() to return: true |falsephp01/array.php2.2 Object type 1, object: object2, detailed explanation in later courses php01/object.php3, special type 3.1 NULL type 1, NULL type: NULL2, means none, the value is null3 , determine whether the data or variable is of NULL type. Using is_null() returns: true |falsephp01/null.php3.2 Resource type 1. Resource type: resource2. External data referenced by PHP will be processed as resource type 3. Resource type can only be obtained, not created 4. Resources also have categories For example, there are link resources and text flow resources. 5. Later course content will explain 4. PHP pseudo-type 4.1 Mixed type 1. Mixed type: mixed2. If the parameter type of a function is mixed type. Data indicating that parameters can be of many different types 4.2 Number type 1. Number type: number 2. If the parameter type of a function is number type, it means that the parameter can be integer type or float type 3. Detailed explanation later 4.3 callback type 1. callback type : callback2. callback represents a callback function. Under certain circumstances, the function automatically called by the program is called a callback function. 3. Detailed explanation later. 5. Get type function gettype() 5. Type conversion 1. Automatic conversion 1. In the PHP program, if data The type does not match the expected type, PHP will automatically convert the data type to the expected type. php01/trype_auto_conversion.php2. Forced conversion 2.1 Temporary forced conversion is to temporarily force the type of the variable to the required type. Format: (type)$variable (int) (bool) (float) (string) (array) (object) Note 1: Converting the type will affect the type of the original variable. 2.2 Permanent conversion uses php function settype()php01/settype.php3. Other types and bool type conversion. The only values ​​of bool type are: true (true) and false (false). In many cases, it is necessary to convert other types into bool types for conditional judgment. Integer type: 0 (false) Non-0 (true) string: empty string, '0' string is converted into bool and is false. Others are true arrays: empty arrays are converted to bool and false.Others are trueNULL: null is converted to bool and false is summarized in one sentence: non-0 non-empty non-null non-false is truephp01/other_type_conversion_bool.php4. Type conversion function Type conversion related functions: intval() strval() 6. Operator 1. Arithmetic operators - * / % 2. Assignment operator = = --= *= /= %= 3. Comparison operator > greater than >= greater than or equal to < less than <= less than or equal to == equal to (the values ​​are equal, Type is not judged) === Congruent (type and value must be equal)!= Not equal !== Not congruent 4. Logical operators &&(and) and ||(or) Or! Not (reverse) 5. Connector .. is a connection symbol in PHP. Connection: variables and variables, variables and strings, strings and strings 6. Ternary operator 6.1 Unary operator increment - decrement 6.2 Ternary operator $rs = expression? value 1: value 2; if expression The result is true. Return value 1, otherwise return value 2 7. Operator priority () > Arithmetic operator > Comparison operator > Logical operator > Assignment operator 7. Constant 1, Constant 1.1 Overview of constants 1. In the program If a piece of data rarely changes, the entire data can be declared as a constant. 2. Constants are generally declared at the beginning of the program. 1.2 Constant definition define("constant name", value); const constant name = value; the constant name is in capital letters, which is meaningful. php01/define.php1.3 constants use the constant name directly. php01/define.php1.4 Constant judgment uses defined() to judge whether the constant has been declared. Return: true or falsephp01/define.php2, predefined constant PHP_VERSION The currently used PHP version number PHP_OS The running operating system of the current PHP environment PHP_INT_MAX The maximum value of the integer value DIRECTORY_SEPARATOR The directory separator of the current system, Windows \ Linux /php01/sys_const .php3. The value of magic constants does not change, but the value of magic constants changes. __Magic constant name__php01/magic_const.php4, View all constants (understand) get_defined_constants()php01/get_defined_constants.php8, Super global variables Super global variables can be used and visible anywhere in the script. 1. $_SERVER Gets server and client related information. 2. $_GET , $_POST , $_FILES , $_COOKIE , $_SESSION Detailed explanation of later courses 9. Exercise 1, realize the exchange of two variable values ​​​​

The above is the detailed content of php basic syntax. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!