Increment dan Decrement adalah nama untuk operasi seperti $a++ dan $a--. Increment digunakan untuk menambah nilai variable sebanyak 1 angka, sedangkan Decrement digunakan untuk mengurang nilai variable sebanyak 1 angka. Operator ini dapat mempersingkat pembuatan kode pada PHP. Berikut 4 jenis Increment dan Decrement pada PHP :
- Pre-increment
- Post-increment
- Pre-decrement
- Post-decrement
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$x = 10; | |
echo ++$x; | |
?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$x = 10; | |
echo $x++; | |
?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$x = 10; | |
echo --$x; | |
?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$x = 10; | |
echo $x--; | |
?> |