PHP development key points and skills Summary (1), php development key points and skills
- Opcache: Opcache comes from Zend Optimizer + renamed. It is mainly used to improve PHP performance by storing pre-compiled PHP script bytecode in the shared memory, the advantage of storing pre-compiled bytecode is that it saves the overhead of loading and parsing PHP scripts each time, but does not affect the I/O overhead, such as reading and writing disk files and reading and writing databases. Opcache is likely to replace APC, although it does not have the user cache function like APC. In addition, Opcache may conflict with similar components such as eaccelerator, xcache, and apc.
- PHP-FPM process pool: The master Process OF FastCGI process Manager is resident memory. Multiple processes are dynamically created and managed in the Process pool, which can effectively control the memory and processes and smoothly reload PHP configurations, in the event of an accident, you can restart and restore the damaged opcode. Refer to my PHP-FPM process pool exploration.
- Data Type: PHP supports nine types of raw data:
Four scalar types: 1. boolean (boolean) 2. integer (integer) 3. float (float type, also called double) 4. string (string) composite types: 1. array (array) 2. object 3. callable (callable) is the last two special types: 1. resource (resource) 2. NULL (no type)
- Lambda expressions (anonymous functions) and closures: Lambda expressions (anonymous functions) Implement an execution and pollution-free function definition. They are throwing functions and do not maintain any type of status. The closure adds Variable Interaction with the external environment based on the anonymous function.
use
The external environment variable to be imported in the clause.function getClosure($n){ $a = 100; return function($m) use ($n, &$a) { $a += $n + $m; echo $a."\n"; };}$fn = getClosure(1);$fn(1);//102$fn(2);//105$fn(3);//109echo $a;//Notice: Undefined variable
Class Dog {private $ _ name; protected $ _ color; public function _ construct ($ name, $ color) {$ this-> _ name = $ name; $ this-> _ color = $ color;} public function greet ($ greeting) {return function () use ($ greeting) {// the closure in the class can use the $ this variable to import the echo "$ greeting, I am a {$ this-> _ color} dog named {$ this-> _ name }. \ n ";};} in the static closure of the public function swim () {return static function () {// class, the object cannot be imported using the $ this variable, because you do not need to import the object into the closure,
// This saves a lot of memory, especially when there are many closures that do not require this function. Echo "Deming .... \ n ";};} private function privateMethod () {echo" You have accessed to {$ this-> _ name}'s privateMethod (). \ n ";}public function _ invoke () {// This method allows the object to be called as the closure echo" I am a dog! \ N ";}}$ dog = new Dog (" Rover "," red "); $ dog-> greet (" Hello ")(); $ dog-> swim (); $ dog (); // dynamically creates a closure using ReflectionClass and ReflectionMethod, and calls a non-public method directly. $ Class = new ReflectionClass ('dog '); $ closure = $ class-> getMethod ('privatemethod')-> getClosure ($ Dog); $ closure ();
- Single/double quotes, Heredoc, Nowdoc: Only single quotation marks (\ ') and backslash (\) need to be escaped in a single quotation mark string, and the rest are output as is; variables in double quotation marks string will be parsed; Heredoc structure is similar to double quotation mark string; nowdoc is similar to a single quotes string. The nowdoc structure and heredocs structure use the same tag<But the identifier following it must be enclosed in single quotes, that is<'Eot'
- String variable Parsing: It can be divided into $ resolution and {} resolution. $ Parsing is to parse the valid variables in $, and {} Parsing is to parse the variables in {}.
- SQL Injection risks:
1. risk of escaping the addslashes function: For URL parameter arg = % df \ 'after escaping through addslashes, arg = run' 2 in GBK encoding. urldecode function decoding risk: For URL parameter uid = 1% 2527 after calling the urldecode function decoding (secondary decoding), it will become uid = 1'
- Case-sensitive Conversion:
$str = preg_replace_callback( '/([a-z]*)([A-Z]*)/', function($matchs){ return strtoupper($matchs[1]).strtolower($matchs[2]); }, $str);
- Binary Security: The C string ends with an empty character ('\ 0'), which makes it impossible to store binary data such as images, audios, videos, and compressed files, the opposite is binary security. This concept is often mentioned in PHP. Here is a simple explanation. The following is the implementation of Redis simple dynamic string (SDS), which is binary secure:
// File path: src/sds. hstruct sdshdr {// record the number of bytes used in the buf array int len; // record the number of unused bytes in the buf array int free; // byte array, used to save the string char buf [];};
- /, %, And ** Operator: The operands of the modulo operator % are converted to Integers (excluding decimal digits) before the operation. The modulo operator%The result is the same as that of the divisor (plus or minus sign). ** it indicates multiplication.
5 / 3;//1.66666666666675.7 % 3;//25 % 3;//22 ** 3;//8
- Operator priority: The priority is reduced from top to bottom.
Integration direction |
Operator |
Additional information |
None |
Clone new |
Clone and new |
Left |
[ |
Array () |
Right |
** |
Arithmetic Operators |
Right |
++ -- ~ (Int) (Float) (String) (Array) (Object) (Bool) @ |
Type and increment/decrease |
None |
Instanceof |
Type |
Right |
! |
Logical operators |
Left |
* / % |
Arithmetic Operators |
Left |
+ - . |
Arithmetic Operators and string Operators |
Left |
< > |
Bitwise operators |
None |
< <= > > = |
Comparison Operators |
None |
= ! = === ! = <> <=> |
Comparison Operators |
Left |
& |
Bitwise operators and references |
Left |
^ |
Bitwise operators |
Left |
| |
Bitwise operators |
Left |
&& |
Logical operators |
Left |
| |
Logical operators |
Left |
?? |
Comparison Operators |
Left |
? : |
Ternary |
Right |
= + = -= * = ** = /= . = % = & = | = ^ = <= >>= |
Value assignment operator |
Left |
And |
Logical operators |
Left |
Xor |
Logical operators |
Left |
Or |
Logical operators |
- Unset () and NULL: Delete the reference and trigger the corresponding variable container refcount minus one. However, the behavior in the function varies depending on the type of the variable to be destroyed, for example, unset a global variable, only local variables are destroyed, while variables in the call environment (including the variables passed by function parameters) remain the same value before the unset call. The unset variable is different from the NULL value assigned to the variable, variable value NULL directly to the corresponding variable container refcount = 0
// Example 1: destroy the global variable $ foo in the function is invalid function destroy_foo () {global $ foo; unset ($ foo); echo $ foo; // Notice: undefined variable: foo} $ foo = 'bar'; destroy_foo (); echo $ foo; // bar // Example 2: unset a global variable in the function, use the $ GLOBALS array to implement function foo () {unset ($ GLOBALS ['bar']);} $ bar = "something"; foo (); echo $ bar; // Notice: Undefined variable: bar
- Pack () and unpack (): These two functions can be used as binary string encoding/decoding functions during socket programming.
$binarydata = pack("nvc*", 0x1234, 0x5678, 65, 66);//Pack data into binary string$array = unpack("c4chars/nint", $binarydata);//Unpack data from binary stringprint_r($array);//Array ( [chars1] => 19 [chars2] => 52 [chars3] => 24 [chars4] => 22 [int] => 16706 )
PHP7-Group Use usage:
// Proposed group use syntax: use FooLibrary\Bar\Baz\{ ClassA, ClassB, ClassC, ClassD as Fizbo }; // Compared to current use syntax: use FooLibrary\Bar\Baz\ClassA;use FooLibrary\Bar\Baz\ClassB;use FooLibrary\Bar\Baz\ClassC;use FooLibrary\Bar\Baz\ClassD as Fizbo;
- PHP7-NULL merge operator (??):
// Fetches the request parameter user and results in 'nobody' if it doesn't exist$username = $_GET['user'] ?? 'nobody';// equivalent to: $username = isset($_GET['user']) ? $_GET['user'] : 'nobody';
- PHP7-merge and compare operators (<=>):
Operator |
<=> Equivalent |
$a < $b |
($a <=> $b) === -1 |
$a <= $b |
($a <=> $b) === -1 || ($a <=> $b) === 0 |
$a == $b |
($a <=> $b) === 0 |
$a != $b |
($a <=> $b) !== 0 |
$a >= $b |
($a <=> $b) === 1 || ($a <=> $b) === 0 |
$a > $b |
($a <=> $b) === 1 |
- PHP7-user-layer random number generator: More secure and convenient
1. random_bytes(int length):Generates cryptographically secure pseudo-random bytes, such as when generating salts, keys or initialization vectors. 2. random_int(int min, int max):Generates cryptographically secure pseudo-random integers, such as when shuffling a deck of cards for a poker game.
$bytes = random_bytes(5);var_dump(bin2hex($bytes));//string(10) "385e33f741"var_dump(random_int(100, 999));//int(248)
PHP7-declare (strict_type = 1): Four scalar types, int, float, string, and bool, are added to PHP7. declare (strict_type = 1) will disable PHP from automatically converting the data type, PHP becomes a strong language. Declare (strict_type = 1) must be the first statement of the file. It only affects all function calls in the current file, and does not affect other files contained by the file (such as include.
- PHP7-captured Error: PHP7 implements a global throwable interface, which is implemented by both the Exception and some errors. More errors in PHP 7 are returned to the developer as caught exceptions. If no capture is performed, the Error is returned.