Php Interview Question 3

Source: Internet
Author: User
Php interview question 3 I. Basic question 1. write the output results of the following program
$ Str1 = null;
$ Str2 = false;
Echo $ str1 = $ str2? 'Equality ': 'Unequal ';

$ Str3 = ";
$ Str4 = 0;
Echo $ str3 = $ str4? 'Equality ': 'Unequal ';

$ Str5 = 0;
$ Str6 = '0 ′;
Echo $ str5 ===$ str6? 'Equality ': 'Unequal ';
?>
2. write the output results of the following program
$ A1 = null;
$ A2 = false;
$ A3 = 0;
$ A4 = ";
$ A5 = '0 ′;
$ A6 = 'null ';
$ A7 = array ();
$ A8 = array ());

Echo empty ($ a1 )? 'True': 'false ';
Echo empty ($ a2 )? 'True': 'false ';
Echo empty ($ a3 )? 'True': 'false ';
Echo empty ($ a4 )? 'True': 'false ';
Echo empty ($ a5 )? 'True': 'false ';
Echo empty ($ a6 )? 'True': 'false ';
Echo empty ($ a7 )? 'True': 'false ';
Echo empty ($ a8 )? 'True': 'false ';
?>

3. write the output results of the following program
$ Test = 'aaaaa ';
$ Abc = & $ test;
Unset ($ test );

Echo $ abc;
?>

4. write the output results of the following program
Function get_count (){
Static $ count = 0;
Return $ count ++;
}

Echo $ count;
+ + $ Count;
Echo get_count ();
Echo get_count ();
?>

5. write the output results of the following program
$ GLOBALS ['var1'] = 5;
$ Var2 = 1;
Function get_value (){
Global $ var2;
$ Var1 = 0;
Return $ var2 ++;
}
Get_value ();

Echo $ var1;
Echo $ var2;
?>

6. write the output results of the following program
Function get_arr ($ arr ){
Unset ($ arr [0]);
}
$ Arr1 = array (1, 2 );
$ Arr2 = array (1, 2 );

Get_arr (& $ arr1 );
Get_arr ($ arr2 );

Echo count ($ arr1 );
Echo count ($ arr2 );
?>
7. get the extension of a file in more than five ways

Requirement: dir/upload.image.jpg to find. jpg or. jpg,
The PHP built-in handler must be used for processing. the methods cannot be repeated and can be encapsulated into functions, such as get_ext1 ($ file_name) and get_ext2 ($ file_name)

II. algorithm questions
1. use PHP to describe the bubble sort and quick sort algorithms. the object can be an array.

2. using PHP to describe sequential search and binary search (also called semi-query) algorithms, sequential search must take efficiency into account. the object can be an ordered array.

3. write a two-dimensional array sorting algorithm function that is universal and can call php built-in functions.
[Attached answer] (the answer below is not necessarily the best, but a simple reference)

I. basic questions

1. equal and unequal
2. true false
3. aaaaaa
4. 5 0 1
5. 5 2
6. 1 2
7. get the extension of a file in more than five ways

Function get_ext1 ($ file_name ){
Return strrchr ($ file_name ,'.');
}
Function get_ext2 ($ file_name ){
Return substr ($ file_name, strrpos ($ file_name ,'.'));
}
Function get_ext3 ($ file_name ){
Return array_pop (explode ('.', $ file_name ));
}
Function get_ext4 ($ file_name ){
$ P = pathinfo ($ file_name );
Return $ p ['extension'];
}
Function get_ext5 ($ file_name ){
Return strrev (substr (strrev ($ file_name), 0, strpos (strrev ($ file_name ),'.')));
}

II. algorithm questions

1. use PHP to describe the bubble sort and quick sort algorithms. the object can be an array.

// Bubble sort (array sorting)
Function bubble_sort ($ array)
{
$ Count = count ($ array );
If ($ count <= 0) return false;

For ($ I = 0; $ I <$ count; $ I ++ ){
For ($ j = $ count-1; $ j> $ I; $ j ?) {
If ($ array [$ j] <$ array [$ j-1]) {
$ Tmp = $ array [$ j];
$ Array [$ j] = $ array [$ j-1];
$ Array [$ j-1] = $ tmp;
}
}
}
Return $ array;
}

// Fast sorting (array sorting)
Function quick_sort ($ array ){
If (count ($ array) <= 1) return $ array;

$ Key = $ array [0];
$ Left_arr = array ();
$ Right_arr = array ();

For ($ I = 1; $ I If ($ array [$ I] <= $ key)
$ Left_arr [] = $ array [$ I];
Else
$ Right_arr [] = $ array [$ I];
}

$ Left_arr = quick_sort ($ left_arr );
$ Right_arr = quick_sort ($ right_arr );

Return array_merge ($ left_arr, array ($ key), $ right_arr );
}
2. using PHP to describe sequential search and binary search (also called semi-query) algorithms, sequential search must take efficiency into account. the object can be an ordered array.

// Binary search (find an element in the array)
Function bin_sch ($ array, $ low, $ high, $ k ){
If ($ low <= $ high ){
$ Mid = intval ($ low + $ high)/2 );
If ($ array [$ mid] === k ){
Return $ mid;
} Elseif ($ k <$ array [$ mid]) {
Return bin_sch ($ array, $ low, $ mid-1, $ k );
} Else {
Return bin_sch ($ array, $ mid + 1, $ high, $ k );
}
}
Return-1;
}

// Sequential search (find an element in the array)
Function seq_sch ($ array, $ n, $ k ){
$ Array [$ n] = $ k;
For ($ I = 0; $ I <$ n; $ I ++ ){
If ($ array [$ I] === k ){
Break;
}
}
If ($ I <$ n ){
Return $ I;
} Else {
Return-1;
}
}
3. write a two-dimensional array sorting algorithm function that is universal and can call php built-in functions.

// Two-dimensional array sorting. $ arr indicates data, $ keys indicates the key value of sorting, $ order indicates sorting rules, 1 indicates ascending, and 0 indicates descending.
Function array_sort ($ arr, $ keys, $ order = 0 ){
If (! Is_array ($ arr )){
Return false;
}
$ Keysvalue = array ();
Foreach ($ arr as $ key => $ val ){
$ Keysvalue [$ key] = $ val [$ keys];
}
If ($ order = 0 ){
Asort ($ keysvalue );
} Else {
Arsort ($ keysvalue );
}
Reset ($ keysvalue );
Foreach ($ keysvalue as $ key => $ vals ){
$ Keysort [$ key] = $ key;
}
$ New_array = array ();
Foreach ($ keysort as $ key => $ val ){
$ New_array [$ key] = $ arr [$ val];
}
Return $ new_array;
}

A php interview question (with answers)

The test is very basic, but the foundation is not strong, and a batch of tests will not be done. For example, in HTTP 1.0, the meaning of the status code 401 is unknown due to error_reporting (2047. But it seems a little abnormal to use 2047, which is too difficult to read.

If you can refer to the manual ~~~~

* 1. in PHP, the name of the current script (excluding the path and query string) is recorded in the predefined variable (1), and the URL linked to the current page is recorded in the predefined variable (2).
2. Execution section Output (3 ).
3. in HTTP 1.0, status code 401 indicates (4). if the "File Not Found" prompt is returned, the header function is available. The statement is (5 ).
4. The role of the array function arsort is (6); the role of the statement error_reporting (2047) is (7 ).
5. the format of the database connection string in PEAR is (8 ).
6. write a regular expression and overwrite all JS/VBS scripts on the webpage (that is, remove the script tag and its content): (9 ).
7. install PHP using the Apache Module. in the http. conf file, first use the statement (10) to dynamically load the PHP module,
Then, use the statement (11) to make Apache process all files with the extension of php as PHP scripts.
8. the statement include and require can both include another file to the current file. The difference is (12). to avoid multiple inclusion of the same file, you can use the statement (13) to replace them.
9. attributes of the class can be serialized and saved to the session, so that the entire class can be restored later. the Function used is (14 ).
10. a function parameter cannot be a reference to a variable unless (15) is set to on.
11. the meaning of left join in SQL is (16 ).
If tbl_user records the student name and student ID ),
Tbl_score records the student's student ID (ID), score (score), and subject (subject ),
To print the student name and the total score of each subject, you can use SQL statement (17 ).

12. in PHP, heredoc is a special string whose end mark must be (18 ).

13. write a function to traverse all files and subfolders in a folder.
14. briefly describe the implementation principles of unlimited classification in the forum.
15. design a webpage so that a full screen window is displayed when it is opened, which contains a text box and a button. After entering information in the text box, you can click the button to close the window, but the entered information is displayed on the main page.

// Answer (fill in the blanks ):

1. echo $ _ SERVER ['php _ SELF ']; echo $ _ SERVER ["HTTP_REFERER"];
2. 0
3. (4) unauthorized (5) header ("HTTP/1.0 404 Not Found ");
4. (6) reverse sort the array and maintain the index relationship (7) All errors and warnings
5. not clear
6 ./ ]. *?>. *? <\/Script>/si
7. (10) LoadModule php5_module "D:/xampp/apache/bin/php5apache2. dll"

(11) AddType application/x-httpd-php-source. phps
AddType application/x-httpd-php. php. php5. php4. php3. phtml
8. (12) when an exception occurs, include generates a warning. require generates a fatal error (13) require_once ()/include_once ()
9. serialize ()/unserialize ()
10. allow_call_time_pass_reference
11. (16) natural left outer join
(17) select name, count (score) as sum_score from tbl_user left join tbl_score on tbl_user.ID = tbl_score.ID group by tbl_user.ID
12. the row containing the end identifier cannot contain any other characters ";"

13.

/**
* Traverse the directory and save the result to an array. Php4 and above are supported. Php5 and later use the scandir () function to replace the while loop.
* @ Param string $ dir
* @ Return array
*/
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] = rec_scandir ($ dir. "/". $ file );
} Else {
$ Files [] = $ file;
}
}
}
Closedir ($ handle );
Return $ files;
}
}

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.