search
HomeBackend DevelopmentPHP Tutorial33道php常见面试题及答案_php实例

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
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
How can you protect against Cross-Site Scripting (XSS) attacks related to sessions?How can you protect against Cross-Site Scripting (XSS) attacks related to sessions?Apr 23, 2025 am 12:16 AM

To protect the application from session-related XSS attacks, the following measures are required: 1. Set the HttpOnly and Secure flags to protect the session cookies. 2. Export codes for all user inputs. 3. Implement content security policy (CSP) to limit script sources. Through these policies, session-related XSS attacks can be effectively protected and user data can be ensured.

How can you optimize PHP session performance?How can you optimize PHP session performance?Apr 23, 2025 am 12:13 AM

Methods to optimize PHP session performance include: 1. Delay session start, 2. Use database to store sessions, 3. Compress session data, 4. Manage session life cycle, and 5. Implement session sharing. These strategies can significantly improve the efficiency of applications in high concurrency environments.

What is the session.gc_maxlifetime configuration setting?What is the session.gc_maxlifetime configuration setting?Apr 23, 2025 am 12:10 AM

Thesession.gc_maxlifetimesettinginPHPdeterminesthelifespanofsessiondata,setinseconds.1)It'sconfiguredinphp.iniorviaini_set().2)Abalanceisneededtoavoidperformanceissuesandunexpectedlogouts.3)PHP'sgarbagecollectionisprobabilistic,influencedbygc_probabi

How do you configure the session name in PHP?How do you configure the session name in PHP?Apr 23, 2025 am 12:08 AM

In PHP, you can use the session_name() function to configure the session name. The specific steps are as follows: 1. Use the session_name() function to set the session name, such as session_name("my_session"). 2. After setting the session name, call session_start() to start the session. Configuring session names can avoid session data conflicts between multiple applications and enhance security, but pay attention to the uniqueness, security, length and setting timing of session names.

How often should you regenerate session IDs?How often should you regenerate session IDs?Apr 23, 2025 am 12:03 AM

The session ID should be regenerated regularly at login, before sensitive operations, and every 30 minutes. 1. Regenerate the session ID when logging in to prevent session fixed attacks. 2. Regenerate before sensitive operations to improve safety. 3. Regular regeneration reduces long-term utilization risks, but the user experience needs to be weighed.

How do you set the session cookie parameters in PHP?How do you set the session cookie parameters in PHP?Apr 22, 2025 pm 05:33 PM

Setting session cookie parameters in PHP can be achieved through the session_set_cookie_params() function. 1) Use this function to set parameters, such as expiration time, path, domain name, security flag, etc.; 2) Call session_start() to make the parameters take effect; 3) Dynamically adjust parameters according to needs, such as user login status; 4) Pay attention to setting secure and httponly flags to improve security.

What is the main purpose of using sessions in PHP?What is the main purpose of using sessions in PHP?Apr 22, 2025 pm 05:25 PM

The main purpose of using sessions in PHP is to maintain the status of the user between different pages. 1) The session is started through the session_start() function, creating a unique session ID and storing it in the user cookie. 2) Session data is saved on the server, allowing data to be passed between different requests, such as login status and shopping cart content.

How can you share sessions across subdomains?How can you share sessions across subdomains?Apr 22, 2025 pm 05:21 PM

How to share a session between subdomains? Implemented by setting session cookies for common domain names. 1. Set the domain of the session cookie to .example.com on the server side. 2. Choose the appropriate session storage method, such as memory, database or distributed cache. 3. Pass the session ID through cookies, and the server retrieves and updates the session data based on the ID.

See all articles

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 Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version