Posted 8 hours ago8 hr Can someone explain why this php code prints 1 ??<?php $n=0; if ($n<6&&$n>0) { echo ++$i."\n"; ++$n; } the value of $n is not > 0 but exactly 0.Php 7.4 x86 Edited 8 hours ago8 hr by CodeExplorer
8 hours ago8 hr 17 minutes ago, CodeExplorer said:Can someone explain why this php code prints 1 ??<?php $n=0; if ($n<6&&$n>0) { echo ++$i."\n"; ++$n; } the value of $n is not > 0 but exactly 0.Php 7.4 x86Hello Friend,I Just noticed something about in your snippet. The code prints 1 not because of $n, but because $i was never defined. In PHP, doing ++$i on an undefined variable automatically converts NULL to 1, which is why you get 1 as output. Edited 8 hours ago8 hr by .hloire
6 hours ago6 hr Author <?php $n=0; if ($n<6&&$n>0) { echo "Shouldn't print this"."\n"; } Won't print anything. 3 0* ASSIGN !0, 0 4 1* IS_SMALLER ~2 !0, 6 2* JMPZ_EX ~2 ~2, ->5 3* IS_SMALLER ~3 0, !0 4* BOOL ~2 ~3 5* JMPZ ~2, ->7 5 6* ECHO 'Shouldn%27t+print+this%0A' 7 7* RETURN 1if ((!0<6)==false)goto 5*; goto 7*; so will got out of ifelse if ((0<!0)==false)goto 7*; so will got out of if{ // IF BODY}7*:rewrite:if ((!0<6)==false||(0<!0)==false)out of jumpelse{if body}let's reverse:if ((!0<6)==true&&((0<!0)==true)if bodyelse{out of jump}But now let's try:if ($n<6||$n>0) { echo ++$i."\n"; ++$n; } 4 0* ASSIGN !0, 0 5 1* IS_SMALLER ~3 !0, 6 2* JMPNZ_EX ~3 ~3, ->5 3* IS_SMALLER ~4 0, !0 4* BOOL ~3 ~4 5* JMPZ ~3, ->10 6 6* PRE_INC $5 !1 7* CONCAT ~6 $5, '%0A' 8* ECHO ~6 7 9* PRE_INC !0 15 10* ECHO '+' 11* RETURN 1if (!0<6==true)JMP 5* No JMP 10* // LEAD TO IF BODYelseif (0<!0==false)JMP 10* // jump out of if{ // IF BODY}10*:let's rewrite:if (!0<6==true)jump to if bodyif (0<!0==false)jump out of if bodyelsejump to if bodyif (!0<6||0<!0)jump to if bodyelsejump out of if bodyOk. Everything make sense now.
Create an account or sign in to comment