Apa Operator? Jawabannya dapat diberikan dengan menggunakan contoh 4 + 5 sama dengan 9.
4 dan 5 disebut operan dan + disebut operator. Ada Operator apa saja dalam bahasa Pemograman PHP?
dan Outpunya:
Berikut Contoh Code PHP nya:
Berikut Outputnya
4 dan 5 disebut operan dan + disebut operator. Ada Operator apa saja dalam bahasa Pemograman PHP?
- Arithmetic Operators
- Comparison Operators
- Logical (or Relational) Operators
- Assignment Operators
- Conditional (or ternary) Operators.
Mari kita bahas satu per satu operator diatas
Arithmetic Operator
Mari kita asumsikan variable A = 10 dan Variable B = 20
| Operator | Description | Example |
|---|---|---|
| + | Penambahan | A + B = 30 |
| - | Pengurangan | A - B = 10 |
| * | Perkalian | A * B = 200 |
| / | Pembagian | A / B = 2 |
| % | sisa setelah pembagian | A % B = 0 |
| % | sisa setelah pembagian | A % B = 0 |
| ++ | meningkatkan nilai integer 1 | A ++ B = 1 |
| -- | mengurankan nilai integer 1 | A -- B = 9 |
Berikut Contoh Code PHP nya
?php
$a = 42;
$b = 20;
$c = $a + $b;
echo "Addtion Operation Result: $c
";
$c = $a - $b;
echo "Substraction Operation Result: $c
";
$c = $a * $b;
echo "Multiplication Operation Result: $c
";
$c = $a / $b;
echo "Division Operation Result: $c
";
$c = $a % $b;
echo "Modulus Operation Result: $c
";
$c = $a++;
echo "Increment Operation Result: $c
";
$c = $a--;
echo "Decrement Operation Result: $c
";
?
dan Outpunya:
Addtion Operation Result: 62 Substraction Operation Result: 22 Multiplication Operation Result: 840 Division Operation Result: 2.1 Modulus Operation Result: 2 Increment Operation Result: 42 Decrement Operation Result: 43
Comparison Operators
Mari kita asumsikan variable A = 10 dan Variable B = 20
| Operator | Description | Example |
|---|---|---|
| == | Memeriksa apakah nilai dari dua operan yang sama atau tidak, jika ya maka kondisi menjadi true/benar. | (A == B) is not true. |
| != | Memeriksa apakah nilai dari dua operan yang sama atau tidak, jika nilai-nilai yang tidak sama maka kondisi menjadi true/benar | (A != B) is true. |
| >> | Memeriksa apakah nilai operan kiri lebih besar dari nilai operan kanan, jika ya maka kondisi menjadi true/benar. | (A > B) is not true. |
| < | Memeriksa apakah nilai operan kiri lebih kecil dari nilai operan kanan, jika ya maka kondisi menjadi true/benar. | (A < B) is true. |
| >= | Memeriksa apakah nilai operan kiri lebih besar sama dengan dari nilai operan kanan, jika ya maka kondisi menjadi true/benar | (A >= B) is not true. |
| <= | Memeriksa apakah nilai operan kiri lebih kecil sama dengan dari nilai operan kanan, jika ya maka kondisi menjadi true/benar | (A <= B) is true. |
Berikut Contoh Code PHP nya:
?php
$a = 42;
$b = 20;
if( $a == $b ) {
echo "TEST1 : a is equal to b
";
}else {
echo "TEST1 : a is not equal to b
";
}
if( $a > $b ) {
echo "TEST2 : a is greater than b
";
}else {
echo "TEST2 : a is not greater than b
";
}
if( $a < $b ) {
echo "TEST3 : a is less than b
";
}else {
echo "TEST3 : a is not less than b
";
}
if( $a != $b ) {
echo "TEST4 : a is not equal to b
";
}else {
echo "TEST4 : a is equal to b
";
}
if( $a >= $b ) {
echo "TEST5 : a is either greater than or equal to b
";
}else {
echo "TEST5 : a is neither greater than nor equal to b
";
}
if( $a <= $b ) {
echo "TEST6 : a is either less than or equal to b
";
}else {
echo "TEST6 : a is neither less than nor equal to b
";
}
?
Berikut Outputnya :
TEST1 : a is not equal to b TEST2 : a is greater than b TEST3 : a is not less than b TEST4 : a is not equal to b TEST5 : a is either greater than or equal to b TEST6 : a is neither less than nor equal to b
Logical Operators
Mari kita asumsikan variable A = 10 dan Variable B = 20
| Operator | Description | Example |
|---|---|---|
| and | Disebut Logical AND operator. Jika kedua operan adalah benar maka kondisi menjadi true/benar | (A and B) is true. |
| or | Disebut Logical OR Operator. Jika salah satu dari dua operan bukan 0 maka kondisi menjadi true/benar. | (A or B) is true. |
| >&& | Disebut Logical AND Operator. Jika kedua dari dua operan bukan 0 maka kondisi menjadi true/benar | (A && B) is true. |
| || | Memeriksa apakah nilai operan kiri lebih kecil dari nilai operan kanan, jika ya maka kondisi menjadi true/benar. | (A || B) is true. |
| >! | Disebut Logical NOT Operator. Gunakan untuk membalikkan keadaan logis dari operan nya. Jika kondisi benar maka Logical operator NOT akan menjadi false. | !(A && B) is false. |
Berikut Contoh Code PHP nya:
?php
$a = 42;
$b = 0;
if( $a && $b ) {
echo "TEST1 : Both a and b are true
";
}else{
echo "TEST1 : Either a or b is false
";
}
if( $a and $b ) {
echo "TEST2 : Both a and b are true
";
}else{
echo "TEST2 : Either a or b is false
";
}
if( $a || $b ) {
echo "TEST3 : Either a or b is true
";
}else{
echo "TEST3 : Both a and b are false
";
}
if( $a or $b ) {
echo "TEST4 : Either a or b is true
";
}else {
echo "TEST4 : Both a and b are false
";
}
$a = 10;
$b = 20;
if( $a ) {
echo "TEST5 : a is true
";
}else {
echo "TEST5 : a is false
";
}
if( $b ) {
echo "TEST6 : b is true
";
}else {
echo "TEST6 : b is false
";
}
if( !$a ) {
echo "TEST7 : a is true
";
}else {
echo "TEST7 : a is false
";
}
if( !$b ) {
echo "TEST8 : b is true
";
}else {
echo "TEST8 : b is false
";
}
?
Berikut Outputnya
TEST1 : Either a or b is false TEST2 : Either a or b is false TEST3 : Either a or b is true TEST4 : Either a or b is true TEST5 : a is true TEST6 : b is true TEST7 : a is false TEST8 : b is false
Assignment Operators
Berikut Contoh code PHP Assignment Operator
?php
$a = 42;
$b = 20;
$c = $a + $b; /* Assignment operator */
echo "Addtion Operation Result: $c
";
$c += $a; /* c value was 42 + 20 = 62 */
echo "Add AND Assigment Operation Result: $c
";
$c -= $a; /* c value was 42 + 20 + 42 = 104 */
echo "Subtract AND Assignment Operation Result: $c
";
$c *= $a; /* c value was 104 - 42 = 62 */
echo "Multiply AND Assignment Operation Result: $c
";
$c /= $a; /* c value was 62 * 42 = 2604 */
echo "Division AND Assignment Operation Result: $c
";
$c %= $a; /* c value was 2604/42 = 62*/
echo "Modulus AND Assignment Operation Result: $c
";
?
Berikut Outputnya
Addtion Operation Result: 62 Add AND Assigment Operation Result: 104 Subtract AND Assignment Operation Result: 62 Multiply AND Assignment Operation Result: 2604 Division AND Assignment Operation Result: 62 Modulus AND Assignment Operation Result: 20
Conditional Operator
Berikut Contoh code PHP Conditional Operator
?php
$a = 10;
$b = 20;
/* If condition is true then assign a to result otheriwse b */
$result = ($a > $b ) ? $a :$b;
echo "TEST1 : Value of result is $result
";
/* If condition is true then assign a to result otheriwse b */
$result = ($a < $b ) ? $a :$b;
echo "TEST2 : Value of result is $result
";
?
Berikut Outputnya
TEST1 : Value of result is 20 TEST2 : Value of result is 10
Sumber: Tutorial Points

EmoticonEmoticon