Document directory
- 12. how to configure, use, and clear the Smarty cache?
- 13. How does PHP establish a connection with mysql?
1. Differences between echo (), print (), and print_r ()
ECHO is a language structure and has no return value. The print function is basically the same as Echo. The difference is that print is a function and has a return value. print_r is a recursive print that is used to output array objects.
2. What is the difference between session and cookie in PHP?
Cookie is the information stored on the client. It is a mechanism for storing data in a remote browser and tracking and identifying users. The HTTP header of PHP sends cookies. Therefore, the setcookie () function must be called before other information is output, which is similar to the header () function.
Session is the information stored on the server. From this perspective, session is safer than cookie. When a session is created, the server returns an encrypted session_id from the client to identify the user. When the browser is closed, the session will be destroyed, and the value of the session will be lost.
3. What is the difference between include require in PHP?
These two structures are different in terms of processing failures. Include generates a warning, while require leads to a fatal error. In other words, if you want to stop processing the page when a file is lost, use require. This is not the case for include. The script will continue to be executed.
The use of require is as follows: require ("myfile. PHP); this table is usually placed at the top of the PHP program. Before PHP is executed, it will read the file specified by require to make it part of the PHP program webpage.
The use of include is the same as the preceding method. This program is generally placed in the Process Section of process control. Only when the PHP program webpage reads the include file can it be read, the process during program execution can be simplified.
When the page is executed to require (), if require is a PHP or HTML page, it will be immediately forwarded to the execution page. The include is generally used to include some inc files. For example, you can use the page header and header of your website as an inc file, so that you can include them in each PHP file. The include operation only embeds the file you want to include into the current page. And require is to immediately execute the page you requested.
---------------------------
Incluce loaded when used
Require load at the beginning
_ Once suffix indicates that the loaded files are not loaded.
The php system has a pseudo-compilation process when loading the php program, which can speed up the program running. However, the CE documentation is still interpreted.
An error occurred in the include file. The main program continues to execute.
The require file has an error and the main program has stopped.
Therefore, if the error of the contained file does not affect the system much (such as the interface file), use include. Otherwise, use require.
For the include_once () function, the require_once () function first checks whether the content of the target file has been imported before. If yes, the same content will not be imported again.
4. The difference between passing a value in PHP and transferring a reference. When will the value be passed for reference?
A: passing a value only transfers the value of a variable to another variable, and referencing the value indicates that the two points to the same place.
5. Use PHP to write the code that shows the Client IP address and Server IP address?
A: print the Client IP Address: Echo $ _ server ['remote _ ADDR ']; or: getenv ('remote _ ADDR'); // getenv gets the development environment variable
Print Server IP: Echo gethostbyname ("www.bolaiwu.com") // gethostbyname gets the IP address Function
6. What is the difference between get and post submission methods in a form?
A: Get is a request sent through the HTTP protocol to receive the request through URL parameter transmission, while post is an object data, and a large amount of information can be submitted through the form.
7. Use PHP to print the time format of the previous day in the format of 22:21:21 (2 points)
Answer: Echo date ('Y-m-d h: I: s', strtotime ('-1 days '));
8. Write the output results of the following program
<?
$ Str1 = NULL;
$ Str2 = false;
Echo $ str1 = $ str2? 'Equality ': 'unequal ';
// Equal
$ Str3 = ";
$ Str4 = 0;
Echo $ str3 = $ str4? 'Equality ': 'unequal ';
// Equal
$ Str5 = 0;
$ Str6 = '0 ′;
Echo $ str5 ===$ str6? 'Equality ': 'unequal ';
// Not equal
?>
9. Write the output results of the following program
<? $ Count = 5;
Function get_count (){
Static $ count = 0;
Return $ count ++;
}
Echo $ count; // 5
+ + $ Count;
Echo get_count (); // 0
Echo get_count (); // 1
?>
10. Write a function to traverse all files and subfolders in a folder.
A:
Function my_scandir ($ dir)
{
$ Files = array ();
If ($ handle = opendir ($ dir )){
While ($ file = readdir ($ handle ))! = False ){
If ($ file! = "..." & $ File! = ".") {
If (is_dir ($ dir. "/". $ file )){
$ Files [$ file] = scandir ($ dir. "/". $ file );
} Else {
$ Files [] = $ file;
}
}
}
Closedir ($ handle );
Return $ files;
}
}
11. Write a function to retrieve the file extension from a standard url as efficiently as possible.
Example: http://www.sina.com.cn/abc/de/fg.php? Id = 1 need to retrieve php or. php
Answer 1:
Function getExt ($ url ){
$ Arr = parse_url ($ url );
$ File = basename ($ arr ['path']);
$ Ext = explode (".", $ file );
Return $ ext [1];
}
Answer 2:
Function getExt ($ url ){
$ Url = basename ($ url );
$ Pos1 = strpos ($ url ,".");
$ Pos2 = strpos ($ url ,"?");
If (strstr ($ url ,"?")) {
Return substr ($ url, $ pos1 + 1, $ pos2-$ pos1-1 );
} Else {
Return substr ($ url, $ pos1 );
}
}
12. how to configure, use, and clear the Smarty cache?
1. Smarty cache Configuration
$ Smarty-> cache_dir = "/caches/"; // cache directory
$ Smarty-> caching = true; // when cache is enabled, the cache is invalid.
$ Smarty-> cache_lifetime = 60; // Cache Time
2. Use and clear the Smarty Cache
$ Smarty-> display ('cache. tpl ', cache_id); // create a cache with an ID
$ Smarty-> clear_all_cache (); // clear all caches
$ Smarty-> clear_cache('index.htm'); // clear the cache of index. tpl
$ Smarty-> clear_cache('index.htm', cache_id); // clear the cache of the specified id
13. How does PHP establish a connection with mysql?
Php. ini load mysql components:
Before extension = php_mysql.dll; remove
Is the extension_dir = "" path correct?
Link PHP to mysql Functions
Mysql_connect: Enable the MySQL Link
Mysql_select_db: open a database
@ And or die hide error and condition display
Mysql_connect ("host", "User Name", "password ")
Mysql_select_db ("Open Database", connection identifier );
If the connection identifier is not specifically declared, it is the last opened connection by default.
14. How to handle simple PHP errors
1. Set display_errors = on in PHP. ini to display the error location.
2. Echo or print
3. Use annotations to shield symbols for debugging
//..........
/*............ */
#..............
4. Check the error message.
15. Get the extension of a file path in at least five ways (for example, www.test.com/index.php, get PHP)
1st Methods
Function get_extension ($ file)
{
Substr (strrchr ($ file, '.'), 1 );
}
2nd methods:
Function get_extension ($ file)
{
Return substr ($ file, strrpos ($ file, '.') + 1 );
}
3rd methods:
Function get_extension ($ file)
{
Returnend (explode ('.',
$ File ));
}
4th methods:
Function get_extension ($ file)
{
$ Info = pathinfo ($ file );
Return $ info ['extension'];
}
5th methods:
Function get_extension ($ file)
{
Return pathinfo ($ file, pathinfo_extension );
}
The above methods seem to work, especially the methods 1 and 2. I don't knowpathinfo
We have been using the second parameter before. However, the first four methods have various problems. To obtain the file extension correctly, you must be able to handle the following three special cases.
1. No file extension
2. The path contains characters..
, Such/Home/test. d/test.txt
3. The path contains characters..
But the file does not have an extension. For example/Home/test. d/test
Obviously: 1 and 2 cannot handle the third case, and 3 cannot correctly handle the first three cases. 4. If the extension does not exist, a warning is issued. Only 5th methods are the most correct methods. Take a look.pathinfo
Method.
16. write out the functions used in PHP to complete the following functions.
A. Determine whether the value of A variable is null: Empty ($ bianliang)
B. Determine whether a variable is NULL: Null ($ bianliang)
C. Determine whether a variable exists: Isset ($ myvar)
D. Release a variable:
17. What functions do you use to capture remote images to a local device?
Socket & file;
18. Print the previous day in PHP format: 22:21:21
<? Php
Echo
Date ("Y-m-d H: I: s", time ()-60*60*24 );
Echo
Date ("Y-m-d H: I: s", strtotime ("-1 day "));
?>
19. PHP itself has a string flip function: strrev (), of course, you can also define a simple function to complete, the following method 1 and method 2 can only flip non-Chinese character strings, method 3: Flip the string.
<? Php
// Method 1: PHP system functions
Echo
Strrev ("ABCDEF"). "<br/> ";
// Method 2: User-defined functions. Chinese characters cannot be flipped.
Function
MyStrrev ($ str)
{
$ NewStr =
"";
For($ I = 0; $ I <strlen ($ str); $ I ++)
{
$ NewStr =
$ Str [$ I]. $ newStr;
}
Return
$ NewStr;
}
Echo
MyStrrev ("ABCDEF"). "<br/> ";
// Method 3: Flip Chinese Characters
$ NewcnStr = "";
$ CnStr =
Str_split ("I love you", 3 );
// Each Chinese Character occupies 3 bytes
// You can also use array_reverse () to flip and Output
For($ J = count ($ cnStr)-1; $ j> = 0; $ j --)
{
$ NewcnStr. = $ cnStr [$ j];
}
Echo
$ NewcnStr;
?>
20. Write a function to calculate the relative path of the two files.
For example, $ a = '/a/B/c/d/e. php ';
$ B = '/a/B/12/34/c. php ';
Calculate the relative path of $ B relative to $ a./../c/d to add ().
A: function getRelativePath ($ a, $ B ){
$ ReturnPath = array (dirname ($ B ));
$ ArrA = explode ('/', $ );
$ ArrB = explode ('/', $ returnPath [0]);
For ($ n = 1, $ len = count ($ arrB); $ n <$ len; $ n ++ ){
If ($ arrA [$ n]! = $ ArrB [$ n]) {
Break;
}
}
If ($ len-$ n> 0 ){
$ ReturnPath = array_merge ($ returnPath, array_fill (1, $ len-$ n ,'..'));
}
$ ReturnPath = array_merge ($ returnPath, array_slice ($ arrA, $ n ));
Return implode ('/', $ returnPath );
}
Echo getRelativePath ($ a, $ B );
21. directly exchange values of two existing variables without using new variables. (Test Basic php skills)
Answer: list ($ a, $ B) = array ($ B, $ );
22. The SQL query statement is as follows:
Select * from table where (ID = 10) or (ID = 32) or (ID = 22) or (ID = 76) or (ID = 13) or (ID = 44)
Let the results be retrieved in the order of 10, 32,. How can I write it? (SQL capability)
23. php simultaneously calls the information of one table in three databases (set up. a. aid = B. b. bid = C. c. cid). Describe your ideas and write some code.
24. Currently, I have an IM software. id and email are used as registration conditions. Assume that 1040 people have been registered, there is a zh_cn forum, and id and email are used as registration conditions. If 200 people have been registered, there is an en_us forum where id and email are used as registration conditions. Assume that you have registered 150 people.
Requirement: Synchronize the three, use the unified registration process, and write the design ideas