国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Home Backend Development PHP Tutorial Summary of php basic knowledge suitable for php beginners

Summary of php basic knowledge suitable for php beginners

Jul 25, 2016 am 08:59 AM

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



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

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

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.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What are some best practices for versioning a PHP-based API? What are some best practices for versioning a PHP-based API? Jun 14, 2025 am 12:27 AM

ToversionaPHP-basedAPIeffectively,useURL-basedversioningforclarityandeaseofrouting,separateversionedcodetoavoidconflicts,deprecateoldversionswithclearcommunication,andconsidercustomheadersonlywhennecessary.StartbyplacingtheversionintheURL(e.g.,/api/v

How do I implement authentication and authorization in PHP? How do I implement authentication and authorization in PHP? Jun 20, 2025 am 01:03 AM

TosecurelyhandleauthenticationandauthorizationinPHP,followthesesteps:1.Alwayshashpasswordswithpassword_hash()andverifyusingpassword_verify(),usepreparedstatementstopreventSQLinjection,andstoreuserdatain$_SESSIONafterlogin.2.Implementrole-basedaccessc

What are the differences between procedural and object-oriented programming paradigms in PHP? What are the differences between procedural and object-oriented programming paradigms in PHP? Jun 14, 2025 am 12:25 AM

Proceduralandobject-orientedprogramming(OOP)inPHPdiffersignificantlyinstructure,reusability,anddatahandling.1.Proceduralprogrammingusesfunctionsorganizedsequentially,suitableforsmallscripts.2.OOPorganizescodeintoclassesandobjects,modelingreal-worlden

What are weak references (WeakMap) in PHP, and when might they be useful? What are weak references (WeakMap) in PHP, and when might they be useful? Jun 14, 2025 am 12:25 AM

PHPdoesnothaveabuilt-inWeakMapbutoffersWeakReferenceforsimilarfunctionality.1.WeakReferenceallowsholdingreferenceswithoutpreventinggarbagecollection.2.Itisusefulforcaching,eventlisteners,andmetadatawithoutaffectingobjectlifecycles.3.YoucansimulateaWe

How can you handle file uploads securely in PHP? How can you handle file uploads securely in PHP? Jun 19, 2025 am 01:05 AM

To safely handle file uploads in PHP, the core is to verify file types, rename files, and restrict permissions. 1. Use finfo_file() to check the real MIME type, and only specific types such as image/jpeg are allowed; 2. Use uniqid() to generate random file names and store them in non-Web root directory; 3. Limit file size through php.ini and HTML forms, and set directory permissions to 0755; 4. Use ClamAV to scan malware to enhance security. These steps effectively prevent security vulnerabilities and ensure that the file upload process is safe and reliable.

How can you interact with NoSQL databases (e.g., MongoDB, Redis) from PHP? How can you interact with NoSQL databases (e.g., MongoDB, Redis) from PHP? Jun 19, 2025 am 01:07 AM

Yes, PHP can interact with NoSQL databases like MongoDB and Redis through specific extensions or libraries. First, use the MongoDBPHP driver (installed through PECL or Composer) to create client instances and operate databases and collections, supporting insertion, query, aggregation and other operations; second, use the Predis library or phpredis extension to connect to Redis, perform key-value settings and acquisitions, and recommend phpredis for high-performance scenarios, while Predis is convenient for rapid deployment; both are suitable for production environments and are well-documented.

What are the differences between == (loose comparison) and === (strict comparison) in PHP? What are the differences between == (loose comparison) and === (strict comparison) in PHP? Jun 19, 2025 am 01:07 AM

In PHP, the main difference between == and == is the strictness of type checking. ==Type conversion will be performed before comparison, for example, 5=="5" returns true, and ===Request that the value and type are the same before true will be returned, for example, 5==="5" returns false. In usage scenarios, === is more secure and should be used first, and == is only used when type conversion is required.

How do I perform arithmetic operations in PHP ( , -, *, /, %)? How do I perform arithmetic operations in PHP ( , -, *, /, %)? Jun 19, 2025 pm 05:13 PM

The methods of using basic mathematical operations in PHP are as follows: 1. Addition signs support integers and floating-point numbers, and can also be used for variables. String numbers will be automatically converted but not recommended to dependencies; 2. Subtraction signs use - signs, variables are the same, and type conversion is also applicable; 3. Multiplication signs use * signs, which are suitable for numbers and similar strings; 4. Division uses / signs, which need to avoid dividing by zero, and note that the result may be floating-point numbers; 5. Taking the modulus signs can be used to judge odd and even numbers, and when processing negative numbers, the remainder signs are consistent with the dividend. The key to using these operators correctly is to ensure that the data types are clear and the boundary situation is handled well.

See all articles