Home Backend Development PHP Tutorial 33道php常见面试题及答案_PHP

33道php常见面试题及答案_PHP

May 30, 2016 am 08:45 AM
php interview questions

1.在PHP中,当前脚本的名称(不包括路径和查询字符串)记录在预定义变量(1)中;而链接到当前页面的URL记录在预定义变量(2)中。

代码如下:


答:echo $_SERVER['PHP_SELF']; echo $_SERVER["HTTP_REFERER"];

2.执行程序段将输出(3)。

代码如下:


答:0

3.在HTTP 1.0中,状态码 401 的含义是(4);如果返回“找不到文件”的提示,则可用 header 函数,其语句为(5)。

代码如下:


答:(4)未授权 (5) header("HTTP/1.0 404 Not Found");

4.数组函数 arsort 的作用是(6);语句 error_reporting(2047)的作用是(7)。

代码如下:


答:(6)对数组进行逆向排序并保持索引关系  (7)All errors and warnings

5.写出一个正则表达式,过虑网页上的所有JS/VBS脚本(即把标记及其内容都去掉):(9)。

代码如下:


答:/].*?>.*?/si

6.以Apache模块的方式安装PHP,在文件http.conf中首先要用语句(10)动态装载PHP模块,

然后再用语句(11)使得Apache把所有扩展名为php的文件都作为PHP脚本处理。

代码如下:


答:(10) LoadModule    php5_module "D:/xampp/apache/bin/php5apache2.dll"
   (11) AddType application/x-httpd-php-source .phps
        AddType application/x-httpd-php .php .php5 .php4 .php3 .phtml

7.语句 include 和 require 都能把另外一个文件包含到当前文件中,它们的区别是(12);为了避免多次包含同一文件,可以用语句(13)来代替它们。

代码如下:


答:(12) 发生异常时include产生警告require产生致命错误  (13) require_once()/include_once()

8.类的属性可以序列化后保存到 session 中,从而以后可以恢复整个类,这要用到的函数是(14)。

代码如下:


答:serialize() /unserialize()

9.一个函数的参数不能是对变量的引用,除非在php.ini中把(15)设为on.

代码如下:


答:allow_call_time_pass_reference

10.SQL 中LEFT JOIN的含义是(16)。

如果 tbl_user记录了学生的姓名(name)和学号(ID),

tbl_score记录了学生(有的学生考试以后被开除了,没有其记录)的学号(ID)和考试成绩(score)以及考试科目(subject),

要想打印出各个学生姓名及对应的的各科总成绩,则可以用SQL语句(17)。

代码如下:


答:(16) 自然左外连接
     (17) select name , count(score) as sum_score from tbl_user left join tbl_score on tbl_user.ID=tbl_score.ID  group by tbl_user.ID

11..在PHP中,heredoc是一种特殊的字符串,它的结束标志必须(18)。

代码如下:


答:结束标识符所在的行不能包含任何其它字符除";"

12.用PHP打印出前一天的时间格式是2006-5-10 22:21:21

代码如下:


答:echo date('Y-m-d H:i:s', strtotime('-1 day'));

13.echo(),print(),print_r()的区别

代码如下:


答:echo是语言结构,无返回值;print功能和echo基本相同,不同的是print是函数,有返回值;print_r是递归打印,用于输出数组对象

14.如何实现字符串翻转?

代码如下:


答:.用strrev函数呗,不准用PHP内置的就自己写:
strrev($str)
{
    $len=strlen($str);
    $newstr = '';
    for($i=$len;$i>=0;$i--)
    {
        $newstr .= $str{$i};
    }
    return $newstr;
}

15.实现中文字串截取无乱码的方法。

代码如下:


答:mb_substr()

16.使用php写一段简单查询,查出所有姓名为“张三”的内容并打印出来

表名User

Name          Tel              Content         Date

张三        13333663366        大专毕业       2006-10-11

张三        13612312331        本科毕业       2006-10-15

张四        021-55665566       中专毕业       2006-10-15

代码如下:


答:SELECT Name,Tel,Content,Date FROM User WHERE Name='张三'

17.如何使用下面的类,并解释下面什么意思?

class test
{
    Get_test($num)
    {
        $num=md5(md5($num)."En");
        return $num;
    }
}

答:用法:

代码如下:


$get_test = new test();
$result = $get_test->Get_test(2);

将$num变量进行两次md5后返回,第2次的md5中的参数,在第一次md5($num)后多加了En

18.使用五种以上方式获取一个文件的扩展名

要求:dir/upload.image.jpg,找出 .jpg 或者 jpg ,

代码如下:


答:使用五种以上方式获取一个文件的扩展名
1)
get_ext1($file_name)
{
    return strrchr($file_name, '.');
}
2)
get_ext2($file_name)
{
    return substr($file_name, strrpos($file_name, '.'));
}
3)
get_ext3($file_name)
{
    return array_pop(explode('.', $file_name));
}
4)
get_ext4($file_name)
{
    $p = pathinfo($file_name);
    return $p['extension'];
}
5)
get_ext5($file_name)
{
    return strrev(substr(strrev($file_name), 0, strpos(strrev($file_name), '.')));
}

19.如何修改SESSION的生存时间

这个函式库让你处理和显示各式格式的图档,它的另一个常见用途是制作所图档。GD 以外的另一个选择是 ImageMagick,但这个函式库并不内建于 PHP 之中,必须由系统管理员安装在伺服器上答:其实 Session 还提供了一个函数 session_set_cookie_params(); 来设置 Session 的生存期的,该函数必须在 session_start() 函数调用之前调用:

<?php
// 保存一天
$lifeTime = 24 * 3600;
session_set_cookie_params($lifeTime);
session_start();
$_SESSION["admin"] = true;
?>

20. 请写一个函数,实现以下功能: 字符串“open_door” 转换成 “OpenDoor”、”make_by_id” 转换成 ”MakeById”。

代码如下:


答:
Function test($str){
$arr1=explode('_',$str);
//$arr2=array_walk($arr1,ucwords( ));
$str = implode(' ',$arr1);
return ucwords($str);
}
$aa='open_door';
echo test($aa);
?>

21. 如何用php的环境变量得到一个网页地址的内容?ip地址又要怎样得到?

代码如下:


答:$_SERVSR[‘REQUEST_URI']
$_SERVER[‘REMOTE_ADDR']

22.求两个日期的差数,例如2007-2-5 ~ 2007-3-6 的日期差数

代码如下:


答:(strtotime(‘2007-3-6')-strtotime(‘2007-2-5'))/3600*24

23.表中有A B C三列,用SQL语句实现:当A列大于B列时选择A列否则选择B列,当B列大于C列时选择B列否则选择C列。

代码如下:


答:select case when A>B then A else B end,
       case when B>C then B else C end
From test

24.请简述项目中优化sql语句执行效率的方法,从哪些方面,sql语句性能如何分析?

代码如下:


答:(1)选择最有效率的表名顺序
(2)WHERE子句中的连接顺序
(3)SELECT子句中避免使用‘*'
(4)用Where子句替换HAVING子句
(5)通过内部函数提高SQL效率
(6)避免在索引列上使用计算。
(7)提高GROUP BY 语句的效率, 可以通过将不需要的记录在GROUP BY 之前过滤掉。

25.mysql_fetch_row() 和 mysql_fetch_array() 有什么分别?

代码如下:


mysql_fetch_row() 把数据库的一列储存在一个以零为基数的阵列中,第一栏在阵列的索引 0,第二栏在索引 1,如此类推。mysql_fetch_assoc() 把数据库的一列储存在一个关联阵列中,阵列的索引就是栏位名称,例如我的数据库查询送回“first_name”、“last_name”、 “email”三个栏位,阵列的索引便是“first_name”、“last_name”和“email”。mysql_fetch_array() 可以同时送回 mysql_fetch_row() 和 mysql_fetch_assoc() 的值。

26.下面的代码用来做什么?请解释。

$date='08/26/2003';print ereg_replace("([0-9]+)/([0-9]+)/([0-9]+)","\\2/\\1/\\3",$date);

代码如下:


这是把一个日期从 MM/DD/YYYY 的格式转为 DD/MM/YYYY 格式。我的一个好朋友告诉我可以把这个正规表达式拆解为以下的语句,对于如此简单的表示是来说其实无须拆解,纯粹为了解说的方便:
// 对应一个或更多 0-9,后面紧随一个斜号$regExpression = "([0-9]+)/";// 应一个或更多 0-9,后面紧随另一个斜号$regExpression .= "([0-9]+)/";// 再次对应一个或更多 0-9$regExpression .= "([0-9]+)";至于 \\2/\\1/\\3 则是用来对应括号,第一个括号对的是月份,

27.GD 函式库用来做什么?

代码如下:


答:这个函式库让你处理和显示各式格式的图档,它的另一个常见用途是制作所图档。GD 以外的另一个选择是 ImageMagick,但这个函式库并不内建于 PHP 之中,必须由系统管理员安装在伺服器上

28.请举例说明在你的开发过程中用什么方法来加快页面的加载速度

代码如下:


   答:要用到服务器资源时才打开,及时关闭服务器资源,数据库添加索引,页面可生成静态,图片等大文件单独服务器。使用代码优化工具啦

29.防止SQL注射漏洞一般用__addslashes___函数。

30.PHP中传值和传引用、传地址的区别是什么?

代码如下:


答:传值是把实参的值赋值给行参 那么对行参的修改,不会影响实参的值
传地址 是传值的一种特殊方式,只是他传递的是地址,不是普通的如int 那么传地址以后,实参和行参都指向同一个对象

31.如何通过javascript判断一个窗口是否已经被屏蔽

代码如下:


答:获取open()的返回值,如果是null,就是屏蔽了

33.对于大流量的网站,您采用什么样的方法来解决访问量问题

代码如下:


答:首先,确认服务器硬件是否足够支持当前的流量
其次,优化数据库访问。
第三,禁止外部的盗链。
第四,控制大文件的下载。
第五,使用不同主机分流主要流量
第六,使用流量分析统计软件

以上所述就是本文的全部内容了,希望能够对大家学习php有所帮助。

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1269
29
C# Tutorial
1248
24
PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

Explain secure password hashing in PHP (e.g., password_hash, password_verify). Why not use MD5 or SHA1? Explain secure password hashing in PHP (e.g., password_hash, password_verify). Why not use MD5 or SHA1? Apr 17, 2025 am 12:06 AM

In PHP, password_hash and password_verify functions should be used to implement secure password hashing, and MD5 or SHA1 should not be used. 1) password_hash generates a hash containing salt values ​​to enhance security. 2) Password_verify verify password and ensure security by comparing hash values. 3) MD5 and SHA1 are vulnerable and lack salt values, and are not suitable for modern password security.

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

The Enduring Relevance of PHP: Is It Still Alive? The Enduring Relevance of PHP: Is It Still Alive? Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

How does PHP type hinting work, including scalar types, return types, union types, and nullable types? How does PHP type hinting work, including scalar types, return types, union types, and nullable types? Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

PHP vs. Python: Understanding the Differences PHP vs. Python: Understanding the Differences Apr 11, 2025 am 12:15 AM

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP and Python: Code Examples and Comparison PHP and Python: Code Examples and Comparison Apr 15, 2025 am 12:07 AM

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

See all articles