这几道题是在德问上看到的,感觉挺有意思,拿来给大家分享其中的陷阱,看看你会不会掉入其中。
第一题
$arr = array(0=>1,"aa"=>2, 3, 4);
foreach($arr as $key=>$val){
print($key == "aa" ? 5 : $val);
}
输出结果是多少?如果的答案是1534就掉入陷阱了。
先看看这个数组最终形成的结构:
Array
(
[0] => 1
[aa] => 2
[1] => 3
[2] => 4
)
然后遍历每一个元素的key看等不等于aa,等于就用5替代。当我告诉你答案是5534的时候,你会不会有点惊讶!难道0等于"aa"吗?是的,0就等于"aa",这道题重点就考你这个。在PHP中两个值进行逻辑判断时,如果两个值的类型不一致PHP会自动把右边的值转换到左边的类型,然后再进行判断。因此"aa"转换整形等于0,自然也就等于左边的0了。你可以使用全等于避免这种该情况,也就是如果你写成:
print($key === "aa" ? 5 : $val);
那么答案就是1534了。
第二题
$i='11';
printf("%d\n",printf("%d",printf("%d",$i)));
输出结果是多少?如果你回答是11,或者111111就掉入陷阱了。
先了解printf这个函数,printf不仅是打印函数,它还有返回值。重点就在这
var_dump(printf("%d",$i));
你猜猜上面的结果是啥?先是printf打印变量本身11,然后printf会返回一个变量字符串长度的值,11有两个字符,于是返回2,于是上面语句的执行结果等于:
清楚了这一点以后,再回过来看上面的试题,按照优先级,限制性深度printf函数,打印11,返回2。接着到第二级printf函数,打印2,返回1。最后到第三层,直接打印1,所以执行结果是 1121。
第三题
$a = 3;
$b = 5;
if($a = 5 || $b = 7) {
$a++;
$b++;
}
echo $a . " " . $b;
执行结果是多少?如果你回答 6 8 or 4 6 or 6 6,那你就掉入陷阱了。
第一个陷阱,认为答案等于 4 6 。估计你粗心把 $a = 5 || $b = 7 看成 $a == 5 || $b == 7 ,这是新手常犯的错误。
第二个陷阱,认为答案等于 6 8。 你识破了 $a = 5 || $b = 7 这个骗局,但你没有注意到,逻辑或里只要依次执行直到某个表达式结果为true,表达式后边的就不再执行,$a = 5 返回true,后边的$b=7就不执行了。
第三个陷阱,认为答案等于 6 6。 OK,你识破了 逻辑或的规则,于是$a=5执行,$b=7不执行,但是你没有考虑到这里是逻辑表达式,返回给$a的值是要转换为布尔值的。这样看。
所以经过以上三个陷阱,你应该知道答案是多少了,其实 $a等于true以后,echo $a 输出就是1 ,$b值不变,结果就是 1 6 。
第四题
$count = 5;
function get_count() {
static $count = 0;
return $count++;
}
++$count;
get_count();
echo get_count();
执行结果是多少?如果你回答 2 ,恭喜,你掉入陷阱了。
其实这道题主要考两点,第一点是static静态类型。这种的值永远都是静态的,第一次调用声明等于0,并且自增等于1。第二次调用,1再自增就等于2。但其实这里还有一道陷阱,那就是++a与a++的区别,前++是先自增,后++是先返回值再自增,所以结果等于 1。
第五题
$a = count ("567") + count(null) + count(false);
echo $a;
如果你回答 3 or 1,恭喜,掉入陷阱了。
因为count(null)等于0,false也算一个值。所以count(false)等于1。

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python are both high-level programming languages that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

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.


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Dreamweaver Mac version
Visual web development tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Chinese version
Chinese version, very easy to use

WebStorm Mac version
Useful JavaScript development tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.