今天遇到的面试题,面试十个九个错
$a = 1;
$b = $a + $a + $a++;
echo $b; //输出3
echo "
";
$d = 1;
$c = $d + $d++;
echo $c; //输出3
?>
大部分人答案:
$b=5; //错的
$c=3;
这里有人知道为什么两个结果是一样的嘛
回复讨论(解决方案)
其实没有为什么,C 就是这样的
#include "stdio.h"#include "conio.h"main(){ int a=1,b=1; printf("%d %d\n", a+a++, b+b+b++); getch();}
按语法说明
$b = $a + $a + $a++;
得到 3 是符合规则的
而 $c = $d + $d++; 得到 3 是不符合规则的
因此,将 ++ 和 + 混合使用,并不能一定得到预期的结果
楼主这个是PHP哦 非C哦
结果确实是两个3哦
和php版本有关。
php4得出的是3
2
Interactive mode enabled
$a = 1;
$b = $a + $a + $a++;
echo $b; //输出3
echo "
";
$d = 1;
$c = $d + $d++;
echo $c; //输出3
?>
^Z
Content-type: text/html
X-Powered-By: PHP/4.3.2
3
2
又见坑爹无意义面试题
呵呵 面试总是很坑 戳戳锐气
第一个,计算的顺序是($a+$a)+$a++,先计算($a+$a)=2,在得到$a++的执行结果1,此时$a的值变为2,但是($a+$a)=2的结果已经存入内存,因此$a为2不影响前面的计算结果,得到(1+1)+1=3
第二个,则是先得到$d++的执行结果1,此时$d的值变为2,所以结果时2+1=3
面试的不着调,他应该考一些项目东西
第一个,计算的顺序是($a+$a)+$a++,先计算($a+$a)=2,在得到$a++的执行结果1,此时$a的值变为2,但是($a+$a)=2的结果已经存入内存,因此$a为2不影响前面的计算结果,得到(1+1)+1=3
第二个,则是先得到$d++的执行结果1,此时$d的值变为2,所以结果时2+1=3
按优先级应该是先执行++ 再执行+运算符把
你这个是先执行+然后再执行++?
第一个,计算的顺序是($a+$a)+$a++,先计算($a+$a)=2,在得到$a++的执行结果1,此时$a的值变为2,但是($a+$a)=2的结果已经存入内存,因此$a为2不影响前面的计算结果,得到(1+1)+1=3
第二个,则是先得到$d++的执行结果1,此时$d的值变为2,所以结果时2+1=3
按优先级应该是先执行++ 再执行+运算符把
你这个是先执行+然后再执行++?
第一个是非结合。此时++递增的优先级高,因此先计算$a++;
第二个是左结合,计算方向从左到右,先计算$a+$a,再计算$a++
第一个,计算的顺序是($a+$a)+$a++,先计算($a+$a)=2,在得到$a++的执行结果1,此时$a的值变为2,但是($a+$a)=2的结果已经存入内存,因此$a为2不影响前面的计算结果,得到(1+1)+1=3
第二个,则是先得到$d++的执行结果1,此时$d的值变为2,所以结果时2+1=3
按优先级应该是先执行++ 再执行+运算符把
你这个是先执行+然后再执行++?
第一个是非结合。此时++递增的优先级高,因此先计算$a++;
第二个是左结合,计算方向从左到右,先计算$a+$a,再计算$a++
既然$a++优先级高,那$a++ 以后 再计算$a+$a的时候 $a应该是2啊 怎么$a还会是1啊
感觉像大学里的无聊考试题,谁tm的在项目里这么写谁就是跟leader过不去!
------------------------------------------------------AutoCSDN签名档------------------------------------------------------
码农场??码农播种代码、放牧思想的农场!冰糖网
感觉像大学里的无聊考试题,谁tm的在项目里这么写谁就是跟leader过不去!
------------------------------------------------------AutoCSDN签名档------------------------------------------------------
码农场??码农播种代码、放牧思想的农场!冰糖网
兄弟没办法啊,谁都不想搞啊,项目中几乎不用
不过越大公司越考这些啊 唉 这家是迅雷的哦
第一个,计算的顺序是($a+$a)+$a++,先计算($a+$a)=2,在得到$a++的执行结果1,此时$a的值变为2,但是($a+$a)=2的结果已经存入内存,因此$a为2不影响前面的计算结果,得到(1+1)+1=3
第二个,则是先得到$d++的执行结果1,此时$d的值变为2,所以结果时2+1=3
按优先级应该是先执行++ 再执行+运算符把
你这个是先执行+然后再执行++?
第一个是非结合。此时++递增的优先级高,因此先计算$a++;
第二个是左结合,计算方向从左到右,先计算$a+$a,再计算$a++
既然$a++优先级高,那$a++ 以后 再计算$a+$a的时候 $a应该是2啊 怎么$a还会是1啊
说了第二个是连续加法,结合方向是从左至右,是先算($a+$a)=2的,之后再去计算$a++时,$a变为2,但是($a+$a)的计算结果已经进入内存,不再受$a值得影响。仔细去看看手册关于优先级这部分的说明吧。注意下各种运算符的结合方向
曾经出现过这样的帖子,当时我也有点纠结。在官方手册中,出现的“运算符的结合方向”是如何考虑的。
我不太了解C的运算方式。自己的解释是:
$a = 1;
$b = $a + $a + $a++;//PHP先计算了$a + $a(尽管$a++的优先级高),最后得到3
$d = 1;
$c = $d + $d++ //PHP先计算了$d++,在计算$d +
不知道这样理解对不对。
我不太了解C的运算方式。自己的解释是:
$a = 1;
$b = $a + $a + $a++;//PHP先计算了$a + $a(尽管$a++的优先级高),最后得到3
$d = 1;
$c = $d + $d++ //PHP先计算了$d++,在计算$d +
不知道这样理解对不对。
我觉得对的,其实加上括号就可以更明显的看出来优先级了。
($a + $a++);//由执行顺序//1、$a++//2、$a//再由$a = 1;$b = $a++;echo($a.'-'.$b);//结果$a为2, $b为1 即($a++)结果为1;//所以$a + $a++ = 2+1 为3//式1$b = $a + ($a + ($a + ($a + $a++)));//执行顺序//1、($a + $a++) //2、($a + ($a + $a++))//...括号优先//因为1先被执行,所以$a变量值被改变了,后面的顺序执行都会按改变的值计算//结果:$b = 2 + (2 + (2 + (2 + 1)));//式2$b = $a + $a + $a + ($a + $a++);//等同于$b = (($a + $a) + $a) + ($a + $a++);//执行顺序//1、($a + $a)//2、($a + $a) + $a)//......括号优先,相同符号 不加括号顺序执行//执行结果为$b = 1+1+1+($a + $a++) 即$b=3+(2+1)//最后$b = $a+$a++;//等同 式1$b = $a+$a+$a++;//等同 式2
就算知道茴香豆的回有好几种写法那又怎样,会一种就行了。其他的你一辈子都用不到
C那边叫Undefined behaviour(未定义行为),标准里面没规定,如何实现是编译器的事情,不同的编译器得到不同结果是正常的,或者有的根本编译不了。
研究这些东西做甚。

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

Alipay PHP...

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,

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

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.

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...
