search

php basic syntax

Apr 28, 2020 pm 02:45 PM
php

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 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!

Statement
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
How does PHP type hinting work, including scalar types, return types, union types, and nullable types?How does PHP type hinting work, including scalar types, return types, union types, and nullable types?Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

How does PHP handle object cloning (clone keyword) and the __clone magic method?How does PHP handle object cloning (clone keyword) and the __clone magic method?Apr 17, 2025 am 12:24 AM

In PHP, use the clone keyword to create a copy of the object and customize the cloning behavior through the \_\_clone magic method. 1. Use the clone keyword to make a shallow copy, cloning the object's properties but not the object's properties. 2. The \_\_clone method can deeply copy nested objects to avoid shallow copying problems. 3. Pay attention to avoid circular references and performance problems in cloning, and optimize cloning operations to improve efficiency.

PHP vs. Python: Use Cases and ApplicationsPHP vs. Python: Use Cases and ApplicationsApr 17, 2025 am 12:23 AM

PHP is suitable for web development and content management systems, and Python is suitable for data science, machine learning and automation scripts. 1.PHP performs well in building fast and scalable websites and applications and is commonly used in CMS such as WordPress. 2. Python has performed outstandingly in the fields of data science and machine learning, with rich libraries such as NumPy and TensorFlow.

Describe different HTTP caching headers (e.g., Cache-Control, ETag, Last-Modified).Describe different HTTP caching headers (e.g., Cache-Control, ETag, Last-Modified).Apr 17, 2025 am 12:22 AM

Key players in HTTP cache headers include Cache-Control, ETag, and Last-Modified. 1.Cache-Control is used to control caching policies. Example: Cache-Control:max-age=3600,public. 2. ETag verifies resource changes through unique identifiers, example: ETag: "686897696a7c876b7e". 3.Last-Modified indicates the resource's last modification time, example: Last-Modified:Wed,21Oct201507:28:00GMT.

Explain secure password hashing in PHP (e.g., password_hash, password_verify). Why not use MD5 or SHA1?Explain secure password hashing in PHP (e.g., password_hash, password_verify). Why not use MD5 or SHA1?Apr 17, 2025 am 12:06 AM

In PHP, password_hash and password_verify functions should be used to implement secure password hashing, and MD5 or SHA1 should not be used. 1) password_hash generates a hash containing salt values ​​to enhance security. 2) Password_verify verify password and ensure security by comparing hash values. 3) MD5 and SHA1 are vulnerable and lack salt values, and are not suitable for modern password security.

PHP: An Introduction to the Server-Side Scripting LanguagePHP: An Introduction to the Server-Side Scripting LanguageApr 16, 2025 am 12:18 AM

PHP is a server-side scripting language used for dynamic web development and server-side applications. 1.PHP is an interpreted language that does not require compilation and is suitable for rapid development. 2. PHP code is embedded in HTML, making it easy to develop web pages. 3. PHP processes server-side logic, generates HTML output, and supports user interaction and data processing. 4. PHP can interact with the database, process form submission, and execute server-side tasks.

PHP and the Web: Exploring its Long-Term ImpactPHP and the Web: Exploring its Long-Term ImpactApr 16, 2025 am 12:17 AM

PHP has shaped the network over the past few decades and will continue to play an important role in web development. 1) PHP originated in 1994 and has become the first choice for developers due to its ease of use and seamless integration with MySQL. 2) Its core functions include generating dynamic content and integrating with the database, allowing the website to be updated in real time and displayed in personalized manner. 3) The wide application and ecosystem of PHP have driven its long-term impact, but it also faces version updates and security challenges. 4) Performance improvements in recent years, such as the release of PHP7, enable it to compete with modern languages. 5) In the future, PHP needs to deal with new challenges such as containerization and microservices, but its flexibility and active community make it adaptable.

Why Use PHP? Advantages and Benefits ExplainedWhy Use PHP? Advantages and Benefits ExplainedApr 16, 2025 am 12:16 AM

The core benefits of PHP include ease of learning, strong web development support, rich libraries and frameworks, high performance and scalability, cross-platform compatibility, and cost-effectiveness. 1) Easy to learn and use, suitable for beginners; 2) Good integration with web servers and supports multiple databases; 3) Have powerful frameworks such as Laravel; 4) High performance can be achieved through optimization; 5) Support multiple operating systems; 6) Open source to reduce development costs.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version