简体   繁体   中英

if not working with decrement/increment

Hi I´m new in php and in one book i found this:

$x = 0;    
if ($x-- == 0) echo $x;

in book they write it should write -1 to my page, because decremenet was done after comparison and before the command exho was executed. I understand why it is that, but when I type it into my editor it's giving me error because it's understanding $x-- as one and $x as another variable. Where's problem?

Basic Steps :

Step 1: (get Notepad++ )

Go to http://notepad-plus-plus.org/ and download Notepad++ .
Notepad++ is a free (as in "free speech" and also as in "free beer") source code editor and Notepad replacement that supports several languages. Running in the MS Windows environment, its use is governed by GPL License.

Step 2: (type the code)

type and save as "filename.php" in "www directory"

<?php

$x = 0;    
if ($x-- == 0) echo $x;

?>

Step 3: (enJoy !)

Check the result

Expected Output : -1 // this is what i got! :D

$x-- is postdecrement that is the $x value will be still 0. when you went away from the statement then only the $x value should be change. so if statement is working fine. the echo value will be -1.

7-isnotbad answer is correct.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM