PHP interview questions (1)

Source: Internet
Author: User
Tags true true
Preface I am about to start recruiting students. My job is mainly for PHP and C development. I will also find several PHP interview questions from the Internet recently, and I will record the basic questions myself. 1. Answer
<? PHP $ str1 = NULL; $ str2 = false; echo $ str1 = $ str2? 'Equality ': 'unequal ';

Equal.

<? PHP $ str1 = ''; $ str2 = 0; echo $ str1 = $ str2? 'Equality ': 'unequal ';

Equal

<? PHP $ str1 = 0; $ str2 = '0'; echo $ str1 ===$ str2? 'Equality ': 'unequal ';

Explanation of inequality: in PHP, variables are stored in a C-language struct. Both null and null, and false are stored in a value of 0. The struct has a zend_uchar type; such a member variable is used to save the type of the variable, while the null string type is string, the null type is null, and false is boolean.
2. Write the output results of the following program

<?php$a1 = null;$a2 = false;$a3 = 0;$a4 = '';$a5 = '0';$a6 = 'null';$a7 = array();$a8 = array(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 ';?>

True true false True False
Explanation: the judgment of empty is stricter.

3. Write the output results of the following program

<?php$test = 'aaaaaa';$abc = & $test;unset($abc);echo $abc;?>

Aaaaaa

Explanation: For reference transfer, the number of references in memory occupied by 'aaaaa' is not 0, so the memory is not released and the output is aaaaaa. 4. Write the output results of the following program
<?php$count = 5;function get_count(){    static $count = 0;        return $count ++;}echo $count;++ $count;echo get_count();echo get_count();?>

501 explanation: basic variable scopes and static variables5. Write the output results of the following program

<?php$GLOBALS['var1'] = 5;$var2 = 1;function get_value(){    global $var2;    $var1 = 0;        return $var2 ++;}get_value();echo $var1;echo $var2;

52. Explanation: global $ var2 makes $ var2 use a global variable $ var1 and $ globals ['var1'] instead of a global variable. Therefore, it is a local variable, modifications to local variables do not affect global variables.6. Write the output results of the following program

<?phpfunction 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);

12 interpretation: Pass the score and reference7. Get the extension of a file in more than 5 WaysRequirement: Dir/upload.image.jpg, find .jpg or JPG

<? PHP/*** get the file extension of the specified path in five ways */$ STR = "DIR/upload.image.jpg"; function one ($ Str) {$ arr = explode ('. ', $ Str); $ COUNT = count ($ ARR); return $ arr [$ count-1];} function two ($ Str) {$ Len = strlen ($ Str); For ($ I = $ len-1, $ name = ''; $ STR [$ I]! = '. '; $ I --) {$ name. = $ STR [$ I] ;}$ name = strrev ($ name); return $ name;} function three ($ Str) {$ Path = pathinfo ($ Str ); return $ path ['extension'];} function four ($ Str) {$ arr = explode ('. ', $ Str); Return array_pop ($ ARR);} function five ($ Str) {$ start = strrpos ($ STR ,'. '); Return substr ($ STR, $ start + 1);} Echo One ($ Str); echo "<br>"; Echo Two ($ Str ); echo "<br>"; Echo Three ($ Str); echo "<br>"; Echo Four ($ Str); echo "<br> "; echo Five ($ Str); echo "<br> ";
Algorithm questions 1. Use PHP to describe Bubble sorting and quick sorting. The object can be an array.
<? PHP/*** Bubble Sorting Algorithm Implementation (from small to large) */function maopaosort (& $ array) {$ COUNT = count ($ array); For ($ I = 0; $ I <$ count-1; $ I ++) {for ($ J = 0; $ j <$ count-$ I-1; $ J ++) {if ($ array [$ J]> $ array [$ J + 1]) {$ TMP = $ array [$ J]; $ array [$ J] = $ array [$ J + 1]; $ array [$ J + 1] = $ TMP ;}}}} /*** Quick Sort */function merge tparation (& $ array, $ start, $ end) {$ stand = $ array [$ start]; while ($ start <$ end) {While ($ start <$ end & $ array [$ end] >=$ stand) {$ end --;} if ($ start <$ end) {$ array [$ start ++] = $ array [$ end];} while ($ start <$ end & $ array [$ start] <= $ stand) {$ start ++;} if ($ start <$ end) {$ array [$ end --] = $ array [$ start] ;}} $ array [$ start] = $ stand; return $ start;} function quicksort (& $ array, $ begin, $ end) {if ($ begin <$ end) {$ topology = maid ($ array, $ begin, $ end); quicksort ($ array, $ begin, $ sort-1); quicksort ($ array, $ sort + 1, $ end) ;}$ arr = array (5, 1, 3, 2, 19, 11, 25, 12,100,100 00, 12 ); // Bubble Sorting maopaosort ($ ARR); print_r ($ ARR); echo "<br>"; // quick sorting $ COUNT = count ($ ARR ); quicksort ($ arr, 0, $ count-1); print_r ($ ARR );
2. Use PHP to describe sequential search and Binary Search
<? PHP/*** sequential search */function seqsearch ($ arr, $ needle) {for ($ I = 0, $ Len = count ($ ARR ); $ I <$ Len; $ I ++) {if ($ arr [$ I] ==$ needle) {return $ I ;}} return-1 ;} /*** Binary Search */function midsearch ($ arr, $ start, $ end, $ needle) {While ($ start <= $ end) {$ mid = (INT) ($ start + ($ end-$ start)/2); // prevents if ($ arr [$ mid] = $ needle) from exceeding the integer representation range) {return $ mid;} else if ($ arr [$ mid]> $ needle) {$ end = $ mid-1 ;} else {$ start = $ Mid + 1 ;}} return-1 ;}$ arr = array (1, 2, 3, 4, 5, 6, 7, 8, 9, 10); $ needle = 5; echo seqsearch ($ arr, $ needle); echo "<br>"; echo midsearch ($ arr, 0, count ($ ARR) -1, $ needle );

3. Write a two-dimensional array Sorting Algorithm function that is universal and can call PHP built-in functions.

/*** Description: obtain the location of the central vertex ** @ Param array $ array * @ Param int $ left * @ Param int $ right * @ Param string $ field * @ return int */function fetcharrayray( & $ array, $ left, $ right, $ field) {// baseline Definition $ stand = $ array [$ left]; // traverses the array while ($ left <$ right) {While ($ left <$ Right & $ array [$ right] [$ field] >=$ stand [$ field]) {$ right --;} if ($ left <$ right) {$ array [$ left ++] = $ array [$ right];} while ($ left <$ Right & $ array [$ left] [$ field] <= $ stand [$ field]) {$ left ++ ;} if ($ left <$ right) {$ array [$ right --] = $ array [$ left];} // obtain the central position $ array [$ left] = $ stand; return $ left;}/*** description: quick sorting main program ** @ Param array $ array * @ Param int $ begin * @ Param int $ end * @ Param string $ field */function quicksort (& $ array, $ begin, $ end, $ field) {// variable definition $ begin = NULL; if ($ begin <$ end) {$ begin = fetcharrayray( $ array, $ begin, $ end, $ field); quicksort ($ array, $ begin, $ fields-1, $ field); quicksort ($ array, $ struct + 1, $ end, $ field );}}

Add a field parameter using the concept of "Quick Sort"

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.