PHP之幾道面試題

來源:互聯網
上載者:User

標籤:style   blog   http   color   os   io   使用   ar   檔案   

1.二分尋找演算法

function two_find($arr,$low,$height,$k){    if($low<=$height){        $mid = intval(($low+$height)/2);        if($arr[$mid]==$k){            return true;        }elseif($k<$arr[$mid]){           return two_find($arr,$low,$mid-1,$k);        }else{           return two_find($arr,$mid+1,$height,$k);        }    }    return false;}$arr = array(1,3,4,5,6,9,10,15,20,25,27,30);$key = 20;if(two_find($arr,min(array_keys($arr)),max(array_keys($arr)),$key)){    echo "二分尋找成功!";}else{    echo "二分尋找失敗!";}
View Code

2.一個字串中含有字母和數字,請輸出所有數位和,注意,連續的數字算一個數字。(比如輸入字串為“23abc23dbf23” 最後輸入的值應該為:69)

function str_sum($str){    $reg = ‘/[1-9]+[0-9]+/‘;    preg_match_all($reg,$str,$marth);    if(empty($marth)){        return false;    }    return array_sum($marth[0]);}echo str_sum("12adb012add012");
View Code

3.php從url取出副檔名,儘可能高效和簡單。

function get_url($url){    $path = pathinfo($url);    $extenstion = explode("?",$path[‘extension‘]);    return $extenstion[0];}echo ‘從url取出副檔名,url為:http://m.cn/asdfasdf/2.asdf.adsfa.php?a=bbb&c=adfas,他的副檔名為:‘.get_url(‘http://m.cn/asdfasdf/2.asdf.adsfa.php?a=bbb&c=adfas‘);
View Code

4.php中控制頁面的顯示狀態為404,請寫出PHp代碼

echo "php中用header()函數是可以為返回頁面添加404的頭資訊的,從而提示瀏覽器該網頁找不到了。所以可以使用:header(‘HTTP/1.0 404 Not Found‘);或者:header(‘Status: 404 Not Found‘);後者是在FastCGI模式下使用的,在php代碼中可以把兩句直接同時寫上。";

 

PHP之幾道面試題

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.