Home  >  Article  >  Backend Development  >  Summary of php basic knowledge suitable for php beginners

Summary of php basic knowledge suitable for php beginners

WBOY
WBOYOriginal
2016-07-25 08:59:051250browse
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:
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