PHP basic questions and answers

Source: Internet
Author: User
The user's requests on the page communicate with the server asynchronously through the ajax engine. the server returns the request results to the ajax engine.

2. what is ajax? What is the principle of ajax? What is the core ajax technology? What are the advantages and disadvantages of ajax?
Ajax is the abbreviation of asynchronous javascript and xml,
Is a combination of javascript, xml, css, DOM, and other technologies.
'$' Is the alias of jQuery.

Users' requests on the page communicate with the server asynchronously through the ajax engine,
The server returns the request results to the ajax engine.
To display the returned data to the specified position on the page.
Ajax allows you to load all the output content of another page at the specified position of a page.
In this way, a static page can also obtain the returned data information in the database.
Therefore, ajax technology enables a static webpage to communicate with the server without refreshing the entire page,
Reduces user waiting time, reduces network traffic, and enhances user experience.
Degree.

Ajax has the following advantages:
1. this reduces the burden on the server, transfers some of the work previously borne by the server to the client for execution, and uses idle resources of the client for processing;
2. updating the page only when partial refreshing increases the page response speed, making the user experience more friendly.
The disadvantage of Ajax is that it is not conducive to seo promotion and optimization, because search engines cannot directly access the content of ajax requests.
The core technology of ajax is XMLHttpRequest, which is an object in javascript.

3. ** What is jQuery? What methods does jQuery use to simplify ajax?
JQuery is a Javascript framework.
JQuery's simplified ajax methods include:
$. Get (), $. post (), $. ajax (). $ Is the alias of the jQuery object.

The code is as follows:
Script
Var loadUrl = 'translate. php ';
Var input_val = $ ('# word'). val ();
// Method 1:
$. Post (loadUrl, {'word': input_val}, function (msg ){
$ ("# Result" cmd.html (msg );
});

// Method 2:
$. Get (loadUrl, {'word': input_val}, function (msg ){
$ ("# Result" cmd.html (msg );
});

// Method 3:
JQuery. ajax ({
Type: "post ",
Url: loadUrl,
Cache: false,
Data: "words =" + input_val,
Success: function (msg ){
$ ("# Result" cmd.html (msg );
}
});
Script

4. what is session control?
In short, session control is a mechanism for tracking and identifying user information.
The idea of session control is to be able to track a variable in the website. through this variable,
The system can identify the corresponding user information and obtain the user permissions based on the user information,
This shows the page content suitable for the user's corresponding permissions.
Currently, the most important method of session tracking is cookie and session.

5. *** what is the principle of Session?
When a user sends a session-enabled url request to the server,
The server will randomly assign a sessionid for this request because the session is enabled for this request.
At the same time, the specified sessionid and login information of the user are recorded in the specified path of the server.
(For example, user name and password ). The sessionid is also sent back to the client sending the request,
The sessionid is saved in a client cookie with a lifecycle of 0.
Therefore, the normal use of Sessions depends on the enabling of cookies.

Because cookies are saved in the client browser, in the IE browser,
If the browser's privacy is set to block all cookies, the cookies are disabled,
The session cannot be used normally.
If you want the session to work normally, you must set the browser privacy to high or below.

6. *** what is the concept of session and cookie? what is the difference between the two?
The cookie is saved on the client machine. for a cookie with no expiration time set, the cookie value is saved in the machine's memory. if the browser is disabled
The cookie disappears automatically. If the cookie expiration time is set, the browser saves the cookie as a text file to the hard disk,
When the browser is opened again, the cookie value is still valid.
Session stores the information to be stored on the server. The session information of each user is stored on the server like a key-value pair,
The key is sessionid, and the value is the information that the user needs to store. The server uses sessionid to distinguish the stored session information from
User.
Sessions play an important role in web development. It records the information correctly logged on to the server memory.
When you access the website management background, you do not need to log on again to get the identity confirmation. Users who do not log on correctly do not allocate session space, even if
The access address of the Management backend cannot see the page content. The user's operation permission on the page is determined through session.

Difference between Session and Cookie:
The cookie is saved on the client machine. for a cookie with no expiration time set,
The cookie value is stored in the memory of the machine. if the browser is disabled, the cookie disappears automatically.
If the cookie expiration time is set, the browser will take the cookie as a text file
Saved to the hard disk. the cookie value is valid when the browser is opened again.
Session stores the information to be stored on the server. Session information of each user
The key-value pair is stored on the server, where the key is sessionid and the value is the user.
Information needs to be stored. The server uses sessionid to identify which session information is stored.
User.
The biggest difference between Session and Cookie is that session is stored on the server,
The cookie is on the client. Session security is higher, while cookie security is weak.

7. ** What are the usage steps of session and cookie?
Steps for using session:
1. start the session:
Use the session_start () function to start. Session_start () cannot have any output, including empty rows.
2. registration session:
Directly add elements to the $ _ SESSION array.
3. use session:
Check whether the session is null or has been registered. If yes, use it as a normal array.
4. delete a session:
1. you can use unset to delete a single session;
2. use $ _ SESSION = array () to cancel all SESSION variables at a time;
3. use the session_destroy () function to completely destroy the session.

How to use cookies?
1. record part of user access information
2. pass variables between pages
3. store the internet pages viewed in the temporary cookies folder to improve browsing speed.
Create cookie:
Setcookie (string cookiename, string value, int expire );
Read cookie:
Use the Super Global Array $ _ COOKIE to read the cookie value of the browser.
Delete cookie: There are two methods
1. manual deletion:
Right-click the browser properties and you will be able to see the deletion of cookies. then, you can delete all cookie files.
2. setcookie () method:
The method for setting a cookie is the same as that for setting a cookie. However, the cookie value is set to null and the effective time is 0 or less than the current timestamp.

8. What are the three keywords $ this, self, and parent? In what scenarios?
$ This current object
Self current class
Parent class of the current parent class
$ This is used in the current class. use-> to call attributes and methods.
Self is also used in the current class, but you need to use: call.
Parent is used in the class.

9. What are the three major features of OOP?
1. encapsulation:
It is also called Information Hiding. it separates the use and implementation of a class, and only keeps some interfaces and methods in contact with the outside, or only discloses some methods for developers.
As a result, developers only need to pay attention to how this class is used, instead of concerned about its specific implementation process. in this way, the MVC division of labor can be achieved, and inter-program dependency can be effectively avoided,
Implement loose coupling between code modules.

2. Inheritance:
A subclass automatically inherits the attributes and methods of its parent class, and can add new attributes and methods or rewrite some attributes and methods. Inheritance increases code reusability.
Php only supports single inheritance, that is, a subclass can only have one parent class.

3. polymorphism:
Subclass inherits the attributes and methods from the parent class and overwrites some of them.
As a result, although all the sub-classes have the same method, the objects instantiated by these sub-classes call these same methods to obtain completely different results. this technology is polymorphism.
Polymorphism enhances software flexibility.

10. How often do magic methods trigger?
1) _ autoload (): When a program instantiates a class, the class is not introduced in the current file.
This will trigger the execution of _ autoload (). The program automatically introduces this class file through this method.
This method has a parameter, that is, the name of the class that you forgot to introduce.
_ How does the autoload () method work? When the program is executed to instantiate a class,
If this class file is not introduced before instantiation, The _ autoload () function is automatically executed.
This function searches for the path of the class file based on the name of the instantiated class,
After determining that this class file exists in the path of this class file,
Execute include or require to load the class, and then the program continues to execute,
If the file does not exist in this path, an error is prompted.
Using the automatically loaded magic function, you do not need to write many include or require functions.
2) _ construct (): This is a magic constructor.
The constructor is the method automatically executed when the object is instantiated. it is used to initialize the object.
This method can have no or multiple parameters. If there are parameters,
Remember to write the corresponding parameters for the new object. Before php5,
There is no magic constructor. a common constructor is constructed by a method with the same name as the class name.
If a class contains both Magic constructor and common constructor.
In php5 and later versions, the magic method works, while the common constructor does not.
On the contrary, in versions earlier than php5, you do not know the magic constructor,
Just use this method as a common method.
3) _ destruct (): This is a magic destructor.
The role of the destructor is the opposite of that of the constructor. it is automatically called when the object is destroyed,
The function is to release the memory. The destructor has no parameters.
4) _ call (): When the program calls a nonexistent or invisible member method,
Automatically trigger the execution of _ call (). It has two parameters,
They are the names and parameters of methods that are not accessed. The second parameter is of the array type.
5) _ get (): When the program calls an undefined or invisible member attribute,
Automatically trigger the execution of _ get (). It has a parameter that indicates the name of the property to be called.
6) _ set (): When the program tries to write a nonexistent or invisible member attribute,
PHP will automatically execute _ set (). It contains two parameters, indicating the property name and the property value.
7) _ tostring (): When the program uses echo or print to output an object,
This method is automatically called. This method is used to convert an object into a string and then output the object. __
Tostring () has no parameter, but this method must have a return value.
8) _ clone (): When the program clones an object, the _ clone () method can be triggered,
The program hopes to achieve this by Magic: not only simply cloning objects,
The cloned object also has all attributes and methods of the original object.

11. ** What are the main smarty configurations?
The purpose of the smarty template technology is to separate php from html,
Artists and programmers perform their respective duties and do not interfere with each other.
Smarty configuration information includes:
1. introduce Smarty. class. php;
2. instantiate the smarty object;
3. modify the default template path again;
4. modify the default path of the compiled file;
5. modify the path of the default configuration file again;
6. modify the default cache path again;
7. You can set whether to enable the cache;
8. you can set the delimiters on the left and right.
Advantage: fast access after Compilation

12. *** how to apply the concept of smarty?
Smarty is a template engine based on MVC ideas,
It divides a page program into two parts:
That is, the view layer and the control layer, that is, the smarty technology separates html and php code.
In this way, programmers and artists perform their respective duties and do not interfere with each other.

Usage of smarty:
1. configure smarty correctly.
It mainly needs to instantiate the smarty object and configure the path of the smarty template file;
2. use assign assignment and display on the php page;
3. php code segments cannot appear in the smarty template file,
All comments, variables, and functions must be included in the delimiters.
The Smarty template language mainly includes:
A. basic smarty syntax
B. The Smarty template calls various variables.
C. built-in functions of the Smarty Template: include, literal, foreach, if else, config_load
D. Application of Smarty cache technology
E. Smarty common variable regulators:
Default, {$ title | default :'??? '}
Date_format {$ smarty. now | data_format: '% Y-% m-% d '},
Truncate {$ title | truncate: 10}
Count_characters {$ title | count_characters}
Strip_tags {$ title | strip_tags}

13. ** What is the concept of MVC?
MVC (model-view-controller) is a software design pattern or programming idea.
M refers to the model layer, V refers to the view layer (display layer or user interface), and C refers to the control layer.
The purpose of using mvc is to implement M and V separation, so that a program can easily use different user interfaces.
The purpose of C is to adjust M and V to ensure synchronization between M and V. once M changes, V should be updated synchronously.
Separating M and V enables the same webpage to display different page styles when different festivals arrive. This requires creating multiple view layer template pages in advance,
Without changing the M-layer program.
MVC achieves division of labor and cooperation in programming, and the reusability of code is maximized,
The program logic is clearer and more organized to facilitate later maintenance and management.

14. What are the access permission modifiers?
Answer: 1. public indicates public. in this class, you can call
2. protected indicates protected, which can be called in this class and in the subclass.
3. private indicates private and can only be called in this class.
4. var, equivalent to public

15. where are the scope operators used?
Answer: It applies to the use of operators.
A) in this class:
I. self: Class constant
Ii. self: static attributes
Iii. self: Method () parent: Method ()
B) in the subclass:
I. parent: Class constant
Ii. parent: Static attribute (public or protected)
Iii. parent: Method () (public or protected)
C) out-of-class:
I. class name: Class constant
Ii. class name: static property (public)
Iii. class name: static method (public)

16. what do $ this, self, and parent represent? Where to use
A: $ this indicates the current object.
Self indicates the current class.
Parent indicates the parent class of the current class.
Usage:
$ This can only be used in the current class. you can use $ this-> to call the attributes and methods of the current class;
Self can only be used in the current class. the scope operator is used to access class constants in the current class, static attributes in the current class, and methods in the current class;
The parent can only be used in the current class with a parent class. it uses the scope operator: To access class constants in the parent class, static attributes in the parent class, and methods in the parent class.

17. What are the similarities and differences between interfaces and abstract classes?
1. interfaces help php implement multi-inheritance in the functional sense. interfaces are used to declare that the methods have no method body and implemens keywords are used to implement interfaces.
An interface can only contain abstract methods and class constants, but cannot contain member attributes.
2. an abstract class is a class that cannot be instantiated and can only be used as a parent class. it is defined by abstract class. abstract classes and common classes can have no difference, class can contain member attributes, class constants, and methods.
The subclass must be inherited by extends, and can only be a single inheritance.
The similarities between the two are that they cannot be instantiated and can be used only after being inherited.
The biggest difference between the two is that interfaces can implement multi-inheritance, while abstract classes can only be single-inheritance.
The interface cannot contain member attributes, but the abstract class can contain member attributes.
Abstract methods in an interface must be public or have no access modifier. abstract methods in an interface cannot be modified.
Methods in abstract classes can be common or abstract methods. abstract methods must be used for modification.

18. What are constructor methods and constructor methods?
The constructor is a member method that is automatically executed when an object is instantiated. the function is to initialize the object.
Before php5, a method identical to the class name is the constructor. after php5, the magic method _ construct () is the constructor.
If no constructor is defined in the class, php will automatically generate one. the automatically generated constructor does not have any parameters,
No operation.
The constructor format is as follows:
Function _ construct (){}
Or: function class name (){}
The constructor can have no or multiple parameters.

The function of the destructor is the opposite of that of the constructor. it is automatically called when the object is destroyed, and the function is to release the memory.
The method for defining the destructor is __destruct ();
Because php has a garbage collection mechanism, it can automatically clear objects that are no longer in use and release the memory. Generally, you can not manually create the destructor.

19. ** How does one explain the single-instance mode in PHP?
It is also called single-state mode, single-element mode, and singleton pattern.
Singleton mode refers to creating only one instance for the specified class within the scope of the PHP application.
Classes that use the Singleton mode are called Singleton classes.
You must have a private constructor for a single class in php,
There is also a private magic clone method (this method is empty)
And a private static member attribute $ _ instance.
Private constructor prevents classes other than itself from instantiating it. The private method body is empty to prevent the class from being cloned.
$ _ Instance is used to store objects instantiated by itself.
You must also have a public static method getInstance ().
This method must return $ _ instance.

20. * What is a single portal (single point Portal?
The so-called single portal means that the entire application has only one portal, and all implementations are forwarded through this portal,
For example, we use index. php as the single point of entry for the program,
Of course, the name of this file can be defined by yourself.

A single point of entry has several benefits:
1. some system Global variables can be defined here.
For example, if you want to preliminarily filter data, you need to simulate session processing,
You need to define some global variables, or even register some objects or variables in the register.
Second, the program architecture is clearer and clearer.

21. *** what is the paging principle?
Data paging requires the following conditions:
1. total number of items involved in the page $ msg_count, which can be obtained through database query;
2. the number of entries displayed on each page $ pagesize, which is defined by yourself;
3. the current page number $ page, which is passed and received through the address bar;
4. you can use the preceding data to calculate the total page number $ pagecount. here you need to use ceil ();
$ Pagecount = ceil ($ msg_count/$ pagesize );
5. database query uses limit in SQL statements to implement data changes:
For example:
Select * from table name where condition limit $ startnum, $ pagesize;
While $ startnum = ($ page-1) * $ pagesize;

22. *** what is the principle of Infinitus classification?
To implement Infinitus classification, the database table structure is the key.
At least three fields are required in the table structure. to avoid recursive loops, four fields are required.
1. id, the unique identifier of the current data;
2. typename, type name;
3. parentid: The parent id of the current type;
4. path, which stores the id of the current type and the id of all its parent types.
These IDs are separated.
5. You can use the following SQL statements to display the information in the same top-level classes in a centralized manner.
Select * from table name where condition order by path;

23. * common regular expression syntax:
Chinese:/^ [\ u4E00-\ u9FA5] + $/
Mobile phone number:/^ (86 )? 0? 1 \ d {10} $/
EMAIL:
/^ [\ W-] + [\ w-.]? @ [\ W-] + \. {1} [A-Za-z] {2, 5} $/
Password (in security level ):
/^ (\ D + [A-Za-z] \ w * | [A-Za-z] + \ d \ w *) $/
Password (high security level ):
/^ (\ D + [a-zA-Z ~! @ # $ % ^ & () {}] [\ W ~! @ # $ % ^ & () {}] * | [A-zA-Z ~! @ # $ % ^ & () {}] + \ D [\ w ~! @ # $ % ^ & () {}] *) $/

24. *** what is the Smarty Cache mechanism?
Smarty cache technology can cache the page that the user sees as a static HTML page.
If cache is enabled on the page, that is, the caching attribute of the smarty object is 1 or 2, during the cache_lifetime period set by the smarty object, users' WEB requests are directly forwarded to this static HTML file, which is like calling a static HTML page.
Apache accesses an html static Page 2-10 times faster than accessing a php page. The cache technology is introduced to improve page access performance by 25-100%.

Smarty features:
1. fast speed;
2. use the compilation method to generate a post-compilation file. If the page content does not change, you do not need to re-compile the modified template file;
3. built-in efficient cache technology. This greatly improves page access speed and reduces the burden on servers.
4. you can use the if... else logic in the template for judgment, and the page processing is flexible.
5. You can customize the plug-in. For example, you can use a custom plug-in to disable local caching.

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.