The 10 most common PHP interview 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 . "\""; }
正确运行的输出结果:
"yabadabadoo" does not contain "yaba"
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 . "\""; }
需要注意的是我们使用了!==,在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;
实际运行结果是
5 11 7 1 5
关于 $x++ 和 $x--这个问题其实非常容易遇见,我们只需记住$x++使用最近的值,然后才自增。
运算符的优先级,++ 是明显高于 +,因此先执行++ 再执行 + 。关于运算符的优先级,有的时候我们真的可以通过括号来让我们的程序更让人直观的了解,毕竟代码不光是用于执行的,有的时候或许团队的可读性也是提高效率的一种。
3.关于变量的引用;
$a = '1'; $b = &$a; $b = "2$b";
请问 $a 和 $b的值各位多少
部分第一时间会想到 $a='1' $b='21',仔细一看 $b=&$a,这里$b是变量$a的引用而不是直接 赋值。
4.下面是true还是false
var_dump(0123 == 123); var_dump('0123' == 123); var_dump('0123' === 123);
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['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);
实际输出如下:
array(2) { [0]=> int(1) [1]=> int(2) } NULL NULL
运行的时候你或许还能看到下面的警告
Warning: array_merge(): Argument #2 is not an array Warning: array_merge(): Argument #1 is not an array
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);
6.$x应该是输出什么?
$x = true and false; var_dump($x);
部分同学或许会第一时间想到false,实际上这里依旧是强调运算符的优先级,= 会比 and级别高点,因此等同下面的代码
$x = true; true and false
答案显而易见。
7.经过下面的运算 $x的值应该是多少?
$x = 3 + "15%" + "$25"
答案是18,PHP是会根据上下文实现类型的自动转换
上面的代码我们可以这样理解,如果我们在与字符串进行数学运算,实际php会尽可能将字符串中的数组进行转换,如果是数字开头的话则转换成改数字比如”15%”会变成15,如果不是数字开头则会变成0; 上面的运算类似下面 :
$x = 3 + 15 + 0
8.运行下面的代码,$text 的值是多少?strlen($text)又会返回什么结果?
$text = 'John '; $text[10] = 'Doe';
上面代码执行完毕后 $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"; }
实际的输出是”no”,只要仔细分析就不难得出
$l>$m 会转换成1 ,则这个时候再和$m比较。
10.执行下面代码$x会变成什么值呢?
$x = NULL; if ('0xFF' == 255) { $x = (int)'0xFF'; }
实际的运行结果是$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:
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











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

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,

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.

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