Summary of php basic knowledge suitable for php beginners
Some basic knowledge suitable for PHP beginners can also be regarded as some accumulation of experience. Friends in need can refer to it.
PHP Basics 1. First introduction to PHP PHP is an embedded language mixed with HTML. 1. PHP markup Default tag short tag ?>, you need to turn on the short_open_tag option in php.ini. The use of short tags and other tags is not recommended 2. Keywords are not case-sensitive. User-defined class names and function names are not case-sensitive. Variable names are case-sensitive 3. Output boolean print(parameter) returns a Boolean value void echo (parameter) Echo without return value is more efficient 2. Data type 1. The usual way to compare two floating point numbers is to first move a number of decimal places, then convert them to integer data and then compare them. 2. Strings with double quotes as delimiters support variable name parsing, while strings with single quotes as delimiters do not support variable name parsing. $name="Zhang San"; "$name"=>Zhang San|| '$name'=>$name || "Mr.$name"=>empty|| "Mr.{$name}"=>Mr. Zhang San|| "Mr.${name} "=>Mr. Zhang San 3. How to define strings: single quotes, double quotes and heredoc( 4. The object type must be explicitly declared. A class is defined with the keyword class, the keyword new is used to generate an instance of this class, and the -> symbol class is used to access the properties and methods in the class. class car{public $cololr;function beep(){}}$mycar = new car; $mycar->color='red'; $mycar->beep(); 5. PHP is a weak language type. The type of the variable will be determined by itself according to the assigned value, and the initial value of the variable is often assigned at the same time as the variable is declared. 6. When forcing data type conversion, just write the required type name in the brackets before the variable. 3. Constants and variables 1. Define constants define("constant name", expression or value) It is recommended that the constant name be in all uppercase letters, but it is not required. Use constants. Use the defined constant name directly without adding "$" in front of the constant name. Predefined constants: _FILE_Current PHP program file name _LINE_The line number of the current PHP program (where it refers) 2. Variables do not need to be explicitly declared. The variables are declared when assigning the initial value to the variable. If the variable has not been set to an initial value, its initial value is NULL. 3. Assignment of variables: assignment by value and assignment by reference. For example, $a=&$b; that is, b originally points to a storage location. After reference assignment, a also points to this storage location. At this time, the destruction of a or b will not have any impact on the other, but if one of them If the value of one changes, the other one will also make the same change. 4. Use super global variables to access predefined variables, a total of 9 super global variables 5. Local variables: variables defined within a function can only be used within the function Global variables: variables defined outside the function. By default, they can only be used outside the function. To use global variables within a function, you need to declare the variable as global within the function, or use the superglobal variable array &GLOBALS["variable name"] In PHP, only functions can provide local scope. The superglobal variable $GLOBALS contains all variables Characteristics of static variables: they are only initialized when called for the first time, are not destroyed after the function ends, and the variable retains its original value the next time it is used. Static variables can only be defined and used within functions. Mutable variable: Use the name of the variable as a variable. $a=b;$b=c;$$a=$b=c; External variables: The maximum data that can be transferred using GET is 256 bytes, and the maximum data that can be transferred using POST is 2MB 4. Process control (only different from other languages such as java) 1. Interactive format (colon syntax) is not recommended, the classic one is more intuitive 2. foreach(): This syntax is specially designed for arrays The first format foreach(target_array as $value) statement The second format foreach(target_array as $key=>$value) statement 3. break number: the number of layers of the structure to be jumped out of contiue number: the number of layers of the structure to be jumped out 4. The exit statement can end the execution of the entire current script and is usually used for error checking. exit; exit("Error reason"); die() is an alias for exit $conn=mysql_connect("localhost","root","") or die("Unable to connect to MySQL server"); 5. Array 1. The only difference between an associative array and a numeric index array is the type of index. 2. Numeric index array Initialization: Directly assign values to array elements with the array() function If the array does not exist, the array can be created while assigning values to the array elements. If the array elements are numbers in order, you can use the range() function when initializing the array range() has 3 parameters. The first parameter specifies the initial value, the second parameter specifies the end value, and the third parameter is optional and is used to specify the step size 3. Associative array Initialization: Directly assign array() function to array elements 4. Operators related to arrays + Union $a+$b appends $ after $a, but any elements with conflicting index values will not be added == equals $a==$b Returns true if $a and $b contain the same elements (both index values and elements must be the same) except for the order, everything else must be exactly the same != is not equal to === Identity Returns true if $a and $b contain the same elements in the same order (the index values and elements must be the same) and must be exactly the same !== Not equal 5. Sorting of arrays boolean sort() sorts in numerical and alphabetical order. After sorting, a new index value will be assigned, and the original index value will be deleted. void asort() sorts the array in ascending order and retains the original index relationship after sorting. integer ksort() sorts by index value in ascending order usort (array, method name) sorts by user-defined method array_multisort() sorts multiple arrays at once natsort() sorts in natural order and retains the original index relationship after sorting natcasesort() natural sorting, case-insensitive 6. Reverse sorting of arrays rsort() sorts array elements in descending order arsort() krsort() 7. Reorder the array boolean shuffle() randomly arranges the array array array_reverse() reverses the elements in the array array array_flip() Converts the index in the array to its element value 8. Array traversal current() gets the element value pointed to by the current pointer in the array next() moves the pointer of the array backward one bit and returns the element value of the element pointed to by the moved pointer. prev() moves the array pointer forward one bit and returns the element value of the element pointed to by the moved pointer. reset() sets the pointer back to the starting position of the array end() moves the pointer to the last element of the array each() returns the "index/element value" pair pointed to by the current pointer in the array, and moves the array pointer backward one bit Returns an array containing 4 elements, and the indexes of the array are 0, key, 1, and value. key() returns the index value pointed to by the current pointer in the array array_walk() processes each element in the array in the same way array_reduce() applies a custom function to each element of an array in turn 9. Other array operation functions list() extracts multiple values from an array at once and assigns them to multiple variables at the same time count()/sizeof() calculates the number of elements in the array 1 2 3 4 Next page Last page |

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











There are four main error types in PHP: 1.Notice: the slightest, will not interrupt the program, such as accessing undefined variables; 2. Warning: serious than Notice, will not terminate the program, such as containing no files; 3. FatalError: the most serious, will terminate the program, such as calling no function; 4. ParseError: syntax error, will prevent the program from being executed, such as forgetting to add the end tag.

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

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 is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

HTTP request methods include GET, POST, PUT and DELETE, which are used to obtain, submit, update and delete resources respectively. 1. The GET method is used to obtain resources and is suitable for read operations. 2. The POST method is used to submit data and is often used to create new resources. 3. The PUT method is used to update resources and is suitable for complete updates. 4. The DELETE method is used to delete resources and is suitable for deletion operations.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP handles file uploads through the $\_FILES variable. The methods to ensure security include: 1. Check upload errors, 2. Verify file type and size, 3. Prevent file overwriting, 4. Move files to a permanent storage location.

In PHPOOP, self:: refers to the current class, parent:: refers to the parent class, static:: is used for late static binding. 1.self:: is used for static method and constant calls, but does not support late static binding. 2.parent:: is used for subclasses to call parent class methods, and private methods cannot be accessed. 3.static:: supports late static binding, suitable for inheritance and polymorphism, but may affect the readability of the code.
