Home >Backend Development >PHP Tutorial >Summary of PHP written test questions

Summary of PHP written test questions

WBOY
WBOYOriginal
2016-07-29 09:04:51931browse

1. What function would you use to capture remote images locally?
fsockopen, A
2. Use the least amount of code to write a function that finds the maximum of 3 values.
function($a,$b,$c){
* W0 z* u6 k+ e. L a: }5 } return $a>$b? ($a>$c? $a : $c) : ($b>$c? $b : $c );
5 O: F6 v1 W# U}
3. Use PHP to print out the time of the previous day. The printing format is May 10, 2007 22:21:21
Echo date('Y-m-d H:i:s',strtotime( '-1 day'));
4. Can JavaScript define a two-dimensional array? If not, how do you solve it?
Javascript does not support two-dimensional array definition. You can use arr[0] = new array() to solve it
5. Assume that a.html and b.html are in the same folder. Use JavaScript to automatically jump to b.html after opening a.html for five seconds.
function go2b(){
window.location = “b.html”;
window.close();
}
setTimeout( “go2b()”,5000 ); //Automatically execute go2b() after 5 seconds

6. //The IP address of the user who is browsing the current page: 127.0.0.1
echo $_SERVER["REMOTE_ADDR"]."
";
//The string of query (after the first question mark? in the URL) content): id=1&bi=2
echo $_SERVER["QUERY_STRING"]."
";
//The document root directory where the currently running script is located: d:inetpubwwwroot
echo $_SERVER["DOCUMENT_ROOT"]."
";
7. In HTTP 1.0, the meaning of status code 401 is unauthorized ____; if the prompt "File not found" is returned, the header function can be used, and its statement is header("HTTP/1.0 404 Not Found" );
Answer: 401 means unauthorized; header(“HTTP/1.0 404 Not Found”);
8. Write a function that can traverse all files and subfolders in a folder.
function my_scandir($dir)
{
$files=array();
if(is_dir($dir))
{
if($handle=opendir($dir))
{
while( ($file=readdir($handle))!==false)
{
if($file!=”.” && $file!=”..”)
{
if(is_dir($dir.”/” .$file))
{
$files[$file]=my_scandir($dir.”/”.$file);
}
else
{
$files[]=$dir.”/”.$file;
}
}
}
closedir($handle);
return $files;
}
}
}
print_r(my_scandir(“D:Program FilesInternet ExplorerMUI”));
?>

9. Add John to the new page Add to users array?
$users[] = 'john'; array_push($users,'john');

10. What is the function of error_reporting in PHP?
Answer: error_reporting() sets the error level of PHP and returns the current level.

11. Please use regular expression (Regular Expression) to write a function to verify whether the format of the email is correct.
Answer:
$email=$_POST['email'];
if(!preg_match('/^[w.]+@([w.]+).[a-z]{2,6 }$/i',$email)) {
echo "Email detection failed";
}else{
echo "Email detection successful";
}
?>
12. Use PHP to write out the client IP Code with server IP
Answer: print client IP: echo $_SERVER['REMOTE_ADDR']; or: getenv('REMOTE_ADDR');
print server IP: echo gethostbyname("www.bolaiwu.com")

13 , How to modify the lifetime of SESSION (1 point).
Answer: Method 1: Set session.gc_maxlifetime in php.ini to 9999 and restart apache
Method 2: $savePath = “./session_save_dir/”;
$lifeTime = Hours * seconds;
session_save_path($savePath);
session_set_cookie_params($lifeTime);
session_start();
Method 3: setcookie() and session_set_cookie_params($lifeTime);

14. Have a web page address, such as PHP development resources Home page: http://www.phpres.com/index.html, how to get its contents? ($1 point)
Answer: Method 1 (for PHP5 and above):
$readcontents = fopen("http: //www.phpres.com/index.html", "rb");
$contents = stream_get_contents($readcontents);
fclose($readcontents);
echo $contents;
Method 2:
echo file_get_contents("http ://www.phpres.com/index.html”);

15. Please explain the difference between passing values ​​and passing references in PHP. When to pass by value and when to pass by reference? (2 points)
Answer: Pass by value: Any changes to the value within the function scope will be ignored outside the function
Pass by reference: Any change to the value within the function scope is outside the function Can also reflect these modifications
Advantages and Disadvantages: When passing by value, PHP must copy the value. Especially for large strings and objects, this can be an expensive operation.
Passing by reference does not require copying the value, which is very good for improving performance.

16. Write a function to retrieve the file extension from a standard URL as efficiently as possible
For example: http://www.sina.com.cn/abc/de/fg.php?id=1 Required Take out php or .php
Answer 1:
function getExt($url){
$arr = parse_url($url);
$file = basename($arr['path']);
$ext = explode(".",$file);
return $ext[1];
}
Answer 2:
function 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);
}
}

17. Use more than five methods to get the extension of a file
Requirements: dir/upload.image.jpg, find .jpg or jpg,
Must use PHP’s own processing function for processing, the method cannot be obvious Repeat, it can be encapsulated into functions, such as get_ext1($file_name), get_ext2($file_name)
function get_ext1($file_name){
return strrchr($file_name, '.');
}
function get_ext2($file_name){
return substr($file_name, strrpos($file_name, '.'));
}
function get_ext3($file_name){
return array_pop(explode('.', $file_name));
}
function get_ext4($file_name ){
$p = pathinfo($file_name);
return $p['extension'];
}
function get_ext5($file_name){
return strrev(substr(strrev($file_name), 0, strpos(strrev( $file_name), '.')));
}

18、$str1 = null;
$str2 = false;
echo $str1==$str2 ? 'Equal' : 'Not equal' ;
$str3 = ”;
$str4 = 0;
echo $str3==$str4 ? 'Equal' : 'Not equal';
$str5 = 0;
$str6 = '0';
echo $str5= ==$str6 ? 'Equal' : 'Not equal';
?>
Equal, equal, not equal

19. What is the main difference between the field types varchar and char in the MySQL database? Which field has higher search efficiency? , why?
Varchar is variable length, saving storage space, while char is fixed length. The search efficiency is faster than char type, because varchar is non-fixed length, you must first search the length, and then extract the data, which is one more step than the char fixed-length type, so the efficiency is lower

20. Please use JavaScript to write three types of generation Method of an Image tag (hint: think from the perspective of methods, objects, and HTML)
(1)var img = new Image();
(2)var img = document.createElementById(“image”)
(3)img. innerHTML = “”

21, 16. Please describe more than two of the most significant differences between XHTML and HTML
(1) XHTML must be forced to specify the document type DocType, HTML does not need to
(2) All XHTML tags must be closed, HTML comparison Feel free to
22. Write a sorting algorithm, which can be bubble sort or quick sort. Assume that the object to be sorted is a dimensional array.
//Bubble sort (array sort)
function bubble_sort($array)
{
$count = count($array);
if ($count <= 0) return false;
for($i=0; $i<$count; $i++){
for($j=$count-1; $j>$i; $j–){
if ($array[$j] < $array[$j-1 ]){
$tmp = $array[$j];
$array[$j] = $array[$j-1];
$array[$j-1] = $tmp;
}
}
}
return $array;
}
//Quick sort (array sort)
function quicksort($array) {
if (count($array) <= 1) return $array;
$key = $array[0] ;
$left_arr = array();
$right_arr = array();
for ($i=1; $iif ($array[$i] <= $key)
$left_arr[] = $array[$i];
else
$right_arr[] = $array[$i];
}
$left_arr = quicksort($left_arr);
$right_arr = quicksort($ right_arr);
return array_merge($left_arr, array($key), $right_arr);
}

23. Write the names of more than three MySQL database storage engines (tip: not case sensitive)
MyISAM, InnoDB, BDB (Berkeley DB), Merge, Memory (Heap), Example, Federated, Archive, CSV, Blackhole, MaxDB and more than a dozen engines

24. Find the difference between two dates, such as 2007-2-5 ~ 2007 -3-6 date difference
Method one:
class Dtime
{
function get_days($date1, $date2)
{
$time1 = strtotime($date1);
$time2 = strtotime( $date2);
return ($time2-$time1)/86400;
}
}
$Dtime = new Dtime;
echo $Dtime->get_days('2007-2-5′, '2007-3-6 ′);
?>
Method 2:
$temp = explode('-', '2007-2-5′);
$time1 = mktime(0, 0, 0, $temp[ 1], $temp[2], $temp[0]);
$temp = explode('-', '2007-3-6′);
$time2 = mktime(0, 0, 0, $temp[ 1], $temp[2], $temp[0]);
echo ($time2-$time1)/86400;
Method 3: echo abs(strtotime(“2007-2-1″)-strtotime(“2007 -3-1″))/60/60/24 Calculate the time difference

25. Please write a function to achieve the following functions:
Convert the string "open_door" to "OpenDoor", "make_by_id" to "MakeById".
Method:
function str_explode($str){
$str_arr=explode(“_”,$str);$str_implode=implode(” “,$str_arr); $str_implode=implode
(“”,explode(” “,ucwords($ str_implode)));
return $str_implode;
}
$strexplode=str_explode("make_by_id");print_r($strexplode);
Method 2: $str="make_by_id!";
$expStr=explode("_" ,$str);
for($i=0;$i{
echo ucwords($expStr[$i]);
}Method 3: echo str_replace(' ', ”,ucwords(str_replace('_',' ','open_door')));

26. There are multiple records of Id in a table. Find all the records of this id and display the total number of records. , implemented using SQL statements, views, and stored procedures respectively.
DELIMITER //
create procedure proc_countNum(in columnId int, out rowsNo int)
begin
select count(*) into rowsNo from member where member_id=columnId;
end
call proc_countNum(1,@no);
select @no;
Method: View:
create view v_countNum as select member_id,count(*) as countNum from member group by
member_id
select countNum from v_countNum where member_id=1

27. The code for forward and backward web pages in js (forward: history.forward();=history.go(1); backward: history.back
();=history.go(-1); )

28. echo count(“abc”); What does it output?
Answer: 1
count — Count the number of cells in an array or the number of attributes in an object
int count (mixed$var [, int $mode ] ), if var is not an array Types or objects that implement the Countable interface will return 1, with the exception that if var is NULL the result is 0.
For objects, if SPL is installed, count() can be called by implementing the Countable interface. This interface has only one method count(), which returns the return value of the count() function.

29. There is a one-dimensional array that stores integer data. Please write a function to arrange them in order from large to small. High execution efficiency is required. and explain how to improve execution efficiency. (This function must be implemented by yourself, you cannot use php functions)
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);
?>
30. Please give an example of what method you use to speed up page loading during your development process
Answer: A server is required. Only open resources when needed, close server resources in a timely manner, add indexes to the database, and pages can generate static, pictures and other large files on a separate server. Use code optimization tools

31. What will the following code produce? Why?
$num =10;
function multiply(){
$num =$num *10;
}
multiply();
echo $num ;
Since the function multiply() does not specify $num as a global variable (such as global $num or $_GLOBALS['num']), the value of $num is 10.

32. What is the difference between static, public, private and protected in php class?
static is static and can be accessed by the class name.
public means global and can be accessed by both internal and external subclasses of the class.
private means private and can only be accessed within this class. Use;
protected means protected and can only be accessed in this class or subclass or parent class;

33. The difference between GET, POST and HEAD in the HTTP protocol?
HEAD: Only the header of the page is requested.
GET: Request the specified page information and return the entity body.
POST: Requests the server to accept the specified document as a new subordinate entity to the identified URI.
(1) HTTP defines different methods of interacting with the server, the most basic methods are GET and POST. In fact GET is suitable for most requests, while POST is reserved only for updating the site.
(2) When submitting a FORM, if Method is not specified, the default is a GET request. The data submitted in the Form will be appended to the url, separated from the url by ?. Alphanumeric characters are sent as they are, but spaces are converted to "+" signs, and other symbols are converted to %XX, where XX is the ASCII (or ISO Latin-1) value of the symbol in hexadecimal. The data submitted by the GET request is placed in the HTTP request protocol header, while the data submitted by POST is placed in the entity data;
The data submitted by GET can only be up to 1024 bytes, while POST does not have this limit.
(3) GET This is the most commonly used method for browsers to request to the server. The POST method is also used to transmit data, but unlike GET, when using POST, the data is not passed after the URI, but is passed as an independent row. At this time, a Content_length must also be sent. A header to indicate the length of the data, followed by a blank line, and then the actual data transferred. Web forms are usually sent using POST.

The above introduces the summary of PHP written test questions, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn