Home > Article > Backend Development > Summary of common PHP knowledge points
PHP:
1. The difference between echo print and print_r
Echo is a PHP statement, print and print_r are functions. Statements have no return value, but functions can have return values.
Print can only print the value of simple type variables.
Print_r can print complex variable values (arrays or objects)
Echo outputs one or more strings.
2. The difference between mysql_fetch_array() and mysql_fetch_row()
mysql_fetch_array() returns an array generated based on the rows obtained from the result set. If there are no more rows, it returns false. In addition to indexing the data with fields, you can also use fields name as index.
mysql_fetch_row() returns a row from the result set as an enumeration array, returning a numerically indexed array with offset starting from 0.
mysql_fetch_array() is an extended version of mysql_fetch_row().
3. The role of _set() and _construct in PHP object-oriented
_set() ——- is used to set values for attributes, _get() gets the value of attributes
_construct ——- can only be declared in a class A constructor can only call the constructor once every time an object is created. This method cannot be called actively, so it is usually used to perform some useful initialization tasks.
4. The difference between session and cookie in PHP
Cookie is information saved on the client. It is a mechanism that stores data in a remote browser and uses it to track and identify users. PHP's http protocol sends cookies in the header information, so the setcookie() function must be called before other information is output, similar to the restrictions of the header() function.
Session is information stored on the server side. From this perspective, session is more secure than cookie. When a session is created, the server returns an encrypted session_id to the client to identify the user. When the browser is closed, the session will be destroyed, and the value stored in the session will be gone.
5. How to set a cookie and specify the valid time
Bool setcookie(string name, string value, int expire, string path, string domain, bool secure, bool httponly)
Name: cookie variable name
Value: cookie variable value
Expire: Time when the validity period ends
Path: Valid directory
Domain: Valid domain name, top-level or unique
Secure: If the value is 1, the cookie can only be valid on https connections, if it is the default value 0, both http and https can be used
Php set cookie
$value = 'something from somewhere';
setcookie("TestCookie", $value); /* Simple cookie setting*/
setcookie("TestCookie", $value, time()+3600); /* Valid for 1 hour*/
setcookie(“TestCookie”, $value, time()+3600, “/~rasmus/”, “.example.com”, 1); /* Valid directory/~rasmus, valid Domain name example.com and all its subdomains */
?>
Use header() to set cookies;
header(“Set-Cookie: name=$value[;path=$path[;domain=xxx.com[; ]]”); The parameters following
are the same as the parameters of the setcookie function listed above.
Cookie mechanism principle:
a) The server sets a cookie in the client by sending an http Set-Cookie header with the response. (Multiple cookies require multiple headers).
b) The client automatically sends an http cookie header to the server, and the server receives and reads it.
HTTP/1.x 200 OK
X-Powered-By: PHP/5.2.1
Set-Cookie: TestCookie=something from somewhere; path=/
Expires: Thu, 19 Nov 2007 18:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0 , pre-check=0
Pragma: no-cache
Content-type: text/html
This line implements the cookie function. After receiving this line
Set-Cookie: TestCookie=something from somewhere; path=/
Browse The server will create a cookie file on the client's disk and write in it:
TestCookie=something from somewhere;
/
This line is where we use setcookie('TestCookie','something from somewhere','/'); The result. That is, the result of using header('Set-Cookie: TestCookie=something from somewhere; path=/');.
6. Access control in PHP object-oriented
Public means global, and both internal and external subclasses of the class can Access
Private means private, only within this class can be called
Protected means protected, only this class or subclass or parent class can access
7. What is MVC in PHP, the role and principle of MVC
MVC is a design Pattern, which forces the application's input, processing, and output to be separated. Using MVC, the application is divided into three core parts: model, view, and controller, each of which handles its own tasks.
The principle of MVC: First, the controller accepts the user's request and decides which model should be called for processing. Then the model uses business logic to process the user's request and return the data. Then the controller uses the corresponding view to format the returned data from the model. data and presented to the user through the presentation layer.
Basic principle: The request from the presentation layer (V) is sent to the controller (C), and the controller calls the business layer (M) according to the request type, and finally calls the presentation layer to display.
8. The difference between include and require in PHP
The two structures are different besides handling failure. include produces a warning, while require results in a fatal error. In other words, if you want to stop when encountering a missing file Use require when processing pages. This is not the case with Include, and the script will continue to execute.
Require is used like require("Myfile.php"); This form is usually placed at the front of the PHP program. Before PHP is executed, the file specified by require will be read in, making it a part of the PHP program web page. part.
Include is used in the same way as above. This program is usually placed in the processing section of the process control. The PHP program web page reads the include file when it reads it. This method can simplify the process of program execution.
When the page is executed to require(), if require is a PHP or HTML page, it will be immediately transferred to execute the page. And include is generally used to include some inc files. For example, you can use the header and header of your website as an inc file, and then include it in each PHP file. include actually just embeds the file you want to include into the current page. And require is to execute the page you requested immediately.
——————————————————————————–
include is loaded when used
require is loaded at the beginning
_once suffix indicates that it has not been loaded Loading
php system has a pseudo-compilation process when loading php programs, which can make the program run faster. But the include document still explains the execution.
There is an error in the include file, and the main program continues to execute.
There is an error in the require file, and the main program is also stopped.
So if the error in the included file has little impact on the system (such as the interface file ), use include, otherwise use the require
include_once() function. The require_once() function will first check whether the content of the target file has been imported before. If so, the same content will not be imported again
10. Calculate the difference between two times: 2009.5.12 2009.5.20
$regist1 = “05/12/2006″;
$regist2 = “10/05/2007″;
list($month1,$day1,$year1)= explode ("/",$regist1);
list($month2,$day2,$year2)= explode("/",$regist2);
$regist1 = mktime(0,0,0,$month1,$day1, $year1);
$regist2 = mktime(0,0,0,$month2,$day2,$year2);
$time_difference = $regist2-$regist1;
11. What protocols do you know? What does the HTTP protocol error message mean?
SMTP (Simple Mail Transfer Protocol) is called the Simple Mail Transfer Protocol, and its goal is to provide users with efficient and reliable mail transmission.
POP's full name is Post Office Protocol, which is the post office protocol, used for receiving emails. It uses TCP port 110. The third version is commonly used now, so it is called POP3 for short.
IMAP is the abbreviation of Internet Message Access Protocol, as the name suggests. , mainly provides a protocol for obtaining information through the Internet.
HTTP (HyperTextTransferProtocol) is the abbreviation of Hypertext Transfer Protocol. It is used to transmit data in WWW mode. For details on the HTTP protocol, please refer to RFC2616.
IE prompt HTTP 403 – Forbidden
IE prompt HTTP 403.9 – Forbidden: Too many connected users
IE prompt HTTP 404 – File cannot be found
IE prompt HTTP 500 – Internal server error