Php interview questions set-basic program questions

Source: Internet
Author: User

Basic Program questions


Print the time format of the previous day in PHP: 22:21:21

date('Y-m-d H:i:s', strtotime('-1 day'));

How to Implement string flip?
Method 1:
<?phpfunction reverse($var) {  $res = "";  for($i = 0, $j = strlen($var);$i<$j;$i++) {    $res = $var[$i].$res;  }  return $res;}$tmpvar = "wofang";$res = reverse($tmpvar);echo $res;?>
Method 2:
<?php echo strrev("Hello world!"); // outputs "!dlrow olleH" ?>

Write a function to traverse all files and subfolders in a folder.
<? Php/*** traverses the Directory and stores the result in 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 ;}}?>

Please write a function to verify that the email format is correct
<?phpfunction checkEmail( $email ) {    $preg = "/([a-z0-9]*[-_\.]?[a-z0-9]+)*@([a-z0-9]*[-_]?[a-z0-9]+)+[\.][a-z]{2,3}([\.][a-z]{2})?/i";    return preg_match( $preg, $email );}?>

There is a webpage address, such as the PHP development resources home page: http://www.phpres.com/index.html. how can I find my website content?
Method 1 (for php5 and later versions ):
<?php$readcontents = fopen('http://www.phpers.com/index.html');$contents = stream_get_contents($readcontents);fclose($readcontents);echo $contents;?>
Method 2:
<?php echo file_get_contents('http://www.phpers.com/index.html'); ?>

How to obtain the path of the script to be executed, including the obtained parameters.
Note: for example, there is a script www.domain.com, and the parameters passed to it include parameter 1, parameter 2, parameter 3 .... The method for passing parameters may be GET or POST,
So now please write like http://www.domain.com/script.php? Result of parameter 1 = value 1 & Parameter 2 = value 2...
No answer

Write a sorting algorithm, which can be Bubble sorting or fast sorting. Assume that the object to be sorted is a one-dimensional array.
(Note: you cannot use existing functions of the system. Remember the basic knowledge you have learned before)
<? Php/*** sorting class */class Sort {/** Bubble Sorting small to large */public function bubble_sort ($ array) {$ count = count ($ array ); if ($ count <= 0) return false; for ($ I = 0; $ I <$ count; $ I ++) {for ($ j = 1; $ j <= $ count-$ I-1; $ j ++) {if ($ array [$ j] <$ array [$ j-1]) {$ tmp = $ array [$ j]; $ array [$ j] = $ array [$ j-1]; $ array [$ j-1] = $ tmp; }}return $ array;}/*** Quick Sort */public function quick_sort ($ Rr) {$ len = count ($ arr); if ($ len <= 1) return $ arr; $ key = $ arr [0]; $ left_arr = $ right_arr = array (); for ($ I = 1; $ I <$ len; $ I ++) {if ($ arr [$ I] <= $ key) $ left_arr [] = $ arr [$ I]; else $ right_arr [] = $ arr [$ I];} $ left_arr = $ this-> quick_sort ($ left_arr); $ right_arr = $ this-> quick_sort ($ right_arr); return array_merge ($ left_arr, array ($ key ), $ right_arr);}/*** Hill sort */public func Tion shell_sort ($ datas) {// group for ($ increment = count ($ datas)/2; $ increment> 0; $ increment = $ increment/2) {// sort each group for ($ I = $ increment; $ I <count ($ datas); $ I ++) {$ temp = $ datas [$ I]; $ j = 0; for ($ j = $ I; $ j >=$ increment; $ j = $ j-$ increment) {if ($ temp <$ datas [$ j-$ increment]) {$ datas [$ j] = $ datas [$ j-$ increment];} else {break ;}$ datas [$ j] = $ temp ;}} return $ Datas ;}}?>

There is a table menu (mainmenu, submenu, url). Please use recursion to write a tree menu and list all menus.
No answer

You can write a program to find the maximum number.
No answer

There is an array $ a = array (, 2); sort it again and list it in ascending order.
No answer

Write two regular expressions that match the email address and URL. Similar to the following:
Email Address: user_name.first@example.com.cn
URL: http://www.example.cpm.cn/user_profile.php? Uid = 1, 100
(Note: The standard regular expression is the regular expression that can be parsed by the regular expression of the preg _ * class in php)
Email:/\ w + ([-+.] \ w +) * @ \ w + ([-.] \ w + )*\. \ w + ([-.] \ w + )*/;
URL:/^ http: \/[\ w] + \. [\ w] + [\ S] */

Write a piece of code in php. If no 3rd variables are used, the values of $ a and $ B are exchanged, and the initial values of $ a and $ B are set.
<?php    $a = 1;    $b = 2;    list($b, $a) = array($a, $b);?> 

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:
<?phpfunction getExt($url){  $arr = parse_url($url);  $file = basename($arr['path']);  $ext = explode(".", $file);  return $ext[1];}?>  
Answer 2:
<?phpfunction 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); }}?>  

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 ';
The relative path of $ B relative to $ a should be ../c/d.
<?php   function getRelativePath( $a, $b ) {       $returnPath = array( dirname( $b ) );       $arrA = explode( '/', $a );       $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 );?> 


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.