Some important PHP knowledge points to share in PHP

Source: Internet
Author: User
Tags autoload asymmetric encryption
1. Briefly describe the AutoLoad in PHP

When we use classes in PHP, we have to load them before we use them, either through require or include, but there are two problems that affect the decision we make to load. The first thing is that you don't know where the file is stored, and the other is not sure when you need to use it. Especially in the case of project files, it is not possible to write a long string of require for each file in the beginning section.
Autoload loading mechanism, when using new to instantiate a class, PHP will be defined by the Autoload function to load the corresponding file, if the class file uses extends or implements need to use other class files, PHP will rerun AutoLoad to find and load class files, if two times the same type of file request, will be an error.

2. What are the advantages and disadvantages of static variables?

Characteristics of static local variables: 1. Does not change with the invocation and exit of a function, although the variable continues to exist but cannot be used. If the function that defines it is called again, it can continue to be used, and the value left after the previous call is saved. 2. Static local variables are initialized only once. 3. A static property can only be initialized to a character value or a constant and cannot be used with an expression. Even if the local static variable is defined without an initial value, the system automatically assigns the initial values of 0 (to numeric variables) or null characters (to character variables), and the static variable is initially 0. 4. Consider using static local variables when you call a function multiple times and require the values of certain variables to be preserved between calls. Although this can be achieved with global variables, global variables sometimes cause unintended side effects, so it is advisable to use local static variables.

3. What is the difference between STRTR and str_replace, and what are the two separate scenarios?

The Str_replace () function replaces some characters in a string with other characters (case-sensitive)

The STRTR () function converts a specific character in a string.

Version 5.6 Str_replace is more efficient than STRTR, 7.0 version efficiency is basically the same, but 5.6 of str_replace is 3 times times higher than 7.0

4. Magic method

__construct (): The default constructor for a class, which, if __construct () and a method with the same name as the class, is called by default __construct () instead of the same name method.

__call (): The __call ($name, $arguments) method is called when a method that does not exist or is not accessible is called.

__tostring (): is called directly when the object is printed. such as Echo $object;

__clone (): Called directly when the object is copied.

__isset (): __isset () is called when isset () or empty () is used for a nonexistent or inaccessible property;

__destruct (): A destructor for a class that executes when all references to the object are deleted, or when an object is explicitly destroyed.

5. What results will be output as shown below?

foreach ($array as $key = + $item) {            $array [$key + 1] = $item + 2;            echo "$item";        }        Print_r ($array); Example: $array = [3,6,7,8];3678//echo The value of the element within the output array array (    [0] = 3  //$key remains constant     [1] = 5//each time $eky + 1, corresponding value plus 2,    [2] = 8    [3] = 9    [4] = 10)

Laravel

Advantages and disadvantages of laravel and other frame comparisons

Laravel

Advantages: The number of people in the world first, complete documentation, framework structure is clear, a large number of third-party expansion package for reference, suitable for large-scale Web site co-development, the development of artisan development tools to provide high efficiency. Composer extended auto-load, middleware

Cons: Slightly more complex, more slow to get started than the general frame; A lot of references to third-party packages, but in some scenarios we only use some of the methods in the class, the code seems redundant

thinkphp

Pros: A fast, easy-to-use MVC and object-oriented lightweight PHP development framework that adheres to the Apache2 Open source protocol, with simple and practical design principles, while maintaining excellent performance and minimalist code, with particular emphasis on development experience and usability, with numerous original features and features, Provides strong support for Web application development. Easy and quick, quick and easy to get started

Cons: Lack of object-oriented design, 5 version of the basic object-oriented, the framework of Community-related auxiliary tools less

Mysql

1. Differences between int (10) and int (11) in MySQL

BIT[M] Bit field type, M represents the number of bits per value, range from 1 to 64, if M is ignored, default is 1

TINYINT [(M)] [UNSIGNED] [Zerofill] M default is 4. A small integer. The signed range is 128 to 127. The unsigned range is 0 to 255.

Smallint[(M)] [UNSIGNED] [Zerofill] M default is 6. A small integer. The signed range is 32768 to 32767. The unsigned range is 0 to 65535.

Mediumint[(M)] [UNSIGNED] [Zerofill] M default is 9. A medium-sized integer. The signed range is 8388608 to 8388607. The unsigned range is 0 to 16777215.

Int[(M)] [UNSIGNED] [Zerofill] M default is 11. An integer of normal size. The signed range is 2147483648 to 2147483647. The unsigned range is 0 to 4294967295.

Bigint[(M)] [UNSIGNED] [Zerofill] M default is 20. A large integer. The signed range is 9223372036854775808 to 9223372036854775807. The unsigned range is 0 to 18446744073709551615.

Note: M here is not a specific length stored in the database, it was always mistaken for int (3) to store only 3-length numbers, and int (11) would store 11-length numbers, which is a big mistake. In fact, when we choose the type of int, whether it is int (3) or int (11), it is stored in the database is 4 bytes of length, when using int (3) If you enter 10, will default to you storage bit 010, This means that the 3 is the default length, and when you are less than 3, it will help you not all, when you have more than 3 bits, there is no effect.
**int (m) m indicates the maximum display width. The maximum effective display width is 255. The optional display width specifies that the width is filled from the left when the value that is less than the specified width of the column is displayed. The display width does not limit the range of values that can be saved within the column, nor does it limit the display of values that exceed the specified width of the column.

2. Index left principle:

Like, when matching a string, does not start with a wildcard character, the left must be fixed, and the field index will only work

Composite index, when the field on the left is fixed, the index on the right is valid when the index matches. Because of the compound index keyword sort, sort by the left field, if the left field is the same, it is based on the right field.

3. Advantages and disadvantages of index creation:

Advantages:

Creating indexes can greatly improve the performance of the system

Uniqueness Index guarantees the uniqueness of each row of data in a database table

Greatly speed up the retrieval speed

Accelerating the connection between a table and a table

Reduce the time to group and sort in queries when using grouping and sort clauses for data retrieval

By using an index, you can improve system performance by using an optimized hidden device during the query process

Disadvantages:

Creating indexes and maintaining indexes is time consuming and increases with the amount of data

Index occupies physical space

When the data in the table is changed, the index needs to be maintained dynamically, which reduces the maintenance speed of the data.

4. Please describe how the MySQL master-slave server synchronization between the data, what kind of SQL will cause the master and slave can not sync correctly?

Latency of the network

Since MySQL master-slave replication is based on a binlog asynchronous replication, transmission of Binlog files over the network, it is natural that network delay is the main reason for the majority of the synchronization, especially across the computer room data synchronization is very large, so do read and write separation, pay attention to the early design from the business layer.

Load inconsistency between master and slave machines

Since MySQL master-slave replication is the primary database that starts 1 IO threads, and starts 1 SQL threads and an IO thread from above, any one of the machines has a high load and is not busy, causing any one of these threads to be out of resources, and there will be a situation where the master is never consistent.

Max_allowed_packet setting inconsistencies

The main database set above the Max_allowed_packet than from the database, when a large SQL statement, can be executed on the main database above, set too small from the database, can not be executed, resulting in the Lord never consistent.

The key self-increment key starts with a value that is inconsistent with the self-increment step setting.

In the event of an abnormal MySQL outage, if sync_binlog=1 is not set or innodb_flush_log_at_trx_commit=1 is likely to occur binlog or relaylog file corruption, the master will never be consistent.

MySQL itself bugs cause the master to never sync.

Versions are inconsistent, especially if the higher version is primary, the lower version is from the case of the primary database above the supported features, which are not supported from the database above

5. There is an order form, as below, for the two users with the most purchase times

order_id

user_id

Goods

100000 100 apples

100001 100 Apples

100002 101 Oranges

100003 102 Apples

100004 102 Banana

Sql:

SELECT Order_id,user_id,count (order_id) as COUNT from order GROUP by user_id ORDER by COUNT DESC limit 2

Linux

How to find Web services slow

Top: Viewing system performance

Nginx: Last field added $request_time

List pages with PHP pages requesting more than 3 seconds, and count the number of times they appear, showing the top 100
Cat Access.log|awk ' ($NF > 1 && $7~/\.php/) {print $7} ' |sort-n|uniq-c|sort-nr|head-100

Implementation in code, start write time, end write time

Comprehensive

1. The difference between AES and RSA:

RSA is asymmetric encryption, public key encryption, private key decryption, and vice versa. Disadvantage: Slow running, not easy to implement hardware. Often the length of the private key is 512bit,1024bit,2048bit,4096bit, the longer the length, the more secure, but the slower the key generation, the more time it takes to decrypt.

AES symmetric encryption, the key is only 256 bit long, fast execution speed, easy to implement hardware. Due to symmetric encryption, the key needs to be learned by both parties before transmission.

AES encrypted block packet length must be 128 bits, the key length can be 128 bits, 192 bits, 256 bits of any one (if the data block and key length is insufficient, will be filled)

Summary: Using asymmetric encryption algorithm to manage the key of symmetric algorithm, and then encrypt the data with symmetric encryption algorithm, so we integrate the advantages of two kinds of encryption algorithm, not only realize the advantages of fast encryption, but also realize the advantages of security and convenient management key.

RBAC: Role-based access control

A user belongs to more than one role

The role has permissions for multiple actions

Whether the user has permissions for certain actions

Table: User table, Role table, action table, User Role Association table, Role Action Association table

Mongodb

MongoDB Data type:

String (String): The strings in MongoDB are UTF-8 valid.

Integer (integer): stores the value. The integer can be either 32-bit or 64-bit, depending on your server.

* Boolean: Stores the Boolean (True/false) value.

Double (doubles): stores floating-point values.

Min/max keys (min/MAX): Compares values to the lowest and highest bson elements.

Arrays (Array): Stores an array or list or multiple values into a single key.

Timestamp (timestamp): stores the timestamp.

Object: an embedded document.

Null (null value): Stores a null value.

Symbol: The same as a string for a language with a specific symbol type.

Date: Stores the current date or time in Unix time format.

Object ID (ID): stores the document ID.

Binary data (binary): stores binary data.

Code: Store JavaScript code in a document.

Regular expression (regular expression): Store Regular expression

Algorithm

1. Sorting algorithms

* Quick Sort

Fast sequencing is a very common high-efficiency algorithm, the idea is: I first select a ruler, use it to filter the entire queue, to ensure that its left element is not greater than it, its right element is not small with its function quickSort ($arr) {//Get array length

$length = count ($arr);  Determine if the length needs to continue the binary comparison  if ($length <= 1) {    return $arr;  }  Defines the datum element  $base = $arr [0];  Defines two empty arrays for storing and comparing results of the datum elements  $left = [];  $right = [];  Iterate array  for ($i =1; $i < $length; $i + +) {     //and Datum elements for comparison    if ($arr [$i] > $base) {      $right [] = $arr [$i] ;    } else {      $left [] = $arr [$i];    }  }  Then recursively handle left and right  $left = QuickSort ($left);  $right = QuickSort ($right);  Merge  return Array_merge ($left, [$base], $right);  }

Bubble sort

Idea: The law, like the name, bubbles up every time a maximum number is emitted from the array
For example: 2,4,1
First Time Out 4:2,1,4
The second time out 2:1,2,4

function Bubblesort ($arr) {  //Gets the array length  $length = count ($arr);  The first layer of loop control bubbles round  for ($i =0; $i < $length-1; $i + +) {         //inner loop control compares the No. 0 key value and the latter key value, each time a maximum number is emitted for    ($k =0; $k < $le ngth-$i; $k + +) {       if ($arr [$k] > $arr [$k +1]) {        $tmp = $arr [$k +1];        $arr [$k +1] = $arr [$k];        $arr [$k] = $tmp;      }    }  }  return $arr;}

Select sort

Idea: Select a corresponding element at a time and place it at the specified location

function selectsort ($arr) {//implementation ideas//double loop complete, outer control wheel count, current minimum, inner control comparison count//Get length $le   Ngth = count ($arr);     for ($i =0; $i < $length 1; $i + +) {//Assume the position of the minimum value $p = $i; Using the assumed minimum and other value comparisons, find the current minimum for ($j = $i +1; $j < $length; $j + +) {//$arr [$p] is the current minimum value known//comparison of the current loop value and the known minimum value. Record the next key when a smaller value is sent and make the next comparison if ($arr [$p] > $arr [$j]) {$p = $j;//Less than the assumed value}}//Key that finds the current minimum by internal for loop      and save in $p//To determine the key inconsistencies in the current $p in daylight and assume the minimum value of the key inconsistency increment it by swapping if ($p! = $i) {$tmp = $arr [$p];      $arr [$p] = $arr [$i];   $arr [$i] = $tmp; }}//Return the final result $arr;} 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.