PHP面试题集

Jun 23, 2016 pm 02:30 PM

汗~~做了一下网络上的php题目,不知不觉做到现在.....把答案贴出来
如果有问题请欢迎补充和指正

 

 1、用PHP打印出前一天的时间格式是2006-5-10 22:21:21(2分)

$a = date("Y-m-d H:i:s", strtotime("-1 day"));

print_r($a);

 

  2、echo(),print(),print_r()的区别(3分)

echo 和print不是一个函数,是一个语言结构

int print(string $arg), 只有一个参数

echo $arg1,$arg2; 可以输出多个参数,返回void

echo和print只能打印出string,不能打印出结构

print_r能打印出结构

比如 

$arr = array("key"=>"value");

print_r($arr);

 

  3、能够使HTML和PHP分离开使用的模板(1分)

smarty,phplib

 

  4、使用哪些工具进行版本控制?(1分)

svn,git,cvs

 

  5、如何实现字符串翻转?(3分)

英文:

strrev($a)

中文或其他文字:

中文:GB2312, 代码是使用GB2312编码

function reverse($str)

$ret = "";

$len = mb_strwidth($str,"GB2312");

for($i=0; $i

$arr[] = mb_substr($str, $i, 1, "GB2312");

return implode("", array_reverse($arr));

print_r(reverse("你好"));

 

  ---------------------------------------------------------------

  6、优化MYSQL数据库的方法。(4分,多写多得)

语句方面:

1 使用索引,增加查询效率

2 优化查询语句,提高索引命中率

数据库涉及方面:

1 构造分库分表,提高数据库的存储和扩展能力

2 根据需要使用不同的存储引擎

 

  7、PHP的意思(送1分)

超级文本预处理语言

Hypertext PreProcessor

 

  8、MYSQL取得当前时间的函数是?,格式化日期的函数是(2分)

CURRENT_TIMESTAMP()

DATE_FORMAT()

select DATE_FORMAT("2011-11-21 10:10:10", "%Y-%m-%d");

 

  9、实现中文字串截取无乱码的方法。(3分)

mb_substr($str, 1, 1, "GB2312");

 

  ---------------------------------------------------------------

  10、您是否用过版本控制软件? 如果有您用的版本控制软件的名字是?(1分)

svn

git

 

  11、您是否用过模板引擎? 如果有您用的模板引擎的名字是?(1分)

smarty

 

  12、请简单阐述您最得意的开发之作(4分)

XXX

 

  13、对于大流量的网站,您采用什么样的方法来解决访问量问题?(4分)

1 有效使用缓存,增加缓存命中率

2 使用负载均衡

3 对静态文件使用CDN进行存储和加速

4 想法减少数据库的使用

5 查看出现统计的瓶颈在哪里

 

  -----------------------------------------------------------------

  14、用PHP写出显示客户端IP与服务器IP的代码1分)

$_SERVER["REMOTE_ADDR"]

$_SERVER["SERVER_ADDR"]

 

  15、语句include和require的区别是什么?为避免多次包含同一文件,可用(?)语句代替它们? (2分)

在失败的时候:

include产生一个warning,而require产生直接产生错误中断

require在运行前载入

include在运行时载入

require_once

include_once

 

  16、如何修改SESSION的生存时间(1分).

session_set_cookie_params 

 

  17、有一个网页地址, 比如PHP研究室主页: http://www.phpv.net/index.html,如何得到它的内容?($1分)

file_get_contents

curl 

 

  18、在HTTP 1.0中,状态码401的含义是(?);如果返回“找不到文件”的提示,则可用 header 函数,其语句为(?);(2分)

未授权

header("HTTP/1.0 404 Not Found");

fast CGI中:

header("Status: 404 Not Found");

 

  19、在PHP中,heredoc是一种特殊的字符串,它的结束标志必须?(1分)

成对出现

$a =

good test

EOD;

 

  20、谈谈asp,php,jsp的优缺点(1分)

asp是需要依赖IIS,是微软开发的语言

php和jsp可以依赖apache或者 nginx等其他服务器

 

  21、谈谈对mvc的认识(1分)

model : 数据结构层

view :展现

control : 接收和判断处理输入

 

  -------------------------------------------------------------------

  22、写出发贴数最多的十个人名字的SQL,利用下表:members(id,username,posts,pass,email)(2分)

select top 10 id,username from members order by posts desc

 

  23. 请说明php中传值与传引用的区别。什么时候传值什么时候传引用?(2分)

&表示传引用

函数中参数传引用会将参数进行改变

一般在输出参数有多个的时候可以考虑使用引用

 

  24. 在PHP中error_reporting这个函数有什么作用? (1分)

设定error的展示级别

 

  25. 请写一个函数验证电子邮件的格式是否正确 (2分)

$str = "jianfeng@126.com";

$regex = "^([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})$" ; //正则

return preg_match($regex, $str)

 

  26. 简述如何得到当前执行脚本路径,包括所得到参数。(2分)

$argc --获取参数数量

$argv --获取参数列表

 

  27.如何修改SESSION的生存时间. (1分)

session_set_cookie_params

 

  --------------------------------------------------------------------

  28、JS表单弹出对话框函数是?获得输入焦点函数是? (2分)

alert()

confirm()

promopt()

focus()

 

  29、JS的转向函数是?怎么引入一个外部JS文件?(2分)

window.location.href="#"

 

  30、foo()和@foo()之间有什么区别?(1分)

@代表所有warning忽略

 

  31、如何声明一个名为”myclass”的没有方法和属性的类? (1分)

class myclass

 

  32、如何实例化一个名为”myclass”的对象?(1分)

$myclass = new myclass();

 

  33、你如何访问和设置一个类的属性? (2分)

class A

public $name = "A";

$a = new A();

$n = $a->name;

print_r($n);

 

  34、mysql_fetch_row() 和mysql_fetch_array之间有什么区别? (1分)

mysql_fetch_array() 是 mysql_fetch_row() 的扩展版本。除了将数据以数字索引方式储存在数组中之外,还可以将数据作为关联索引储存,用字段名作为键名。 

    mysql_connect("localhost", "mysql_user", "mysql_password") or

        die("Could not connect: " . mysql_error());

    mysql_select_db("mydb");

    $result = mysql_query("SELECT id, name FROM mytable");

    while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {

        printf ("ID: %s  Name: %s", $row["id"], $row["name"]);

    }

    mysql_free_result($result);

 

  --------------------------------------------------------------------

  35、GD库是做什么用的? (1分)

动态的开放的图片处理库

 

  36、指出一些在PHP输入一段HTML代码的办法。(1分)

echo "{html}"

echo

{html}

EOD;

 

  37、下面哪个函数可以打开一个文件,以对文件进行读和写操作?(1分) c

  (a) fget() (b) file_open() (c) fopen() (d) open_file()

 

  38、下面哪个选项没有将 john 添加到users 数组中? (1分) b

  (a) $users[] = ‘john’;

  (b) array_add($users,’john’);

  (c) array_push($users,‘john’);

  (d) $users ||= ‘john’;

 

  39、下面的程序会输入是否?(1分) 10

  $num = 10;

  function multiply(){

  $num = $num * 10;

  }

  multiply();

  echo $num;

  ?>

 

  40、使用php写一段简单查询,查出所有姓名为“张三”的内容并打印出来 (2分)

  表名 UserName Tel Content Date

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

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

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

  请根据上面的题目完成代码:

  $mysql_db=mysql_connect("local","root","pass");

  @mysql_select_db("DB",$mysql_db);

$sql = sprintf("select * from %s where UserName = '%s'", 

"表名", 

"张三");

$values = mysql_query($sql);

while($item = mysql_fetch_query_array($values))

echo sprintf("用户名:%s, 电话 %s, 学历: %s, 毕业日期: %s", 

$item['UserName'], $item['Tel'], $item['Content'], $item['Date']

 

  41、如何使用下面的类,并解释下面什么意思?(3)

  class test{

  function Get_test($num){

  $num=md5(md5($num)."En");

  return $num;

  }

  }

$test = new test();

$ret = $test->Get_test(11);

print_r($ret);exit;

将num进行MD5编码之后生成的32位字符串a1和"En"联系起来之后再进行一次MD5编码

 

  ----------------------------------------------------------------------------

  42、写出 SQL语句的格式 : 插入 ,更新 ,删除 (4分)

  表名 UserName Tel Content Date

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

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

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

  (a) 有一新记录(小王 13254748547 高中毕业 2007-05-06)请用SQL语句新增至表中

insert into 表名 values('小王', '13254748547', '高中毕业', '2007-05-06')

  (b) 请用sql语句把张三的时间更新成为当前系统时间

update 表名 set Date = GETDATE() where UserName = "张三"

  (c) 请写出删除名为张四的全部记录

delete from 表明 where UserName = "张四"

 

  43、请写出数据类型(int char varchar datetime text)的意思; 请问varchar和char有什么区别(2分)

int 整型

char 存储定长

varchar 存储变长

datetime 时间

text 存储变长的

varchar是变长

char(20) 定长

 

  44、MySQ自增类型(通常为表ID字段)必需将其设为(?)字段(1分)

auto_increment

  45、写出以下程序的输出结果 (1分)

  $b=201;

  $c=40;

  $a=$b>$c?4:5;

  echo $a;

  ?>

 

  46、检测一个变量是否有设置的函数是否?是否为空的函数是?(2分)

isset()

empty()

 

  -----------------------------------------------------------------------------

  47、取得查询结果集总数的函数是?(1分)

mysql_num_rows()

 

  48、$arr = array('james', 'tom', 'symfony'); 请打印出第一个元素的值 (1分)

print_r($arr[0]);

reset($arr);

print_r(current($arr));

print_r(array_shift($arr));

 

  49、请将41题的数组的值用','号分隔并合并成字串输出(1分)

implode

 

  50、$a = 'abcdef'; 请取出$a的值并打印出第一个字母(1分)

$a[0];

substr($a, 0, 1);

 

  51、PHP可以和sql server/oracle等数据库连接吗?(1分)

有现成的库

 

  52、请写出PHP5权限控制修饰符(3分)

public

private 

protected

 

  53、请写出php5的构造函数和析构函数(2分)

public function __construct()

public function __destruct()

 

  编程题

  1. 写一个函数,尽可能高效的,从一个标准 url 里取出文件的扩展名

  例如: http://www.sina.com.cn/abc/de/fg.php?id=1 需要取出 php 或 .php

$url = "http://www.sina.com.cn/abc/de/fg.php?id=1";

$arr = parse_url($url);

$pathArr = pathinfo($arr['path']);

print_r($pathArr['extension']);

 

  3. 写一个函数,算出两个文件的相对路径

  如 $a = '/a/b/c/d/e.php';

  $b = '/a/b/12/34/c.php';

  计算出 $b 相对于 $a 的相对路径应该是 http://www.cnblogs.com/12/34/c.php将添上

 

$a = '/a/b/c/d/e.php';

$b = '/a/b/12/34/c.php';

//获取path相对于conpath的相对路径

function sGetRelativePath($path, $conpath)

$pathArr = explode("/", $path);

$conpathArr = explode("/", $conpath);

$dismatchlen = 0;

for($i = 0; $i

if($conpathArr[$i] != $pathArr[$i])

$dismatchlen = count($pathArr) - $i;

$arrLeft = array_slice($pathArr, $i);

break;

$ret = str_repeat("../", $dismatchlen).implode("/", $arrLeft);

return $ret;

print_r(sGetRelativePath($b, $a));

 

  3.写一个函数,能够遍历一个文件夹下的所有文件和子文件夹。

 

function aGetAllFile($folder)

$aFileArr = array();

if(is_dir($folder))

$handle = opendir($folder);

while(($file = readdir($handle)) !== false)

//如果是.或者..则跳过

if($file == "." || $file == "..")

continue;

if(is_file($folder."/".$file))

$aFileArr[] = $file; 

else if(is_dir($folder."/".$file))

$aFileArr[$file] = aGetAllFile($folder."/".$file);

closedir($handle);

return $aFileArr;

$path = "/home/test/sql";

print_r(aGetAllFile($path));

 

-------------------------- 

 作者:轩脉刃(叶剑峰)

出处:http://www.cnblogs.com/yjf512/

本文版权归yjf512和cnBlog共有,欢迎转载,但未经作者同意必须保留此段声明

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

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

How does session hijacking work and how can you mitigate it in PHP? How does session hijacking work and how can you mitigate it in PHP? Apr 06, 2025 am 12:02 AM

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

What are Enumerations (Enums) in PHP 8.1? What are Enumerations (Enums) in PHP 8.1? Apr 03, 2025 am 12:05 AM

The enumeration function in PHP8.1 enhances the clarity and type safety of the code by defining named constants. 1) Enumerations can be integers, strings or objects, improving code readability and type safety. 2) Enumeration is based on class and supports object-oriented features such as traversal and reflection. 3) Enumeration can be used for comparison and assignment to ensure type safety. 4) Enumeration supports adding methods to implement complex logic. 5) Strict type checking and error handling can avoid common errors. 6) Enumeration reduces magic value and improves maintainability, but pay attention to performance optimization.

Describe the SOLID principles and how they apply to PHP development. Describe the SOLID principles and how they apply to PHP development. Apr 03, 2025 am 12:04 AM

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What is REST API design principles? What is REST API design principles? Apr 04, 2025 am 12:01 AM

RESTAPI design principles include resource definition, URI design, HTTP method usage, status code usage, version control, and HATEOAS. 1. Resources should be represented by nouns and maintained at a hierarchy. 2. HTTP methods should conform to their semantics, such as GET is used to obtain resources. 3. The status code should be used correctly, such as 404 means that the resource does not exist. 4. Version control can be implemented through URI or header. 5. HATEOAS boots client operations through links in response.

How do you handle exceptions effectively in PHP (try, catch, finally, throw)? How do you handle exceptions effectively in PHP (try, catch, finally, throw)? Apr 05, 2025 am 12:03 AM

In PHP, exception handling is achieved through the try, catch, finally, and throw keywords. 1) The try block surrounds the code that may throw exceptions; 2) The catch block handles exceptions; 3) Finally block ensures that the code is always executed; 4) throw is used to manually throw exceptions. These mechanisms help improve the robustness and maintainability of your code.

What are anonymous classes in PHP and when might you use them? What are anonymous classes in PHP and when might you use them? Apr 04, 2025 am 12:02 AM

The main function of anonymous classes in PHP is to create one-time objects. 1. Anonymous classes allow classes without names to be directly defined in the code, which is suitable for temporary requirements. 2. They can inherit classes or implement interfaces to increase flexibility. 3. Pay attention to performance and code readability when using it, and avoid repeatedly defining the same anonymous classes.

See all articles