PHP面試題(二)

來源:互聯網
上載者:User
1. 請對POSIX風格和相容Perl風格兩種Regex的主要函數進行類比說明
ereg preg_match
ereg_replace preg_replace


2. 請說明在php.ini中safe_mode開啟之後對於PHP系統函數的影響
   safe_mode是提供一個基本安全的共用環境。在一個多使用者共用的phpweb伺服器上,當這台伺服器開啟了safe_mode模式,有以下函數將會受到影響。首先,一下嘗試訪問檔案系統的函數將會被限制,運行伺服器的使用者id,如果想要嘗試操作某個檔案,必須要使用者該檔案的讀取或者寫入的存取權限。
   因此,在safe_mode開啟的情況下,下列函數將會收到限制:
ckdir,move_uploaded_file,chgrp,parse_ini_file,chown,rmdir,copy,rename,fopen,require,highlight_file,show_source,include,symlink,link,touch,mkdir,unlink
   以上都是跟操作檔案系統有關的函數,除此之外,一些php擴充的函數也會受到限制,不能在程式裡面直接載入擴充,只能到php.ini裡載入,而且php如果需要執行作業系統的程式時,必須在safe_mode_exec_dir中指定程式的路徑,否則執行將失敗。此外還有exec,shell_exec,pasathru,system,popen等函數會收到限制


3. PHP5中魔術方法函數有哪幾個,請舉例說明各自的用法
__sleep
__wakeup
__toString
__set_state
__construct,
__destruct
__call,
__get,
__set,
__isset,
__unset
__sleep,
__wakeup,
__toString,
__set_state,
__clone
__autoload


4. 請寫出讓,並說明如何在命令列下運行PHP指令碼(寫出兩種方式)同時向PHP指令碼傳遞參數?
$PHP_HOME/bin/php -r 'echo $argv[1];' hello
$PHP_HOME/bin/php hello.php hello

   window下,假設php安裝目錄為c:\program files\php5\,那麼使用命令視窗進入到該路徑下,敲入php hello.php斷行符號,則會執行當前路徑下的hello.php檔案,
   如果要指向其他路徑下php檔案,可以在php 路徑/hello.php ,這種形式稱為CLI模式,我們平時通過瀏覽器看到的那種成為CGI模式,至於傳遞參數,php檔案在cli模式下,直接通過在檔案名稱後面接參數,多個參數中間用空格隔開,在php檔案裡面是通過兩個變數來擷取參數的,一個是$argv,一個是$argc,前者是傳遞參數的數組,預設第一個為php檔案的名稱;後者為$argv的數組個數。
   linux下,一般程式安裝都會安裝在/usr/bin/php下面,可以通過man php查看一下,如果有資訊說明可以使用,使用方法類似於window下。如果前面這步成立,那麼你可以直接 php php檔案 來運行php檔案,如果man php沒有資訊,則說明當前php執行檔案沒有在環境路徑裡面,可以修改環境路徑包含php路徑,也可以類似於window進入php路徑,在執行 php php檔案。其他類似於window下。


5. PHP的垃圾收集機制是怎樣的
在PHP中,沒有任何變數指向這個對象時,這個對象就成為垃圾。PHP會將其在記憶體中銷毀;這是PHP 的GC垃圾處理機制,防止記憶體溢出。
當一個 PHP線程結束時,當前佔用的所有記憶體空間都會被銷毀,當前程式中所有對象同時被銷毀。GC進程一般都跟著每起一個SESSION而開始啟動並執行.gc目的是為了在session檔案到期以後自動銷毀刪除這些檔案.
執行這些函數也可以起到回收作用
__destruct /unset/mysql_close


6.使對象可以像數組一樣進行foreach迴圈,要求屬性必須是私人。
(Iterator模式的PHP5實現,寫一類實現Iterator介面)
PHP5開始支援了介面, 並且內建了Iterator介面, 所以如果你定義了一個類,並實現了Iterator介面,那麼你的這個類對象就是ZEND_ITER_OBJECT,否則就是ZEND_ITER_PLAIN_OBJECT.
對於ZEND_ITER_PLAIN_OBJECT的類,foreach會通過HASH_OF擷取該對象的預設屬性數組,然後對該數組進行foreach.
而對於ZEND_ITER_OBJECT的類對象,則會通過調用對象實現的Iterator介面相關函數來進行foreach, 所以, 對於這道筆試題, 可以作出如下的答案:
[php]
class sample implements Iterator
{
private $_items = array(1,2,3,4,5,6,7);


public function __construct() {
;//void
}
public function rewind() { reset($this->_items); }
public function current() { return current($this->_items); }
public function key() { return key($this->_items); }
public function next() { return next($this->_items); }
public function valid() { return ( $this->current() !== false ); }
}
$sa = new sample();
foreach($sa as $key => $val){
print $key . "=>" .$val;
}[/php]
以上代碼在我的php 5.3下運行正常。

7.請寫一段PHP代碼,確保多個進程同時寫入同一個檔案成功
function writeData($path, $mode, $data){
$fp = fopen($path, $mode);
$retries = 0;
$max_retries = 100;
do {
if ($retries > 0) {
usleep(rand(1, 10000));
}
$retries += 1;
}while (!flock($fp, LOCK_EX) and $retries <= $max_retries);

if ($retries == $max_retries) {
return false;
}

fwrite($fp, "$data\n");
flock($fp, LOCK_UN);
fclose($fp);
return true;
}


8. 用PHP實現一個雙向隊列

9. 使用Regex提取一段標識語言(html或xml)程式碼片段中指定標籤的指定屬性值(需考慮屬性值對不規則的情況,如大小寫不敏感,屬性名稱值與等號間有空格等)。此處假設需提取test標籤的attr屬性值,請自行構建包含該標籤的串



10.請使用socket相關函數(非curl)實現如下功能:構造一個post請求,發送到指定http server的指定連接埠的指定請求路徑(如http://www.example.com:8080/test)。請求中包含以下變數:
使用者名稱(username):溫柔一刀
密碼(pwd):&123=321&321=123&
簡歷(intro):Hello world!
且該http server需要以下cookie來進行簡單的使用者動作跟蹤:

cur_query:you&me
last_tm:...(上次請求的unix時間戳記,定為當前請求時間前10分鐘)
cur_tm:...(當前請求的unix時間戳記)
設定逾時為10秒,發出請求後,將http server的響應內容輸出。


function encode($data, $sep = ‘&’){
while (list($k,$v) = each($data)) {
$encoded .= ($encoded ? "$sep" : "");
$encoded .= rawurlencode($k)."=".rawurlencode($v);
}
Return $encoded;
}

function post($url, $post, $cookie){
$url = parse_url($url);
$post = encode($data, ‘&’);
$cookie = encode($cookieArray, ‘;’);
$fp = fsockopen($url['host'], $url['port'] ? $url['port'] : 80, $errno, $errstr, 10);
if (!$fp) return "Failed to open socket to $url[host]";
fputs($fp, sprintf("POST %s%s%s HTTP/1.0\n", $url['path'], $url['query'] ? "?" : "", $url['query']));
fputs($fp, "Host: $url[host]\n");
fputs($fp, "Content-type: application/x-www-form-urlencoded\n");
fputs($fp, "Content-length: " . strlen($encoded) . "\n");
fputs($fp, "Cookie: $cookie\n\n");
fputs($fp, "Connection: close\n\n");
fputs($fp, "$post \n");
while (!feof($fp)) {
echo fgets($fp, 128);
}

fclose($fp);
}

$url = ‘http://www.example.com:8080/test’;
$encoded = username=溫柔一刀& pwd=
$post = array(
‘username’=> ‘溫柔一刀’,
‘pwd => ‘&123=321&321=123&’,
‘intro => ‘Hello world!’
);

$cookie = array(
‘cur_query’ => ‘you&me,
‘last_tm’ => time() - 600,
‘cur_tm ‘=> time()
);

Post($url, $post, $cookie);

11.你用什麼方法檢查PHP指令碼的執行效率(通常是指令碼執行時間)和資料庫SQL的效率(通常是資料庫Query時間),並定位和分析指令碼執行和資料庫查詢的瓶頸所在?
1.指令碼執行時間,啟用xdebug,使用WinCacheGrind分析。
2.資料庫查詢,mysql使用EXPLAIN分析查詢,啟用slow query log記錄慢查詢。
3, xdebug統計函數執行次數和具體時間進行分析。
4, 線上系統用strace跟蹤相關進程的具體系統調用。


PHP LAMP Engineer Test Paper
Question 1
What does print out?
A) 3
B) False
C) Null
D) 1
E) 0


Question 2
Which of the following snippets prints a representation of 42 with two decimal places?
A) printf("%.2d\n", 42);
B) printf("%1.2f\n", 42);
C) printf("%1.2u\n", 42);


Question 3
Given
$text = 'Content-Type: text/xml';
Which of the following prints 'text/xml'?
A) print substr($text, strchr($text, ':'));
B) print substr($text, strchr($text, ':') + 1);
C) print substr($text, strpos($text, ':') + 1);
D) print substr($text, strpos($text, ':') + 2);
E) print substr($text, 0, strchr($text, ':')
Question 4
What is the value of $a?
$a = in_array('01', array('1')) == var_dump('01' == 1);
?>
A) True
B) False
Question 5
What is the value of $result in the following PHP code?
function timesTwo($int) {
$int = $int * 2;
}
$int = 2;
$result = timesTwo($int);
?>;
Answer: NULL
Question 6
The code below ___________ because ____________.
class Foo {
?>
function bar() {
print "bar";
}
}
?>
A) will work, class definitions can be split up into multiple PHP blocks.
B) will not work, class definitions must be in a single PHP block.
C) will not work, class definitions must be in a single file but can be in multiple PHP blocks.
D) will work, class definitions can be split up into multiple files and multiple PHP blocks.
Question 7
When turned on, ____________ will _________ your script with different variables from HTML forms and cookies.
A) show_errors, enable
B) show_errors, show
C) register_globals, enhance
D) register_globals, inject
Question 8
What will be the output of the following PHP code:
echo count(strlen("http://php.net"));
?>
Answer: 1
Question 9
What is the best all-purpose way of comparing two strings?
A) Using the strpos function
B) Using the == operator
C) Using strcasecmp()
D) Using strcmp()
Question 10
What is the difference between "print()" and "echo()"?
Answer: print is a function,echo is a language construct

  • 聯繫我們

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