PHP面試題及答案(四)

來源:互聯網
上載者:User

1 請說明 PHP 中傳值與傳引用的區別。什麼時候傳值什麼時候傳引用?
   答: 傳值只是把某一個變數的值傳給了另一個變數,而引用則說明兩者指向了同一個地方。
2 在PHP中error_reporting這個函數有什麼作用?
   答: The error_reporting() function sets the error_reporting directive at runtime. PHP has many levels of errors, using this function sets that level for the duration (runtime) of your script.
3 請用Regex(Regular Expression)寫一個函數驗證電子郵件的格式是否正確。
答:

<?php
if(isset($_POST['action']) && $_POST['action']=='submitted')
{
    $email=$_POST['email'];
    if(!preg_match("/^(?:w+.?)*w+@(?:w+.?)*w+$/",$email))
     {
        echo "電子郵件檢測失敗";
     }
    else
     {
        echo "電子郵件檢測成功";
     }
}
else
{
?>
<html>
<head><title>EMAIL檢測</title>
<script type="text/javascript">
    function checkEmail(sText)
     {
        var reg=/^(?:w+.?)*w+@(?:w+.?)*w+$/;
        var email=document.getElementById(sText).value;
        if(!reg.test(email))
         {
             alert("電子郵件檢測失敗");
         }
        else
         {
             alert("電子郵件格式正確");
         }
     }
</script>
</head>
<body>
<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="POST">
電子郵件:<input type="text" id="email" name="email" /><br />
<input type="hidden" name="action" value="submitted" />
<input type="button" name="button" value="用戶端檢測" onclick="checkEmail('email')" />
<input type="submit" name="submit" value="伺服器端檢測" />
</form>
</body>
</html>
<?php
}
?>

4 簡述如何得到當前執行指令碼路徑,包括所得到參數。

<?php
echo "http://".$_SERVER['SERVER_NAME'].$_SERVER['PHP_SELF']."?".$_SERVER['QUERY_STRING'];
//echo "http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
?>

5 有一個一維數組,裡面儲存整形資料,請寫一個函數,將他們按從大到小的順序排列。要求執行效率高。並說明如何改善執行效率。(該函數必須自己實現,不能使用php函數)

 

<?php
function BubbleSort(&$arr)
{
    $cnt=count($arr);
    $flag=1;
    for($i=0;$i<$cnt;$i++)
     {
        if($flag==0)
         {
            return;
         }
        $flag=0;
        for($j=0;$j<$cnt-$i-1;$j++)
         {
            if($arr[$j]>$arr[$j+1])
             {
                $tmp=$arr[$j];
                $arr[$j]=$arr[$j+1];
                $arr[$j+1]=$tmp;
                $flag=1;
             }
         }
     }
}
$test=array(1,3,6,8,2,7);
BubbleSort($test);
var_dump($test);
?>

 

6 請舉例說明在你的開發過程中用什麼方法來加快頁面的載入速度
   答:要用到伺服器資源時才開啟,及時關閉伺服器資源,資料庫添加索引,頁面可產生靜態,圖片等大檔案單獨伺服器。使用代碼最佳化工具啦

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.