Summary of PHP written test questions, summary of PHP test questions_PHP tutorial

WBOY
Release: 2016-07-12 08:59:48
Original
848 people have browsed it

Summary of PHP written test questions, summary of PHP test questions

1. What function would you use to capture remote images to the local?

fsockopen, A

2. Use the least amount of code to write a function that finds the maximum value 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 can you solve it?

Javascript does not support two-dimensional array definition, you can use arr[0 ] = new array() to solve

5. Assume a.html and b.html are in the same folder, use javascript to automatically jump to b after opening a.html for five seconds. html.


function go2b(){
window.location = “b.html”;
window.close();
}

setTimeout( “go2b ()",5000 ); //Automatically execute go2b() after 5 seconds




6. //IP address of the user browsing the current page: 127.0.0.1
echo $_SERVER["REMOTE_ADDR"].”
”;
//Query string (the content after the first question mark? in the URL): 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 the 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 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 the code to display the client IP and 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 SESSION survival time (1 minute).

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. There is a web page address, such as the homepage of the PHP Development Resource Network: 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 by value and passing by reference 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 scope of the function will be ignored outside the function

Pass by reference: Function Any changes to values ​​within the scope are also reflected outside the function

Pros and Cons: When passing by value, PHP must copy the value. Especially for large strings and objects, this can be a costly operation.

Passing by reference does not require copying the value, which is very good for improving performance.



16. Write a function to extract the file extension from a standard URL as efficiently as possible

For example: http://www.sina.com .cn/abc/de/fg.php?id=1 Need to remove 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 ways to get the extension of a file

Requirements: dir/upload.image.jpg, find .jpg or jpg,
must be processed using PHP’s own processing function, the method cannot be obviously repeated, and can be encapsulated into a function, 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. In MySQL database What is the main difference between field types varchar and char? Which field is more efficient in searching, why?
Varchar is of variable length, saving storage space, while char is of 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 ways to generate 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 points that are most significant about XHTML and HTML The difference
(1)XHTML must forcefully specify the document type DocType, HTML does not need to
(2)XHTML all tags must be closed, HTML is more casual

22. Write a sorting algorithm, which can be Bubble sort or quick sort, assuming 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 sorting)
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 the date difference between 2007-2-5 ~ 2007-3-6
Method 1:
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", and convert "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 out all the records of this id and display the total number of records. Use SQL statements and views,
Stored procedures are implemented separately.
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. Code for web page forward and backward in js (forward: history.forward();=history.go(1) ; Back: history.back
();=history.go(-1); )



28. echo count(“abc”); What does it output?
Answer: 1

count — Count the number of cells in the array or the number of attributes in the object

int count (mixed$var [, int $mode ] ), if var is not an array type or Objects that implement the Countable interface will return 1, with one exception, 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 and 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 methods you use to speed up your development process. The loading speed of the page
Answer: Open it only when server resources are needed, close server resources in time, add indexes to the database, and the page 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;
Because the function multiply() does not specify $num as a global variable (such as global $num or $_GLOBALS['num ']), so the value of $num is 10.



32. What are the differences between static, public, private and protected in php class?

static is static, the class name can be accessed

public means global, All subclasses inside and outside the class can access;

private means private, and can only be used within this class;

protected means protected, and can only be used in this class, subclasses, or parent classes. Access;



33. What are the differences 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, and the data submitted in the Form will be appended to the url, separated from the url by ?. Alphanumeric characters are sent as is, 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.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1096606.htmlTechArticleSummary of PHP written test questions, summary of PHP test questions 1. Capture remote images to local, what function would you use? fsockopen , A 2. Use the least amount of code to write a function that finds the maximum value of 3 values. function($a,$b,$c...
Related labels:
php
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template