Jump to content
View in the app

A better way to browse. Learn more.

Tuts 4 You

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Can someone explain why this php code prints 1 ??

Featured Replies

Posted

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

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 x86

Hello 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 by .hloire

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

if ((!0<6)==false)
goto 5*; goto 7*; so will got out of if
else 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 jump
else
{
if body
}

let's reverse:
if ((!0<6)==true&&((0<!0)==true)
if body
else
{
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                                                   1

if (!0<6==true)
JMP 5* No JMP 10* // LEAD TO IF BODY
else
if (0<!0==false)
JMP 10* // jump out of if
{
// IF BODY
}
10*:

let's rewrite:
if (!0<6==true)
jump to if body
if (0<!0==false)
jump out of if body
else
jump to if body

if (!0<6||0<!0)
jump to if body
else
jump out of if body

Ok. Everything make sense now.

Create an account or sign in to comment

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.