Home  >  Article  >  PHP Framework  >  Meet the most difficult thinkphp interview question in history

Meet the most difficult thinkphp interview question in history

coldplay.xixi
coldplay.xixiforward
2020-08-10 17:05:264133browse

Meet the most difficult thinkphp interview question in history

[Related topic recommendations: 2020 thinkphp interview questions and answers (complete collection)]

1. Write the string' 0', convert the Boolean type in php and js respectively, is the value true or false?

Answer:

In php

$a = '0';
$a = (bool) $a;
var_dump($a);

The value is false

In js

Official instructions :Note:If the value parameter is omitted, or is 0, -0, null, , false, undefined, or NaN, the object is set to false. Otherwise it is set to true (even with the string false)!

Test:

<script type="text/javascript">
        var a=&#39;0&#39;;
        if(a){
                alert(1);
        }else{
                alert(0);
        }
</script>  值为true

2. Briefly describe the methods used to optimize mysql and the tools and commands used

Answer:

1. Use pgcc (pentium gcc) compiler. This compiler (http://www.goof.com/pcg/) is optimized for programs running on Pentium processor systems. Use pgcc to compile mysql source code. The overall performance can be Increase by 10%. Of course, if your server does not use a Pentium processor, you do not need to use it, because it is specially designed for Pentium systems.

2. Compile mysql only with the character set you want to use. Mysql currently provides up to 24 different character sets, allowing users around the world to insert or view data in tables in their own languages. By the way, the MySQL installation depends on these character sets, however, the best option is to choose the one you need. For example, to disable all character sets except the latin1 character set:

%>./configure -with-extra-charsets=none [--other-configuration-options]

3 .Compile mysqld into a static executable file. Compiling mysqld into a static executable file without shared libraries can also achieve better performance. Mysqld can be compiled statically by specifying the following options during configuration.

%>./configure -with-mysqld-ldflags=-all-static [--other-configuration-options]

4. Configuration sample The following configuration commands are commonly used Improve performance:

%>cflags="-o6 -mpentiumpro -fomit-frame-pointer" cxx=gcc cxxflags="-o6 -mpentiumpro -fomit-frame-pointer -felide-constructors -fno-exceptions -fno-rtti" ./configure --prefix=/usr/local --enable-assembler --with-mysqld-ldflags=-all-static --disable-shared

5. It is important to ensure that correct compilation is used, but this is only the first step to success. Configuring numerous mysql variables also plays a key role in the normal operation of the server.

You can store the assignments of these variables in a configuration file to ensure that they take effect every time you start mysql. This configuration file is the my.cnf file. Mysql has provided several samples of my.cnf files, which can be found in the /usr/local/mysqld/share/mysql/ directory. The files are named my-small.cnf, my-medium.cnf, my-large.cnf, and my-huge.cnf respectively, with size descriptions found in the header describing the system type to which the configuration files apply.

If you are running mysql on a system with relatively little memory and only use it occasionally, then my-small.cnf is ideal because it instructs mysqld to use only the minimum resources. Similarly, if you plan to build an e-commerce supermarket and the system has 2g of memory, then you may want to use the mysql-huge.cnf file.

In order to utilize one of these files, you need to make a copy of the file that best suits your needs and rename it my.cnf. You can choose to use one of the three scopes of the configuration file: global: Copy the my.cnf file to the /etc directory of the server, which makes the variables in the configuration file act globally, that is, for the mysql database server on all servers. efficient.

local: Copy the my.cnf file to the [mysql-install-dir]/var/ directory to make my.cnf act on a specific server. [mysql-install-dir] indicates the mysql installation directory.

user: You can then limit the effect to specific users and copy my.cnf to the user's root directory. How to set these variables in my.cnf? Furthermore, which variables you can set.

Although the variables used are relatively common to the MySQL server, each variable has a more specific relationship with certain components of MySQL. For example, the variable max_connects is classified under the mysqld category. You can find out by executing the following command: %>/usr/local/mysql/libexec/mysqld --help It displays a large number of options and variables related to mysqld.

You can easily find the variables under this line of text:

possible variables for option --set-variable (-o) are

Then you can set those variables in my.cnf as follows:

set-variable = max_connections=100

It sets The maximum number of concurrent connections for mysql server is 100. Be sure to insert the variable settings in the my.cnf file under the [mysqld] heading.

Transaction security dbd berkeley The db (dbd) table is a table that supports transaction processing, developed by sleepycat Software Company (http://www.sleepycat.com). It provides a long-awaited feature for MySQL users - transaction control.

Transaction controls are an extremely valuable feature in any database system because they ensure that a set of commands can be executed successfully.

Non-transaction security heap The heap table is the fastest table in mysql to access data. This is because they use a hash index stored in dynamic memory. Another important point is that if mysql or the server crashes, the data will be lost.

isam The isam table was the default table type in early mysql versions until myiasm was developed. It is recommended not to use it anymore.

  merge merge是一个有趣的新类型,在3.23.25之后出现。一个merge表实际上是一个相同myisam表的集合,合并成一个表,主要是为了效率原因。这样可以提高速度、搜索效率、修复效率并节省磁盘空间。 

  myiasm 这是mysql的缺省表类型。它基于iasm代码,但有很多有用的扩展。myiasm比较好的原因: myiasm表小于iasm表,所以使用较少资源。 myiasm表在不同的平台上二进制层可移植。 更大的键码尺寸,更大的键码上限。 

指定表类型 你可在创建表时指定表的类型。下例创建一个heap表: 

mysql>create table email_addresses type=heap ( ->email char(55) not null, ->name char(30) not null, ->primary key(email) ); 

bdb表需要一些配置工作,参见http://www.mysql.com/doc/b/d/bdb_overview.html。 

更多的表类型 为了使mysql管理工作更有趣,即将发布的mysql 4.0将提供两种新的表类型,称为innobase和gemeni。 

show 你可能有兴趣知道mysql服务器究竟更了什么,下列命令给出一个总结: 、

mysql>show status; 

它给出了一个相当长的状态变量及其值的列表。有些变量包含了异常终止客户的数量、异常终止连接的数量、连接尝试的次数、最大并发连接数和大量其他有用的信息。这些信息对找出系统问题和低效极具价值。 show还能做更多的事情。它可以显示关于日志文件、特定数据库、表、索引、进程和权限表中有价值的信息。详见mysql手册。 

explain 当你面对select语句时,explain解释select命令如何被处理。这不仅对决定是否应该增加一个索引,而且对决定一个复杂的join如何被mysql处理都是有帮助的。 

optimize optimize语句允许你恢复空间和合并数据文件碎片,对包含变长行的表进行了大量更新和删除后,这样做特别重要。optimize目前只工作于myiasm和bdb表。

5. 一张mysql大数据表有几千万数据,但有一自增id字段,且为主键,要遍历此表的所有数据,写出有效的方法和sql(禁止使用limit n,m)。

答:

案例分析:数据表 collect ( id, title ,info ,vtype) 就这4个字段,其中 title 用定长,info 用text, id 是逐渐,vtype是tinyint,vtype是索引。10万数据的效果。

select id,title from collect limit 1000,10; 基本上0.01秒就OK

select id,title from collect limit 90000,10;                8-9秒完成

select id from collect order by id limit 90000,10;  0.04秒就OK。 为什么?因为用了id主键做索引当然快。

改进方法:select id,title from collect where id>=(select id from collect order by id limit 90000,1) limit 10;;

select id from collect where vtype=1 order by id limit 90000,10; 很慢,用了8-9秒!

vtype 做了索引了啊?怎么会慢呢?vtype做了索引是不错,你直接 select id from collect where vtype=1 limit 1000,10; 是很快的,基本上0.05秒,可是提高90倍,从9万开始,那就是0.05*90=4.5秒的速度了。

分表的方法:

建一个索引表: t (id,title,vtype) 并设置成定长,然后做分页,分页出结果再到 collect 里面去找info 。 是否可行呢?实验下就知道了。

10万条记录到 t(id,title,vtype) 里,数据表大小20M左右。用select id from t where vtype=1 order by id limit 90000,10; 很快了。基本上0.1-0.2秒可以跑完。

其实这样做还是全表扫描,只是因为数据量小,只有10万才快。OK, 来个疯狂的实验,加到100万条,测试性能。

加了10倍的数据,马上t表就到了200多M,而且是定长。还是刚才的查询语句,时间是0.1-0.2秒完成!分表性能没问题?错!因为我们的limit还是9万,所以快。给个大的,90万开始select id from t where vtype=1 order by id limit 900000,10; 看看结果,时间是1-2秒!

怪不得有人说 discuz到了100万条记录就会很慢,我相信这是真的,这个和数据库设计有关!

开始测试结论是: 30万数据,用分表法可行,超过30万他的速度会慢道你无法忍受!当然如果用分表+我这种方法,那是绝对完美的。但是用了我这种方法后,不用分表也可以完美解决!

不分表的方法:

复合索引!search(vtype,id) 这样的索引

select id from collect where vtype=1 limit 90000,10; 非常快!0.04秒完成!

再测试: select id ,title from collect where vtype=1 limit 90000,10; 非常遗憾,8-9秒,没走search索引!

再测试:search(id,vtype),还是select id 这个语句,也非常遗憾,0.5秒。

综上:如果对于有where 条件,又想走索引用limit的,必须设计一个索引,将where 放第一位,limit用到的主键放第2位,而且只能select 主键!

http://blog.csdn.net/zqtsx/article/details/8929625

6. Mysql有一个联合索引KEY(a,b,c),a为tinyint类型(长度为1),b为mediumint类型(长度为3),c为int类型(长度4)。写出条件where a=1 and c=3所使用到此索引的长度。

答:网上没找到答案。

7. InnoDB引擎中,如何开启一个排它的读写锁。

答:

lock tables test write;         //经过测试启用另一个mysql回话都不能读和写test表,表明此答案是正确的

8. 简述json和jsonp的区别以及优缺点,应用场景。

答:说到AJAX就会不可避免的面临两个问题,第一个是AJAX以何种格式来交换数据?第二个是跨域的需求如何解决?这两个问题目前都有不同的解决方案,比如数据可以用自定义字符串或者用xml来描述,跨域可以通过服务器端代理来解决。

但到目前为止最被推崇或者说首选的方案还是用JSON来传数据,靠JSONP来跨域。

http://www.cnblogs.com/dowinning/archive/2012/04/19/json-jsonp-jquery.html

9. 以下js代码有什么潜在问题(点击每个div弹出相应的序号)?该如何解决?

     var es=document.getElementsByTagName(‘div’);
     for(var i in es){
          es.onclick=function(){
                alert(i);
          }
     }

答:没找到答案

10. 写一个遍历目录下所有文件以及子目录的函数

答:

<?php
    function traverse($path = &#39;.&#39;) {
    $current_dir = opendir($path);    //opendir()返回一个目录句柄,失败返回false
    while(($file = readdir($current_dir)) !== false) {    //readdir()返回打开目录句柄中的一个条目
            $sub_dir = $path . DIRECTORY_SEPARATOR . $file;    //构建子目录路径
        if($file == &#39;.&#39; || $file == &#39;..&#39;) {
            continue;
        } else if(is_dir($sub_dir)) {    //如果是目录,进行递归
            echo &#39;Directory &#39; . $file . &#39;:<br>&#39;;
            traverse($sub_dir);
        } else {    //如果是文件,直接输出
            echo &#39;File in Directory &#39; . $path . &#39;: &#39; . $file . &#39;<br>&#39;;
        }
    }
  }

11.将以下jquery选择符翻译成中文描述:

$(“#d div.cls input[type=text][name^=text_]”);

答:

12. Cookie、session的联系和区别,多台web服务器如何共享session

答:

Session采用键值对 , 也就是说ID存放客户端 , 而值放在服务器端 , 是通过用户的ID去找服务器上对应的值 , 这种方式值放置在服务器端 ,有个时间限制 ,时间到则服务器自动释放.

Cookies则有两种方法 , 一种方法是把值保存在浏览器的变量中 , 当浏览器关闭时结束 , 另一种方法是保存在硬盘中 , 只要时间不过期 , 下次还可使用.

Session 是由应用服务器维持的一个服务器端的存储空间,用户在连接服务器时,会由服务器生成一个唯一的SessionID,用该SessionID 为标识符来存取服务器端的Session存储空间。而SessionID这一数据则是保存到客户端,用Cookie保存的,用户提交页面时,会将这一 SessionID提交到服务器端,来存取Session数据。这一过程,是不用开发人员干预的。所以一旦客户端禁用Cookie,那么Session也 会失效。

服务器也可以通过URL重写的方式来传递SessionID的值,因此不是完全依赖Cookie。如果客户端Cookie禁用,则服务器可以自动通过重写URL的方式来保存Session的值,并且这个过程对程序员透明。

可以试一下,即使不写Cookie,在使用request.getCookies();取出的Cookie数组的长度也是1,而这个Cookie的名字就是JSESSIONID,还有一个很长的二进制的字符串,是SessionID的值。

共享session的方法:http://blog.csdn.net/heiyeshuwu/article/details/521010

13. 有以下数据

  • $_GET[‘int’] 整数

  • $_GET[‘float’] 浮点

  • $_GET[‘text’] 文本,不需要展示html

  • $_GET[‘content’] 文本,需要展示html

以上数据该各用什么方法过滤并保证安全?数据入库之前需要做什么处理?

14. 写出匹配,除div,span,img以外的任何html标签的正则(不需要匹配子标签)。

15. 写出正则表达式中,以下关键字的含义?

$ +? ^ [^] ?<g1> ?! ?<! ?:

16. 写出多进程并发同时,读写同一文件时,能保证文件内容完整的代码(读写文件的代码)

17. 面向对象中,self与$this的区别是?

答:this是指向当前对象的指针(姑且用C里面的指针来看吧),self是指向当前类的指针,parent是指向父类的指针

18. 写出linux中,查找后缀名为.txt的,且内容包含delete的文件,找出并删除的命令是?

find / -type f -name "*.text" | xargs grep "delete"  -delete

相关学习推荐:thinkphp

The above is the detailed content of Meet the most difficult thinkphp interview question in history. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:thinkphp. If there is any infringement, please contact admin@php.cn delete