Table of Contents
Q10
Home Backend Development PHP Tutorial # 10个值得深思的PHP面试问题

# 10个值得深思的PHP面试问题

Jun 20, 2016 pm 12:30 PM

本文翻译自: https://www.toptal.com/php/interview-questions,文章所罗列的问题虽然看似简单,但是每个背后都涵盖了一个或几个大家容易忽视的基础知识点,希望能够帮助到你的面试和平时工作。

Q1

第一个问题关于 弱类型

$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中 = !相对== 更为严格需要要求数据类型一致。

Q2

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

$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

实际运行结果是

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

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

Q3

关于变量的引用;

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

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

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

Q4

下面是true还是false

var_dump(0123 == 123);  var_dump('0123' == 123);  var_dump('0123' === 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很显然上面的问题已经说过了数字和字符串类型不一致。

Q5

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

$referenceTable = array();$referenceTable['val1'] = array(1, 2);$referenceTable['val2'] = 3;$referenceTable['val3'] = array(4, 5);$testArray = array();$testArray = array_merge($testArray, $referenceTable['val1']);var_dump($testArray);  $testArray = array_merge($testArray, $referenceTable['val2']);var_dump($testArray);  $testArray = array_merge($testArray, $referenceTable['val3']);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['val1']);var_dump($testArray);  $testArray = array_merge($testArray, (array)$referenceTable['val2']);var_dump($testArray);  $testArray = array_merge($testArray, (array)$referenceTable['val3']);var_dump($testArray);  
Copy after login

Q6

$x应该是输出什么?

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

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

$x = true;true and false  
Copy after login

答案显而易见。

Q7

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

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

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

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

$x = 3 + 15 + 0
Copy after login

Q8

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

$text = 'John ';$text[10] = 'Doe';
Copy after login

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

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

Q9

下面的输出结果会是什么

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

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

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

Q10

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

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

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

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

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

但是 $x = (int)'0xFF';是否也会变成255呢?显然不是,将一个字符串进行强制类型转换实际上用的是 convert_to_long,它实际上是将字符串从左向右进行转换,遇到非数字字符则停止。因此 0xFF到x就停止了。所以 $x=0

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 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
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
1665
14
PHP Tutorial
1270
29
C# Tutorial
1249
24
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 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.

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

PHP vs. Other Languages: A Comparison PHP vs. Other Languages: A Comparison Apr 13, 2025 am 12:19 AM

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

See all articles