Sumber : Sourcecodester.com
Dalam setiap bahasa pemrograman, operator memainkan peran penting. Hal ini dapat digunakan dalam laporan perbandingan seperti IF pernyataan, loop pernyataan seperti loop WHILE, menempatkan nilai ke variabel dan banyak lagi.
Comparison Operators
Operator | Description | Example |
---|---|---|
>= | is greater than or equal to | 4>=9 returns false |
<= | is less than or equal to | 5<=7 returns true |
< | is less than | 2<7 returns true |
> | is greater than | 3>9 returns false |
<> | is not equal | 2<>3 returns true |
!= | is not equal | 4!=8 returns true |
== | is equal to | 2==1 returns false |
Example of comparison operators in IF statement:
<?php If (4>=9){ //the statement here will not be executed } ?>
Arithmetic Operators
Operator | Description | Example | Result |
---|---|---|---|
* | Multiplication | x=2 x*3 | 6 |
/ | Division | 9/3 10/2 | 3 5 |
+ | Addition | x=3 x+4 | 7 |
- | Subtraction | x=1 5-x | 4 |
-- | Decrement | x=4 x-- | x=3 |
++ | Increment | x=7 x++ | x=8 |
% | Modulus (division remainder) | 5%2 10%8 10%2 | 1 2 0 |
Example of arithmetic operators in mathematics:
<?php $x = 2; $y = $x + 3; echo $y; ?>
Logical Operators
Operator | Description | Example |
---|---|---|
&& | and | x=8 y=2 (x < 10 && y > 1) returns true |
! | not | x=7 y=2 !(x==y) returns true |
|| | or | x=8 y=2 (x==8 || y==2) returns false |
Example of logical operators is IF statement:
<?php $x=8 $y=2 If ($x==8 || $y==2){ //the statement here will be executed } ?>
Assignment Operators
Operator | Example | Is The Same As |
---|---|---|
+= | a+=b | a=a+b |
*= | a*=b | a=a*b |
= | a=b | a=b |
.= | a.=b | a=a.b |
-= | a-=b | a=a-b |
%= | a%=b | a=a%b |
/= | a/=b | a=a/b |
Example of assignment operators in a variable x and y:
<?php $x = 2; $y = $x + 3; ?>
Tidak ada komentar:
Posting Komentar