2020 latest PHP interview 100 questions (extra articles)

1、Parse error: syntax error, unexpected T_STRING in /website/index.php on line 18

Syntax error in line 18, check the grammar

2、Warning:fopen(welcome.txt) [function.fopen]: failed to open stream: No such file or directory in /website/index.php on line 2

The welcome.txt file is not found, check if the file exists

Click to join my penguin group

3. Use PHP to print out the time of the previous day, the print format is 22:21:21, May 10, 2007

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. Assuming 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.

<script>
function go2b(){
    
    
 window.location = “b.html”;
 window.close();
}
setTimeout(go2b(),5000 ); //5秒钟后自动执行go2b()
</script>

//正在浏览当前页面用户的 IP 地址:127.0.0.1
echo $_SERVER["REMOTE_ADDR"].<br />;
//查询(query)的字符串(URL 中第一个问号 ? 之后的内容):id=1&bi=2
echo $_SERVER["QUERY_STRING"].<br />;
//当前运行脚本所在的文档根目录:d:inetpubwwwroot
echo $_SERVER["DOCUMENT_ROOT"].<br />;

6. In HTTP 1.0, the meaning of the status code 401 is unauthorized ____; if the prompt "File not found" is returned, the header function can be used, and the sentence is header("HTTP/1.0 404 Not Found");

401 means unauthorized; header("HTTP/1.0 404 Not Found");

7. Add John to the users array?

$users[] = ‘john’; array_push($users,‘john’);

8. What is the function of error_reporting in PHP?

error_reporting() sets PHP's error reporting level and returns the current level.

9. How to modify the survival time of SESSION (1 point).

Method 1: Set session.gc_maxlifetime in php.ini to 9999 and restart apache

方法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);

10. There is a web page address, such as the homepage of PHP Development Resource Network: http://www.phpres.com/index.html, how to get its content? ($1 point)

Method 1 (for PHP5 and higher):

$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”);

11. Write a function that is as efficient as possible to extract the file extension from a standard URL

For example: http://www.sina.com.cn/abc/de/fg.php?id=1 need to 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$pos11);
} else {
    
    
return substr($url,$pos1);
}
}

12. Use more than five ways to get the extension of a file

Requirements: dir/upload.image.jpg, find out .jpg or jpg,

You must use PHP's own processing function for processing. The method cannot be obviously repeated. It can be encapsulated into functions get_ext1( filename), getext 2 (file_name), get_ext2(filena m e ) ,getext2(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),.)));
}

13、

$str1 = null;
$str2 = false;
echo $str1==$str2 ? ‘相等’ : ‘不相等’;
$str3 =;
$str4 = 0;
echo $str3==$str4 ? ‘相等’ : ‘不相等’;
$str5 = 0;
$str6 =0;
echo $str5===$str6 ? ‘相等’ : ‘不相等’;
?>

Answer: Equal equal not equal

14. What is the main difference between varchar and char in MySQL database? The search efficiency of that kind of field is higher, why?

Varchar is variable length, saving storage space, char is a fixed length. The search efficiency is faster than the varchar type, because varchar is a non-fixed length, and the length must be searched first, and then the data is extracted. There is one more step than the char fixed-length type, so the efficiency is lower

15. Please use JavaScript to write three methods to generate an Image tag (hint: consider from the perspective of method, object, and HTML)

(1)var img = new Image();

(2)var img = document.createElement(“image”)

(3)img.innerHTML = “”

16. Please describe the two most significant differences between XHTML and HTML

(1) XHTML must be mandatory to specify the document type DocType, HTML does not need

(2) All tags in XHTML must be closed, HTML is more arbitrary

17. Write the names of more than three MySQL database storage engines (hint: case insensitive)

More than a dozen engines including MyISAM, InnoDB, BDB (Berkeley DB), Merge, Memory (Heap), Example, Federated, Archive, CSV, Blackhole, MaxDB, etc.

18. Find the difference between two dates, such as the date difference between 2007-2-5 and 2007-3-6

method one:

<?php
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 Two:

<?php
$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 three: echo abs(strtotime("2007-2-1")-strtotime("2007-3-1"))/60/60/24 calculate the time difference

19/ Please write a function to achieve the following functions:

The string "open_door" is converted to "OpenDoor", and "make_by_id" is converted 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 Two:

$str=”make_by_id!;
$expStr=explode(“_”,$str);
for($i=0;$i
echo ucwords($expStr[$i]);
}

方法三:echo str_replace(‘ ‘,”,ucwords(str_replace(‘_’,’ ‘,’open_door’)));

20. There are multiple records of Id in a table, find out all the records of this id, and display the total number of records, and use SQL statements, views, and stored procedures to implement 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

The code for forward and backward web pages in js

前进: history.forward();=history.go(1);

后退: history.back();=history.go(-1);

echo count("abc"); What is output?

Answer: 1

count — Count the number of elements in an array or the number of attributes in an object

int count (mixed$var [, int $mode] ), if var is not an array type or an object that implements the Countable interface, 1 will be returned, with one exception, if var is NULL, the result will be 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.

21. There is a one-dimensional array that stores plastic data. Please write a function to arrange them in descending order. High execution efficiency is required. And explain how to improve execution efficiency. (This function must be implemented by yourself, php function cannot be used)

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);

22. Please give an example of what method to use to speed up page loading during your development process

Only open the server resources when they are used, close the server resources in time, add indexes to the database, pages can generate static, pictures and other large files on a separate server. Use code optimization tools.

23. 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.

24. The difference between GET, POST and HEAD in HTTP protocol?

HEAD: Only request the header of the page.

GET: Request the specified page information and return the entity body.

POST: Request 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, and reserved POST is only used to update the site.

(2) When the FORM is submitted, if the Method is not specified, the default is a GET request. The data submitted in the Form will be appended to the url and 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 the POST is placed in the entity data;

The data submitted by GET method can only have a maximum of 1024 bytes, while POST does not have this limitation.

(3) GET This is the most commonly used method for browsers to request 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 as a separate line to be passed, and a Content_length must be sent at this time. The header indicates the length of the data, followed by a blank line, and then the actual data transmitted. Forms on web pages are usually sent using POST.

Pay attention, don't get lost

Alright, everyone, the above is the entire content of this article. The people who can see here are all talents . As I said before, there are a lot of technical points in PHP, because there are too many, it is really impossible to write, and you will not read too much after writing it, so I will organize it into PDF and documents here, if necessary Can

Click to enter the secret code: PHP+「Platform」

Insert picture description here

Insert picture description here


For more learning content, please visit the [Comparative Standard Factory] excellent PHP architect tutorial catalog, as long as you can read it to ensure that the salary will rise a step (continuous update)

The above content hopes to help everyone . Many PHPers always encounter some problems and bottlenecks when they are advanced. There is no sense of direction when writing too much business code. I don’t know where to start to improve. I have compiled some information about this, including But not limited to: distributed architecture, high scalability, high performance, high concurrency, server performance tuning, TP6, laravel, YII2, Redis, Swoole, Swoft, Kafka, Mysql optimization, shell scripts, Docker, microservices, Nginx, etc. Many knowledge points, advanced advanced dry goods, can be shared with everyone for free, and those who need can join my PHP technology exchange group

Guess you like

Origin blog.csdn.net/weixin_49163826/article/details/108968606