<code>$a=3; $b=6; $c=1; if($a=5||$b=7 && $c=10){ var_dump($a, $b,$c);//bool(true) int(6) int(1) $a++; $b++; } var_dump($a, $b,$c);//bool(true) int(7) int(1) </code>
请问为什么会这样输出?
回复内容:
<code>$a=3; $b=6; $c=1; if($a=5||$b=7 && $c=10){ var_dump($a, $b,$c);//bool(true) int(6) int(1) $a++; $b++; } var_dump($a, $b,$c);//bool(true) int(7) int(1) </code>
请问为什么会这样输出?
虽然你的这个判断写的奇奇怪怪的,不过我想应该是故意的吧,应该不是书写BUG。
这个并不是PHP的BUG,而是你自己没有好好理解运算符的优先级的问题,建议看看这个:
- 运算符优先级
- 逻辑运算符
把你的那个判断加上括号,那么你可以这么理解这个逻辑判断
<code>$a = ( 5 || ( ( $b = 7 ) && ( $c = 10 ) ) ) </code>
你可以把括号加上放到程序中测试,结果也还是一样的。从上面我们就可以看出,由于=
的优先级是最低的,所以先执行了( 5 || ( ( $b = 7 ) && ( $c = 10 ) ) )
。由于||
和&&
的操作符是同级的,所以从左到右进行操作,5
这个数字被程序判断为true
。至于为什么可以看手册:转换为布尔值
根据或
操作原则。二者中只要有一者判断为true
则立即结束判断,所以或
操作后面的都不执行了。最后将true
赋值给$a
变量。这就是为什么只有$a
变量的值变了,而$b
和$c
没变的原因。
你可以把逻辑判断改成这样:
<code>$a=0 || $b=7 && $c=10 </code>
看看结果是什么?
由于0
被程序认定为false
,所以或操作得以继续执行。或操作的右边是与操作,由于是两个赋值操作,所以是true
,fase or true
的值也是true
,所以$a=true
,$b=7
,$c=10
另外,增加括号可以提高优先级,如果要达到复制的目的,我们可以这样做
<code>($a=0) || ($b=7) && ($c=10) //可以试试($a=5) || ($b=7) && ($c=10) </code>
补充:
看到程序主体,忘记看下面的++
了,其实下面还有一个需要我们注意的就是$a++
这个操作,++
是递增操作,但是为什么得到的结果还是true
呢?通过查阅手册,我们能发现,虽然很短,但是这句话对我们的帮助非常之大:
递增或递减布尔值没有效果。 —— 递增/递减运算符
至此,程序的真相大白于天下了。至于为什么PHP会做这样的设定,可能是因为PHP是用C编写的原因(个人臆测,不可信)。
所以程序告诉我们一件事,所想并非所得,要善于怀疑自己。以上!
if($a=5||$b=7 && $c=10)
你这个判断有问题,判断相等用的是==

DependencyInjection(DI)inPHPenhancescodeflexibilityandtestabilitybydecouplingdependencycreationfromusage.ToimplementDIeffectively:1)UseDIcontainersjudiciouslytoavoidover-engineering.2)Avoidconstructoroverloadbylimitingdependenciestothreeorfour.3)Adhe

ToimproveyourPHPwebsite'sperformance,usethesestrategies:1)ImplementopcodecachingwithOPcachetospeedupscriptinterpretation.2)Optimizedatabasequeriesbyselectingonlynecessaryfields.3)UsecachingsystemslikeRedisorMemcachedtoreducedatabaseload.4)Applyasynch

Yes,itispossibletosendmassemailswithPHP.1)UselibrarieslikePHPMailerorSwiftMailerforefficientemailsending.2)Implementdelaysbetweenemailstoavoidspamflags.3)Personalizeemailsusingdynamiccontenttoimproveengagement.4)UsequeuesystemslikeRabbitMQorRedisforb

DependencyInjection(DI)inPHPisadesignpatternthatachievesInversionofControl(IoC)byallowingdependenciestobeinjectedintoclasses,enhancingmodularity,testability,andflexibility.DIdecouplesclassesfromspecificimplementations,makingcodemoremanageableandadapt

The best ways to send emails using PHP include: 1. Use PHP's mail() function to basic sending; 2. Use PHPMailer library to send more complex HTML mail; 3. Use transactional mail services such as SendGrid to improve reliability and analysis capabilities. With these methods, you can ensure that emails not only reach the inbox, but also attract recipients.

Calculating the total number of elements in a PHP multidimensional array can be done using recursive or iterative methods. 1. The recursive method counts by traversing the array and recursively processing nested arrays. 2. The iterative method uses the stack to simulate recursion to avoid depth problems. 3. The array_walk_recursive function can also be implemented, but it requires manual counting.

In PHP, the characteristic of a do-while loop is to ensure that the loop body is executed at least once, and then decide whether to continue the loop based on the conditions. 1) It executes the loop body before conditional checking, suitable for scenarios where operations need to be performed at least once, such as user input verification and menu systems. 2) However, the syntax of the do-while loop can cause confusion among newbies and may add unnecessary performance overhead.

Efficient hashing strings in PHP can use the following methods: 1. Use the md5 function for fast hashing, but is not suitable for password storage. 2. Use the sha256 function to improve security. 3. Use the password_hash function to process passwords to provide the highest security and convenience.


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

Atom editor mac version download
The most popular open source editor

Dreamweaver Mac version
Visual web development tools

SublimeText3 Chinese version
Chinese version, very easy to use

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 English version
Recommended: Win version, supports code prompts!
