Home Backend Development PHP Tutorial The 10 most common PHP interview questions

The 10 most common PHP interview questions

Nov 10, 2017 am 10:24 AM
php test questions

很多程序员随着公司面试的次数,以及自己总结过的php面试题亦或者是自己对php知识点掌握的比较好的情况下、很多小伙伴都开始有点眼高手低了,在面对一些只要细心就能完成的php面试题,却因为眼高手低而犯错,现在很多公司都会出一些这样的php面试题、第一是为了测试你对基础知识掌握,第二就是为了测试程序员的细心,今天我们就给大家总结了一下总容易出错的面试题!

1.关于弱类型

$str1 = 'yabadabadoo';
$str2 = 'yaba';
if (strpos($str1,$str2)) {  
    echo "\"" . $str1 . "\" contains \"" . $str2 . "\"";
} else {
    echo "\"" . $str1 . "\" does not contain \"" . $str2 . "\"";
}
Copy after login

正确运行的输出结果:

"yabadabadoo" does not contain "yaba"
Copy after login

strpos是返回字符串str2在str1的位置,没有找到则返回false然而实际上这次返回了0而在if语句中0也被当作false,所以我们需要对false做类型判断,正确的代码如下:

$str1 = 'yabadabadoo';
$str2 = 'yaba';
if (strpos($str1,$str2) !== false) {  
    echo "\"" . $str1 . "\" contains \"" . $str2 . "\"";
} else {
    echo "\"" . $str1 . "\" does not contain \"" . $str2 . "\"";
}
Copy after login

需要注意的是我们使用了!==,在php 和 JS中= !相对== 更为严格需要要求数据类型一致。

2.下面的输出结果会是怎样?

$x = 5;
echo $x;  
echo "<br />";  
echo $x+++$x++;  
echo "<br />";  
echo $x;  
echo "<br />";  
echo $x---$x--;  
echo "<br />";  
echo $x;
Copy after login

实际运行结果是

5  
11  
7  
1  
5
Copy after login

关于 $x++ 和 $x--这个问题其实非常容易遇见,我们只需记住$x++使用最近的值,然后才自增。

运算符的优先级,++ 是明显高于 +,因此先执行++ 再执行 + 。关于运算符的优先级,有的时候我们真的可以通过括号来让我们的程序更让人直观的了解,毕竟代码不光是用于执行的,有的时候或许团队的可读性也是提高效率的一种。

3.关于变量的引用;

$a = &#39;1&#39;;
$b = &$a;
$b = "2$b";
Copy after login

请问 $a 和 $b的值各位多少

部分第一时间会想到 $a='1' $b='21',仔细一看 $b=&$a,这里$b是变量$a的引用而不是直接 赋值。

4.下面是true还是false

var_dump(0123 == 123);  
var_dump(&#39;0123&#39; == 123);  
var_dump(&#39;0123&#39; === 123);
Copy after login

var_dump(0123 == 123);// false,PHP会默认把0123当作8进制来处理,实际转化为10进制就是83,显然这不是相等的。

var_dump('0123' == 123);// true这里php会非常有趣的将’0123’转换成一个数字而且默认去掉了前面的0也就是123==123

var_dump('0123' === 123);// false很显然上面的问题已经说过了数字和字符串类型不一致。

5.下面的代码有什么问题吗?输出会是什么,怎样修复它

$referenceTable = array();
$referenceTable[&#39;val1&#39;] = array(1, 2);
$referenceTable[&#39;val2&#39;] = 3;
$referenceTable[&#39;val3&#39;] = array(4, 5);
 
$testArray = array();
 
$testArray = array_merge($testArray, $referenceTable[&#39;val1&#39;]);
var_dump($testArray);  
$testArray = array_merge($testArray, $referenceTable[&#39;val2&#39;]);
var_dump($testArray);  
$testArray = array_merge($testArray, $referenceTable[&#39;val3&#39;]);
var_dump($testArray);
Copy after login


实际输出如下:

array(2) { [0]=> int(1) [1]=> int(2) }  
NULL  
NULL
Copy after login

运行的时候你或许还能看到下面的警告

Warning: array_merge(): Argument #2 is not an array  
Warning: array_merge(): Argument #1 is not an array
Copy after login

array_merge需要传入的参数都是数组,如果不是,则会返回null。 你可以这样修改

$testArray = array_merge($testArray, (array)$referenceTable[&#39;val1&#39;]);
var_dump($testArray);  
$testArray = array_merge($testArray, (array)$referenceTable[&#39;val2&#39;]);
var_dump($testArray);  
$testArray = array_merge($testArray, (array)$referenceTable[&#39;val3&#39;]);
var_dump($testArray);
Copy after login

6.$x应该是输出什么?

$x = true and false;
var_dump($x);
Copy after login

部分同学或许会第一时间想到false,实际上这里依旧是强调运算符的优先级,= 会比 and级别高点,因此等同下面的代码

$x = true;
true and false
Copy after login

答案显而易见。

7.经过下面的运算 $x的值应该是多少?

$x = 3 + "15%" + "$25"
Copy after login

答案是18,PHP是会根据上下文实现类型的自动转换

上面的代码我们可以这样理解,如果我们在与字符串进行数学运算,实际php会尽可能将字符串中的数组进行转换,如果是数字开头的话则转换成改数字比如”15%”会变成15,如果不是数字开头则会变成0; 上面的运算类似下面 :

$x = 3 + 15 + 0
Copy after login

8.运行下面的代码,$text 的值是多少?strlen($text)又会返回什么结果?

$text = &#39;John &#39;;
$text[10] = &#39;Doe&#39;;
Copy after login

上面代码执行完毕后 $text = "John D"(John后面会有连续的5个空格) strlen($text)会返回11

$text[10] = "Doe"给某个字符串具体的某个位置具体字符时候,实际只会把D赋给$text. 虽然$text才开始只有5个自负长度,但是php会默认填充空格。这和别的语言有些差别。

9.下面的输出结果会是什么?

$v = 1;
$m = 2;
$l = 3;
 
if( $l > $m > $v){  
    echo "yes";
}else{
    echo "no";
}
Copy after login

实际的输出是”no”,只要仔细分析就不难得出

$l>$m 会转换成1 ,则这个时候再和$m比较。

10.执行下面代码$x会变成什么值呢?

$x = NULL;
 
if (&#39;0xFF&#39; == 255) {  
    $x = (int)&#39;0xFF&#39;;
}
Copy after login

实际的运行结果是$x=0而不是255.

首先'oxFF' == 255我们好判断,会进行转换将16进制数字转换成10进制数字,0xff -> 255.

PHP使用is_numeric_string 判断字符串是否包含十六进制数字然后进行转换。

But will $x = (int)'0xFF'; also become 255? Obviously not. To force a string to type conversion actually uses convert_to_long, which actually converts the string from left to right and stops when it encounters non-numeric characters. So it stops from 0xFF to x. So $x=0

Summary:

The article summarizes some error-prone PHP interview questions. In fact, this They are all basic and simple things, it just depends on whether you are careful or not, so during the interview process, you must see the question clearly, listen to the question clearly, review the question well, and it is not too late to answer!

Related recommendations:

php interview questions php Sharing of core technical issues


Summary of written test questions in php interview questions


Summary of Thinkphp questions in php interview questions


Summary of written test questions in php interview questions


The above is the detailed content of The 10 most common PHP interview questions. For more information, please follow other related articles on the PHP Chinese website!

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)

Hot Topics

Java Tutorial
1663
14
PHP Tutorial
1266
29
C# Tutorial
1239
24
How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

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,

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

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

See all articles