Examples of common PHP interview questions and algorithms-PHP source code

Source: Internet
Author: User
The PHP interview questions compiled in this article mainly come from two methods: one is the string and file operation example, and the other is the examples of several commonly used sorting algorithms. Let's take a look at them. The PHP interview questions compiled in this article mainly come from two methods: one is the string and file operation example, and the other is the examples of several commonly used sorting algorithms. Let's take a look at them.

Script ec (2); script

The following are four common questions, mainly examining the degree of understanding of string functions and file operation-related functions.

1. PHP flip Chinese strings

The Code is as follows:


Function reverse ($ str ){
$ R = array ();
For ($ I = 0; $ I $ R [] = mb_substr ($ str, $ I, 1, 'utf-8 ');
}
Return implode (array_reverse ($ r ));
}
Echo reverse ('www .111cn.net PHP blog ');
// Result: 'taobao PHP moc. ahphp. wwww'

2. PHP calculates the URL File suffix

The Code is as follows:

Function getext ($ url ){
$ Data = parse_url ($ url );
$ Path = $ data ['path'];
$ Info = pathinfo ($ path );
Return $ info ['extension'];
}
Echo getext ('HTTP: // www.111cn.net /');
// Result: 'html'

3. PHP calculates the relative path of the two files

The Code is as follows:

Function getrpath ($ path, $ conpath ){
$ PathArr = explode ('/', $ path );
$ ConpathArr = explode ('/', $ conpath );
$ Dismatchlen = 0;
For ($ I = 0; $ I If ($ conpathArr [$ I]! = $ PathArr [$ I]) {
$ Dismatchlen = count ($ pathArr)-$ I;
$ Arrleft = array_slice ($ pathArr, $ I );
Break;
}
}
Return str_repeat ('../', $ dismatchlen). implode ('/', $ arrleft );
}
$ A = '/a/B/c/d/e. php ';
$ B = '/a/B/12/34/5. php ';
Echo getrpath ($ a, $ B );
// Result: '../c/d/e. php'

4. PHP traverses all files and folders in the directory

The Code is as follows:
Function finddir ($ dir ){
$ Files = array ();
If (is_dir ($ dir )){
If ($ handle = opendir ($ dir )){
While ($ file = readdir ($ handle ))! = False ){
If ($ file! = '.' & $ File! = '..'){
If (is_dir (rtrim ($ dir, '/'). '/'. $ file )){
$ Files [$ file] = finddir (rtrim ($ dir, '/'). '/'. $ file );
} Else {
$ Files [] = rtrim ($ dir, '/'). '/'. $ file;
}
}
}
Closedir ($ handle );
}
}
Return $ files;
}
Print_r (finddir ('f:/Golang/src '));
// Result:
Array
(
[0] => F:/Golang/src/hello. go
[1] => F:/Golang/src/src.exe
[Test] => Array
(
[0] => F:/Golang/src/test/sss.txt
)

)

In addition to the basic functions of these string and file operations, the basic algorithms are also widely used in interviews. For more information, see the previous articles on the basic PHP algorithms.

Next we will share some of the most common algorithms that can be implemented using PHP.

1. Bubble Sorting

The Code is as follows:

Function bubble_sort ($ arr ){
$ N = count ($ arr );
For ($ I = 0; $ I <$ n-1; $ I ++ ){
For ($ j = $ I + 1; $ j <$ n; $ j ++ ){
If ($ arr [$ j] <$ arr [$ I]) {
$ Temp = $ arr [$ I];
$ Arr [$ I] = $ arr [$ j];
$ Arr [$ j] = $ temp;
}
}
}
Return $ arr;
}

2. Merge and sort

The Code is as follows:

Function Merge (& $ arr, $ left, $ mid, $ right ){
$ I = $ left;
$ J = $ mid + 1;
$ K = 0;
$ Temp = array ();
While ($ I <= $ mid & $ j <= $ right)
{
If ($ arr [$ I] <= $ arr [$ j])
$ Temp [$ k ++] = $ arr [$ I ++];
Else
$ Temp [$ k ++] = $ arr [$ j ++];
}
While ($ I <= $ mid)
$ Temp [$ k ++] = $ arr [$ I ++];
While ($ j <= $ right)
$ Temp [$ k ++] = $ arr [$ j ++];
For ($ I = $ left, $ j = 0; $ I <= $ right; $ I ++, $ j ++)
$ Arr [$ I] = $ temp [$ j];
}

Function MergeSort (& $ arr, $ left, $ right)
{
If ($ left <$ right)
{
$ Mid = floor ($ left + $ right)/2 );
MergeSort ($ arr, $ left, $ mid );
MergeSort ($ arr, $ mid + 1, $ right );
Merge ($ arr, $ left, $ mid, $ right );
}
}

3. Binary Search-recursion

The Code is as follows:

Function bin_search ($ arr, $ low, $ high, $ value ){
If ($ low> $ high)
Return false;
Else {
$ Mid = floor ($ low + $ high)/2 );
If ($ value = $ arr [$ mid])
Return $ mid;
Elseif ($ value <$ arr [$ mid])
Return bin_search ($ arr, $ low, $ mid-1, $ value );
Else
Return bin_search ($ arr, $ mid + 1, $ high, $ value );
}
}

4. Binary Search-non-recursive

The Code is as follows:
Function bin_search ($ arr, $ low, $ high, $ value ){
While ($ low <= $ high ){
$ Mid = floor ($ low + $ high)/2 );
If ($ value = $ arr [$ mid])
Return $ mid;
Elseif ($ value <$ arr [$ mid])
$ High = $ mid-1;
Else
$ Low = $ mid + 1;
}
Return false;
}

5. Fast sorting

The Code is as follows:

Function quick_sort ($ arr ){
$ N = count ($ arr );
If ($ n <= 1)
Return $ arr;
$ Key = $ arr [0];
$ Left_arr = array ();
$ Right_arr = array ();
For ($ I = 1; $ I <$ n; $ I ++ ){
If ($ arr [$ I] <= $ key)
$ Left_arr [] = $ arr [$ I];
Else
$ Right_arr [] = $ arr [$ I];
}
$ Left_arr = quick_sort ($ left_arr );
$ Right_arr = quick_sort ($ right_arr );
Return array_merge ($ left_arr, array ($ key), $ right_arr );
}

6. Select sorting

The Code is as follows:

Function select_sort ($ arr ){
$ N = count ($ arr );
For ($ I = 0; $ I <$ n; $ I ++ ){
$ K = $ I;
For ($ j = $ I + 1; $ j <$ n; $ j ++ ){
If ($ arr [$ j] <$ arr [$ k])
$ K = $ j;
}
If ($ k! = $ I ){
$ Temp = $ arr [$ I];
$ Arr [$ I] = $ arr [$ k];
$ Arr [$ k] = $ temp;
}
}
Return $ arr;
}

7. Insert sorting

The Code is as follows:

Function insertSort ($ arr ){
$ N = count ($ arr );
For ($ I = 1; $ I <$ n; $ I ++ ){
$ Tmp = $ arr [$ I];
$ J = $ I-1;
While ($ arr [$ j]> $ tmp ){
$ Arr [$ j + 1] = $ arr [$ j];
$ Arr [$ j] = $ tmp;
$ J --;
If ($ j <0)
Break;
}
}
Return $ arr;
}

Of course, there will be more examples of other operations such as database operations or some basic functions. We will not write them here. You can find them in local articles.

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.