Table of Contents
前言
题目
Home Backend Development PHP Tutorial 一些PHP面试标题

一些PHP面试标题

Jun 13, 2016 pm 12:36 PM
cookie lt php quot session

一些PHP面试题目

前言

生活总是要往前看,等待了一天也没有阿里的面试通知,心情是相当灰暗,但是生活总要继续,还是要向前看,做几道PHP的面试题目,毕竟我的主要开发语言还是PHP


题目

1、一个包含中英文的字符串如何获得它的自然长度


需要考虑汉字的字符编码,注意UTF-8中一个汉字占3个字节


<?php /**
 * 获取中英文混排的字符串的长度
 */

$str = "还没等到阿里的面试通知123456";

$nor_len = mb_strlen($str, "utf8");

echo $nor_len."<br>";

$len = strlen($str);

echo $len."<br>";
Copy after login


2、问一问什么是ORM,有时候会问这个词的英文全称是什么,问问ta对于ORM有什么个人的看法


对象关系映射(Object Relationl Mapping,简称ORM)是一种为了解决面向对象与关系数据库存在的互不匹配的现象的技术。简单的说,ORM是通过使用描述对象和数据库之间映射的元数据,将程序中的对象自动持久化到关系型数据库中。本质上是将数据从一种形式转换到另一种形式



3、问一问假如ta要自己去实现一个framework,ta会怎么设计


这里我直接谈一下自己对MVC的理解,我都是用原生php开发,没有用过框架




所以就算不用框架,自己代码里也可以做到mvc分层



4、问一问framework设计里的url dispatcher他会怎么考虑


还是MVC的问题,可以通过url进行设计:http://example.com////, 还可以通过nginx的rewrite进行跳转



5、如何访问会话变量(session)?
A.通过$_GET
B.通过$_POST
C.通过$_REQUEST
D.通过全局变量
E.以上都不对


E,解释:session变量只能通过$_SESSION数组获得



6、哪个函数能让服务器输出如下header?

set-Cookie: foo=bar;
Copy after login


解答:setcookie('foo', 'bar', time() + 3600);



7、在忽略浏览器bug的正常情况下,如何用一个与先前设置的域名(domain)不同的新域名来访问某个cookie?
A.通过HTTP_REMOTE_COOKIE访问
B.不可能
C.在调用setcookie()时设置一个不同的域名
D.向浏览器发送额外的请求
E.使用Javascript,把cookie包含在URL中发送


B,解释:cookie不能跨域访问



8、在HTTPS下,URL和查询字串(query string)是如何从浏览器传到Web服务器上的?
A.这两个是明文传输,之后的信息加密传输
B.加密传输
C.URL明文传输,查询字串加密传输
D.URL加密传输,查询字串明文传输
E.为确保加密,查询字串将转换为header,夹在POST信息中传输


B,解释:ssl加密



9、当把一个有两个同名元素的表单提交给PHP脚本时会发生什么?
A.它们组成一个数组,存储在超级全局变量数组中
B.第二个元素的值加上第一个元素的值后,存储在超级全局变量数组中
C.第二个元素将覆盖第一个元素
D.第二个元素将自动被重命名
E.PHP输出一个警告


C


10、如何把数组存储在cookie里?
A.给cookie名添加一对方括号[]
B.使用implode函数
C.不可能,因为有容量限制
D.使用serialize函数
E.给cookie名添加ARRAY关键词


D,解释:序列化,serialize序列化数组即可,采用implode会丢失关联数组的key值



11、以下脚本输出什么?(E)
ob_start();
for ($i = 0; $i echo $i;
}
$output = ob_get_contents();
ob_end_clean();
echo $output;
?>
A.12345678910
B.1234567890
C.0123456789
D.什么都没有
E.一个提示


C,解释:ob_start打开输出缓存,将输出字符保存在缓冲区里。通过ob_get_contents()获取输出的字符串



12、默认情况下,PHP把会话(session)数据存储在______里。
A.文件系统
B.数据库
C.虚拟内容
D.共享内存
E.以上都不是


A,解释:默认存在文件系统里,为了分布式,一般都放在Nosql数据库里,例如nginx+fastcgi+php5_fpm,修改fpm的php.ini session.save_path=tcp://redisip:redisport?auth=redispasswd



14、假设浏览器没有重启,那么在最后一次访问后的多久,会话(session)才会过期并被回收?
A.1440秒后
B.在session.gc_maxlifetime设置的时间过了后
C.除非手动删除,否则永不过期
D.除非浏览器重启,否则永不过期
E.以上都不对


B或者E,解释:虽然session有过期机制,默认session.gc_maxlifetime是1440秒,但是必须启动gc机制才行,gc机制的启动是有概率的,session分别使用session.gc_probability和session.gc_divisor来确定session的gc概率



15、哪个函数能把换行转换成HTML标签


nl2br



16、index.php脚本如何访问表单元素email的值?(双选)




A.$_GET[‘email’]
B.$_POST[‘email’]
C.$_SESSION[‘text’]
D.$_REQUEST[‘email’]
E.$_POST[‘text’]


BD,解释:提交方法为POST,所以只能从$_POST和$_REQUEST两个超级全局变量数组取得值



17、如果不给cookie设置过期时间会怎么样?
A.立刻过期
B.永不过期
C.cookie无法设置
D.在浏览器会话结束时过期
E.只在脚本没有产生服务器端session的情况下过期


D,解释:cookie跟session没有半毛钱关系,唯一的作用可以用cookie保存session的会话id


18、以下脚本将如何影响$s字符串?(双选)
$s = '

Hello

';
$ss = htmlentities ($s);
echo $s;
?>
A.尖括号会被转换成HTML标记,因此字符串将变长
B.没有变化
C.在浏览器上打印该字符串时,尖括号是可见的
D.在浏览器上打印该字符串时,尖括号及其内容将被识别为HTML标签,因此不可见
E.由于调用了htmlentities(),字符串会被销毁


BD,解释:htmlspecialchars转义$s后赋值给$ss,输出的是$ss










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
1249
24
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,

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

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.

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.

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.

See all articles