Home >Backend Development >PHP Tutorial >PHP interview questions and answers

PHP interview questions and answers

WBOY
WBOYOriginal
2016-07-25 08:53:451412browse
  1. $a = date("y-m-d h:i:s", strtotime("-1 day"));
  2. print_r($a);
Copy code

2. echo(), The difference between print() and print_r() (3 points)

  1. echo and print are not a function, but a language structure
  2. int print(string $arg), only one parameter
  3. echo arg1, arg2; can output multiple parameters and return void
  4. echo and print can only print Output string, cannot print out structure
  5. print_r can print out structure
  6. For example
  7. $arr = array("key"=>"value");
  8. print_r($arr);
Copy code

3. A template that can be used separately from html and php (1 point) smarty,phplib 4. What tools are used for version control? (1 point) svn,git,cvs 5. How to implement string flipping? (3 points) English: strrev($a) Chinese or other text: Chinese: gb2312, the code is encoded using gb2312

  1. function reverse($str)
  2. {
  3. $ret = "";
  4. len=mbstrwidth(str,"gb2312");
  5. for(i=0;i< len;i++ )
  6. {
  7. arr[]=mbsubstr(str, $i, 1, "gb2312");
  8. }
  9. return implode("", array_reverse($arr));
  10. }
  11. print_r(reverse("Hello") );
Copy code

6. Methods to optimize mysql database. (4 points, write more and get more) In terms of sentences: 1 Use indexes to increase query efficiency 2 Optimize query statements and improve index hit rate Database aspects involved: 1 Construct sub-databases and tables to improve the storage and expansion capabilities of the database 2 Use different storage engines as needed 7. The meaning of php (send 1 point) hypertext preprocessing language hypertext preprocessor 8. What is the function of MySQL to obtain the current time?, and the function of formatting the date is (2 points)

  1. current_timestamp()
  2. date_format()
  3. select date_format("2011-11-21 10:10:10", "%y-%m-%d");
Copy code

9. A method to intercept Chinese strings without garbled characters. (3 points) mb_substr($str, 1, 1, "gb2312"); 10. Have you ever used version control software? If so, what is the name of the version control software you used? (1 point) svn git 11. Have you ever used a template engine? If so, what is the name of the template engine you used? (1 point) smarty 12. Please briefly describe your most proud development work (4 points) xxx 13. For websites with large traffic, what methods do you use to solve the traffic problem? (4 points) 1 Effectively use cache to increase cache hit rate 2 Use load balancing 3 Use CDN to store and accelerate static files 4 ideas to reduce database usage 5 Check where the statistical bottlenecks are 14. Use PHP to write the code that displays the client IP and server IP (1 point) $_server["remote_addr"] $_server["server_addr"] 15. What is the difference between the include and require statements? To avoid including the same file multiple times, they can be replaced by (?) statements? (2 points) On failure: include generates a warning, while require generates an error interrupt directly. require is loaded before running include is loaded at runtime require_once include_once 16. How to modify the session survival time (1 point). session_set_cookie_params 17. There is a web page address, such as the Scripting School homepage: http://bbs.it-home.org/index.html, how to get its content? ($1 point) file_get_contents curl 18. In http 1.0, the meaning of status code 401 is (?); if the prompt "File not found" is returned, the header function can be used, and its statement is (?); (2 points) unauthorized header("http/1.0 404 not found"); In fast cgi: header("status: 404 not found"); 19. In PHP, heredoc is a special string, and its end mark must be? (1 point) appear in pairs $a = <

  1. $str = "jianfeng@126.com";
  2. regex="([a?z0?9.?]+)@([da?z.?]+).([a?z .]2,6)"; //Regular
  3. return preg_match(regex,str)
Copy code

26. Briefly describe how to get the current execution script path, including the obtained parameters. (2 minutes) $argc -- Get the number of parameters $argv -- Get the parameter list 27.How to modify the session survival time. (1 point) session_set_cookie_params 28. What is the function for popping up a dialog box in a js form? What is the function for obtaining input focus? (2 points) alert() confirm() prompt() focus() 29. What is the redirection function of js? How to introduce an external js file? (2 points) window.location.href="#" 30. What is the difference between foo() and @foo()? (1 point) @ represents all warnings to be ignored 31. How to declare a class named "myclass" without methods and attributes? (1 point) class myclass { } 32. How to instantiate an object named "myclass"? (1 point) $myclass = new myclass(); 33. How do you access and set the attributes of a class? (2 points)

  1. class a
  2. {
  3. public $name = "a";
  4. }
  5. $a = new a();
  6. n=a->name;
  7. print_r($n);
Copy code

34. What is the difference between mysql_fetch_row() and mysql_fetch_array? (1 point) mysql_fetch_array() is an extended version of mysql_fetch_row(). In addition to storing data in an array as a numerical index, you can also store data as an associative index, using the field name as the key.

  1. mysql_connect("localhost", "mysql_user", "mysql_password") or
  2. die("could not connect: " . mysql_error());
  3. mysql_select_db("mydb");
  4. $result = mysql_query("select id, name from mytable");
  5. while (row=mysqlfetcharray(result, mysql_assoc)) {
  6. printf ("id: %s name: %s", row["id"],row ["name"]);
  7. }
  8. mysql_free_result($result);
Copy code

35. What is the gd library used for? (1 point) Dynamic and open image processing library 36. Point out some ways to enter a piece of html code in php. (1 point)

  1. echo "{html}"
  2. echo <{html}
  3. eod;
Copy code

37. Which of the following functions can open a file to read and read the file? Write operation? (1 point) c (a) fget() (b) file_open() (c) fopen() (d) open_file() 38. Which of the following options does not add john to the users array? (1 point) b

  1.    (a) $users[] = 'john';
  2.   (b) array_add($users,'john');
  3.   (c) array_push($users,'john');
  4.   (d) $users ||= 'john';
Copy code

39. Will the following program be entered? (1 point) 10

  1. $num = 10;
  2.  function multiply(){
  3.  num=num * 10;
  4.  }
  5.  multiply();
  6.  echo $num;
Copy code

40. Use PHP to write a simple query to find all the content named "Zhang San" and print it out (2 points) Table name username tel content date Zhang San 13333663366 Graduated from college 2006-10-11 Zhang San 13612312331 Undergraduate degree 2006-10-15 Zhang Si 021-55665566 Graduated from technical secondary school 2006-10-15 Please complete the code according to the above question:

  1.   $mysql_db=mysql_connect("local","root","pass");
  2.   @mysql_select_db("db",$mysql_db);
  3. $sql = sprintf("select * from %s where username = '%s'",
  4. "Table name",
  5. "Zhang San");
  6. values=mysqlquery(sql);
  7. while(item=mysqlfetchqueryarray(values))
  8. {
  9. echo sprintf("Username: %s , phone %s, education: %s, graduation date: %s",
  10. item[′username′],item['tel'], item[′content′],item['date']
  11. );
  12. }
Copy code

41. How to use the following classes and explain what they mean? (3)

  1. class test{
  2.  function get_test($num){
  3.  num=md5(md5(num)."en");
  4.  return $num;
  5.   }
  6. }
  7. $test = new test();
  8. ret=test->get_test(11);
  9. print_r($ret);exit;
Copy code

Connect the 32-bit string a1 generated after md5 encoding of num with "en" and then perform md5 encoding again 42. Write the format of sql statement: insert, update, delete (4 points) Table name username tel content date Zhang San 13333663366 Graduated from college 2006-10-11 Zhang San 13612312331 Undergraduate degree 2006-10-15 Zhang Si 021-55665566 Graduated from technical secondary school 2006-10-15 (a) There is a new record (Xiao Wang 13254748547 graduated from high school 2007-05-06). Please use sql statement to add it to the table insert into table name values('Xiao Wang', '13254748547', 'High School Graduation', '2007-05-06') (b) Please use sql statement to update Zhang San’s time to the current system time update table name set date = getdate() where username = "Zhang San" (c) Please write to delete all records named Zhang Si delete from indicates where username = "Zhang Si" 43. Please write down the meaning of data type (int char varchar datetime text); what is the difference between varchar and char (2 points) int integer type char storage fixed length varchar storage variable length datetime time text stores variable length varchar is variable length char(20) fixed length 44. The mysq auto-increment type (usually the table id field) must be set to a (?) field (1 point) auto_increment 45. Write the output of the following program (1 point) ​$b=201; ​$c=40; a=b>$c?4:5; echo $a; ?> 4

46. Is there a function to detect whether a variable is set? What is the function to detect whether it is empty? (2 points) isset() empty() 47. What is the function to obtain the total number of query result sets? (1 point) mysql_num_rows() 48.

  1. $arr = array('james', 'tom', 'symfony'); Please print out the value of the first element (1 point)
  2. print_r($arr[0]);
  3. reset( $arr);
  4. print_r(current($arr));
  5. print_r(array_shift($arr)); (Edited and organized by Programmer’s Home bbs.it-home.org)
Copy the code

49. Please Separate the values ​​of the array in question 41 with ',' signs and merge them into string output (1 point) implode 50. a=′abcdef′; Please take out the value of a and print out the first letter (1 point) $a[0]; substr($a, 0, 1); 51. Can php be connected to databases such as sql server/oracle? (1 point) Can There are ready-made libraries 52. Please write the php5 permission control modifier (3 points) public private protected 53. Please write down the constructor and destructor of php5 (2 points) public function __construct() { } public function __destruct() { } Programming questions ​ 1. 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 You need to remove php or .php

  1. $url = "http://www.sina.com.cn/abc/de/fg.php?id=1";
  2. arr=parseurl(url);
  3. patharr =pathinfo(arr['path']);
  4. print_r($patharr['extension']);
Copy code

3. Write a function to calculate the relative paths of two files Such as $a = '/a/b/c/d/e.php'; $b = '/a/b/12/34/c.php'; Calculate the relative path of b relative to a should be http://www.cnblogs.com/12/34/c.php will be added

  1. $a = '/a/b/c/d/e.php';
  2. $b = '/a/b/12/34/c.php';
  3. / /Get the relative path of path relative to conpath
  4. function sgetrelativepath(path,conpath)
  5. {
  6. patharr=explode("/",path);
  7. conpatharr=explode("/",conpath);
  8. $dismatchlen = 0;
  9. for(i=0;i < count(patharr);i++)
  10. {
  11. if(conpatharr[i] != patharr[i])
  12. {
  13. dismatchlen=count(patharr) - $i;
  14. arrleft=arrayslice( patharr, $i);
  15. break;
  16. }
  17. }
  18. ret=strrepeat("../",dismatchlen).implode("/", $arrleft);
  19. return $ret;
  20. }
  21. print_r(sgetrelativepath(b ,a));
Copy code

3. Write a function that can traverse all files and subfolders in a folder.

  1. function agetallfile($folder)
  2. {
  3. $afilearr = array();
  4. if(is_dir($folder))
  5. {
  6. handle=opendir(folder);
  7. while(( file=readdir(handle)) !== false)
  8. {
  9. //If it is. or.., skip
  10. if(file=="."||file == "..")
  11. {
  12. continue;
  13. }
  14. if(is_file(folder."/".file))
  15. {
  16. afilearr[]=file;
  17. }
  18. else if(is_dir(folder."/".file))
  19. {
  20. afilearr[file] = agetallfile(folder."/".file);
  21. }
  22. }
  23. closedir($handle);
  24. }
  25. return $afilearr;
  26. }
  27. $path = "/home/test/sql";
  28. print_r(agetallfile($ path));
Copy code


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