PHP Operators (PHPO)

1. PHP Arithmetic Operators

| Jenis Operator | Operator | Contoh   | Keterangan                    |
|----------------|----------|----------|-------------------------------|
| Aritmatika     | +        | $a + $b; | Penjumlahan                   |
|                | -        | $a - $b; | Pengurangan                   |
|                | *        | $a * $b; | Perkalian                     |
|                | /        | $a / $b; | Pembagian                     |
|                | %        | $a % $b; | Modulus / Sisa dari pembagian |

As usual, I assume that you have installed  Wampserver  first, then place the php files below in the www directory of wampserver.

Example:

Save this code with the name "form_aritmetic.php"

<html>
<!--
*bundet.com
*Wawan Beneran
*Form Operator Aritmatik PHP
-->
<head>
<title>Belajar PHP </title>
</head>
<body>
<h2>Mengenal Operator Aritmatik PHP</h2>
<style type="text/css">
#mhs{
 position: absolute;
 left: 60px;
}
</style>
<form action="aritmatika.php" method="POST">
nilai a<input id="mhs" type="text" name="nilai_a"><br>
nilai b<input id="mhs" type="text" name="nilai_b"><br>
<input id="mhs" style=" margin-top: 10px;" type="submit" value="Kirim">
</form>
</body>
</html>

Save this code with the name "arithmetic.php"

<html>
<head>
<title>Belajar PHP </title>
</head>
<body>
<h2>Mengenal Operator Aritmatik PHP</h2>
<?php
/**
*bundet.com
*Wawan Beneran
*Mengenal Operator Aritmatik PHP
*/
 $nilai_a=$_REQUEST["nilai_a"];
 $nilai_b=$_REQUEST["nilai_b"];
 $nilai_jml=$nilai_a + $nilai_b;
 $nilai_krg=$nilai_b - $nilai_a;
 $nilai_kali=$nilai_a * $nilai_b;
 $nilai_bagi=$nilai_b / $nilai_a;
 $nilai_mod=$nilai_b % $nilai_a;
 printf("Nilai A:  <b>$nilai_a</b><br>");
 printf("Nilai B:  <b>$nilai_b</b><br><br>");
 printf("Hasil A + B: <br><b>$nilai_jml</b><br>");
 printf("Hasil B - A: <br><b>$nilai_krg</b><br>");
 printf("Hasil A x B: <br><b>$nilai_kali</b><br>");
 printf("Hasil B : A: <br><b>$nilai_bagi</b><br>");
 printf("Modulus(Sisa) B : A: <br><b>$nilai_mod</b><br>");
?>
</body>
</html>

PHP Arithmetic Demo:

https://youtu.be/CxRpiBt9OQ4

2. PHP Assignment Operators

| Jenis Operator | Operator | Contoh    | Keterangan                      |
|----------------|----------|-----------|---------------------------------|
| Penugasan      | =        | $a = 4;   | $a diisi dengan 4               |
|                | +=       | $a += $b; | Sama saja a = a+b (Penambahan)  |
|                | -=       | $a -= $b; | Sama saja a = a-b (Pengurangan) |
|                | *=       | $a *= $b; | Sama saja a = a*b (Perkalian)   |
|                | /=       | $a /= $b; | Sama saja a = a/b (Pembagian)   |

Example 1:

<?php
$nilai_a=7;
printf("Nilai A:  $nilai_a
");
?>

I think my friends must already know what the output of the assignment example above looks like:

See the results -> 

Nilai A: 7

Example 2:

<?php
$x = 20;
$x += 100;

echo $x;
?>

See the results -> 

120

Example 3:

<?php
$x = 50;
$x -= 30;

echo $x;
?>

See the results -> 

20

Example 4:

<?php
$x = 10;
$y = 6;

echo $x * $y;
?>

See the results -> 

60

Example 5:

<?php
$x = 10;
$x /= 5;

echo $x;
?>

See the results -> 

2

3. PHP Comparison Operators

| Jenis Operator | Operator | Contoh     | Keterangan              |
|----------------|----------|------------|-------------------------|
| Perbandingan   | ==       | $a == $b;  | Sama dengan             |
|                | ===      | $a === $b; | Identik                 |
|                | !=       | $a != $b;  | Tidak sama dengan       |
|                | <>       | $a <> $b;  | Tidak sama dengan       |
|                | !==      | $a !== $b; | Tidak identic           |
|                | <        | $a < $b;   | Kurang dari             |
|                | >        | $a > $b;   | Lebih dari              |
|                | <=       | $a <= $b;  | Kurang dari sama dengan |
|                | >=       | $a >= $b;  | Lebih dari sama dengan  |

Example 1:

<?php
$x = 100;
$y = "100";

var_dump($x == $y); // Mengembalikan pernyataan true, karena nilai yang dibandingkan sama
meski tipe datanya beda
?>

See the results -> 

boolean true

Example 2:

<?php
$x = 100;
$y = "100";
$z = 100;
$a = 101;

var_dump($x === $y); // Mengembalikan pernyataan "false", karena tipe yang dibandingkan berbeda.
var_dump($x === $z); // Mengembalikan pernyataan "true", karena tipe dan nilai yang dibandingkan sama.
var_dump($x === $a); // Mengembalikan pernyataan "flase", karena nilainya beda meski tipenya sama.

// NB: Meskipun tipe datanya sama tapi jika nilainya berbeda maka akan dianggap "false" (tidak identik).

?>

See the results -> 

boolean false
boolean true
boolean false

Example 3:

<?php
$x = 100;
$y = "100";
$z = 101;
var_dump($x != $y); // Apakah nilai x berbeda dengan nilai y? jawabannya "false", karena nilainya sama.
var_dump($x != $z); // Apakah nilai x berbeda dengan nilai z? jawabannya "true", karena nilainya tidak sama(berbeda).
?>

See the results -> 

boolean false
boolean true

Example 4:

<?php
$x = 100;
$y = "100";
$z = 101;
var_dump($x <> $y); // Apakah nilai x berbeda dengan nilai y? jawabannya "false", karena nilainya sama.
var_dump($x <> $z); // Apakah nilai x berbeda dengan nilai z? jawabannya "true", karena nilainya tidak sama(berbeda).
?>

See the results -> 

boolean false
boolean true

Example 5:

<?php
$x = 100;
$y = "100";
$z = 100;
$a = 101;
var_dump($x !== $y); // Apakah nilai x tidak identik dengan y? jawabannya "true", karena berbeda tipe data.
var_dump($x !== $z); // Apakah nilai x tidak identik dengan z? jawabannya "false", karena nilai dan tipe datanya sama.
var_dump($x !== $a); // Apakah nilai x tidak identik dengan y? jawabannya "true", karena nilainya beda meski tipenya sama.
?>

See the results -> 

boolean true
boolean false
boolean true

Example 6:

<?php
$x = 50;
$y = 100;
$z = 25;

//Dalam studi kasus ini Anda boleh menggunakan istilah "kurang dari" atau "lebih kecil dari" untuk menafsirkan suatu kondisi.
var_dump($x < $y); // Apakah nilai x lebih kecil dari y? jawabannya "true", karena 50 vs 100
var_dump($x < $z); // Apakah nilai x lebih kecil dari z? jawabannya "false", karena 50 vs 25
?>

See the results -> 

boolean true
boolean false

Example 7:

<?php
$x = 50;
$y = 100;
$z = 25;

//Dalam studi kasus ini Anda boleh menggunakan istilah "lebih besar dari" atau "lebih banyak dari" untuk menafsirkan suatu kondisi.
var_dump($x > $y); // Apakah nilai x lebih besar dari y? jawabannya "false", karena 50 vs 100
var_dump($x > $z); // Apakah nilai x lebih besar dari z? jawabannya "true", karena 50 vs 25
?>

See the results -> 

boolean false
boolean true

Example 8:

<?php
$x = 50;
$y = 100;
$z = 25;
$a = 50;

/**Dalam studi kasus ini Anda boleh menggunakan istilah-istilah dibawah ini untuk menafsirkan suatu kondisi:
1. lebih kecil atau sama dengan
2. lebih kecil atau sama
3. lebih kecil sama dengan
4. sama atau lebih kecil
5. sama atau dibawahnya
6. sama atau lebih sedikit
7. lebih sedikit atau sama dengan
8. kurang dari sama dengan.
**/

var_dump($x <= $y); // Apakah nilai x lebih kecil atau sama dengan y? jawabannya "true", karena 50 vs 100
var_dump($x <= $z); // Apakah nilai x lebih kecil atau sama dengan z? jawabannya "false", karena 50 vs 25
var_dump($x <= $a); // Apakah nilai x lebih kecil atau sama dengan a? jawabannya "true", karena 50 vs 50
?>

See the results -> 

boolean true
boolean false
boolean true

Example 9:

<?php
$x = 50;
$y = 100;
$z = 25;
$a = 50;

/**Dalam studi kasus ini Anda boleh menggunakan istilah-istilah dibawah ini untuk menafsirkan suatu kondisi:
1. lebih besar atau sama dengan
2. lebih besar atau sama
3. lebih besar sama dengan
4. sama atau lebih besar
5. sama atau diatasnya
6. sama atau lebih banyak
7. lebih banyak atau sama dengan
8. lebih dari sama dengan.
*/

var_dump($x >= $y); // Apakah nilai x lebih besar atau sama dengan y? jawabannya "false", karena 50 vs 100
var_dump($x >= $z); // Apakah nilai x lebih besar atau sama dengan z? jawabannya "true", karena 50 vs 25
var_dump($x >= $a); // Apakah nilai x lebih besar atau sama dengan a? jawabannya "true", karena 50 vs 50
?>

See the results -> 

boolean false
boolean true
boolean true

4. PHP Operator Increment / Decrement

| Jenis Operator | Operator | Contoh/Model   | Keterangan                                                                                                    |
|----------------|----------|----------------|---------------------------------------------------------------------------------------------------------------|
| Increment      | ++$x     | Pre-increment  | Penaikan nilai sejumlah 1 telah dilakukan sebelum variabel, sehingga variabel   didepannya akan terpengaruh.  |
|                | $x++     | Post-increment | Penaikan nilai sejumlah 1 dilakukan setelah variabel, sehingga variabel dibelakangnya belum terpengaruh.      |
|  Decrement     | --$x     | Pre-decrement  | Penurunan nilai sejumlah 1 telah dilakukan sebelum variabel, sehingga variabel   didepannya akan terpengaruh. |
|                | $x--     | Post-decrement | Penurunan nilai sejumlah 1 dilakukan setelah variabel, sehingga variabel dibelakangnya belum terpengaruh.     |

Tip: To make it easier to understand, illustrate that the program execution process starts from LEFT >> to RIGHT, as you read this text.

Example:

<?php
$a = 10;
$b = 10;
$c = 10;
$d = 10;

//Untuk mengetahui perbedaannya, maka masing-masing model sengaja kita kasih variabel yang berbeda.
echo ++$a;  //Pre-increment
echo "<br>";
echo $b++;  //Post-increment
echo "<br>";
echo --$c;  //Pre-decrement
echo "<br>";
echo $d--;  //Post-decrement
?>

See the results -> 

11
10
9
10

5. PHP Logical Operators

| Jenis Operator | Operator | Contoh      | Keterangan                                     |
|----------------|----------|-------------|------------------------------------------------|
| Logika         | And      | $a and $b;  | TRUE jika $a dan $b TRUE                       |
|                | Or       | $a or $b;   | TRUE jika $a dan/atau $b TRUE                  |
|                | Xor      | $a xor $b;  | TRUE jika $a atau $b TRUE, tapi tidak keduanya |
|                | &&       | $a && $b;   | TRUE jika $a dan $b TRUE                       |
|                | \|\|     | $a \|\| $b; | TRUE jika $a dan/atau $b TRUE                  |
|                | !        | !$a         | TRUE jika $a FALSE                             |

Example 2:

<?php
$x = 100;
$y = 50;

if ($x == 100 or $y == 66) {
    echo "Hello world!, Aku tampil karena x bersama y, dan kondisi salah satu atau keduanya BENAR (meskipun y SALAH)";
}
?>

See the results -> 

Hello world!, Aku tampil karena x bersama y, dan kondisi salah satu atau keduanya BENAR (meskipun y SALAH)

Example 3:

<?php
$x = 100;
$y = 50;

if ($x == 100 xor $y == 66) {
    echo "Hello world!, Aku tampil karena x bersama y, dan cukup salah satu saja yang BENAR, tidak boleh keduanya. (y SALAH)";
}
?>

See the results -> 

Hello world!, Aku tampil karena x bersama y, dan cukup salah satu saja yang BENAR, tidak boleh keduanya. (y SALAH)

Example 4:

<?php
$x = 100;
$y = 50;

if ($x == 100 && $y == 50) {
    echo "Hello world!, Aku tampil karena x bersama y, dan kondisi keduanya BENAR";
}
?>

See the results -> 

Hello world!, Aku tampil karena x bersama y, dan kondisi keduanya BENAR

Example 5:

<?php
$x = 100;
$y = 50;

if ($x == 100 || $y == 66) {
    echo "Hello world!, Aku tampil karena x bersama y, dan kondisi salah satu atau keduanya BENAR (meskipun y SALAH)";
}
?>

See the results -> 

Hello world!, Aku tampil karena x bersama y, dan kondisi salah satu atau keduanya BENAR (meskipun y SALAH)

Example 6:

<?php
$x = 100;
$y = 10;

//Cara penulisannya boleh != atau !==
if ($x !== $y) {
    echo "Hello world!, Aku tampil karena x tidak sama dengan y";
}
?>

See the results -> 

Hello world!, Aku tampil karena x tidak sama dengan y

6. PHP String Operators

| Jenis Operator | Operator | Contoh    | Keterangan                                                                                             |
|----------------|----------|-----------|--------------------------------------------------------------------------------------------------------|
| String         | .        | $a . $b;  | Disebut “Concatenation” yaitu penggabungan dua string dalam dua variabel, contoh $a dan $b             |
|                | .=       | $a .= $b; | Disebut “Concatenation assignment” penggabungan dua string dalam satu variabel, contoh: $a = $a dan $b |

Example:

<?php
$txt1 = "Hello";
$txt2 = " world!";

//Contoh 1
echo $txt1 . $txt2;
echo "<br>";

//Contoh 2
$txt1 .= $txt2;
echo $txt1;

?>

See the results -> 

Hello world!
Hello world!

PHP String Concatenation Operators

In PHP, strings can be combined using the . (dot) operator. This is usually applied to cases where strings are stored in certain variables, then one or more of these variables are to be displayed together with the plain text string.

Example:

$string1 = "Hello";
$string2 = "World";
echo("Ini string tergabung: ".$string1." ".$string2);

Output:

Ini string tergabung: Hello World

That's how to concatenate strings in php, good luck!

Get to know the PHP Scope Resolution Operator

OOP in PHP recognizes the term Scope Resolution Operator or commonly called "Paamayim Nekudotayim" sometimes also called "double colon". Scope Resolution Operator in OOP in PHP is usually used to access properties and methods in a class without having to instantiate the class you want to use. In the discussion of Scope Resolution Operator we will get to know the keywords {parent, self, and static}.

To make it clearer, I tried to create a class test to illustrate what the Scope Resolution Operator is.

<?php
    class test {
        public $job = "PHP Developer";

        public function sebut_nama() {
            return "adiputra";
        }

        public function sebut_kerjaan() {
            return $this->job;
        }
    }

    echo test::sebut_nama(); // output : "adiputra"
?>

In the example above, I can access a method from a class directly without having to instantiate the class first. The rule is easy, we only mention the class name followed by the method or property name. Then if I add code like below.

<?php
    class test {
        public $job = "PHP Developer";

        public function sebut_nama() {
            return "adiputra";
        }

        public function sebut_kerjaan() {
            return $this->job;
        }
    }

    echo test::sebut_nama(); // output : "adiputra"
        echo test::sebut_kerjaan(); // error
?>

If I call the call_job() method it will produce an error like the image below:

The error appears because when calling the call_job() method there is a keyword $this where the keyword is a keyword defining an object. Because we use the scope resolution operator (not creating an object) then the error appears.

It has been explained above that the scope resolution operator cannot be separated from the parent, self and static keywords. Here I try to explain again how to use it in the case of creating an e-commerce class. I created a Product class like the code below:

<?php
    class Produk {
        const DISKON = 20;

        public static function get_diskon() {
            return self::DISKON;
        }
    }
?>

There is a constant Discount, for a simple case, each product has a fixed discount of 20 {in percent}. There is also a get_discount method whose contents are the call value of the DISCOUNT constant. There is a self keyword which is the call to the Discount constant property. The self keyword is usually used to call properties or methods in the class itself.

Then I tried to create a class baju.php

<?php

    class baju extends produk {
        public static $_dijual = "ya";

        private $_ukuran;
        private $_warna;

        public function set_ukuran($ukuran) {
            $this->_ukuran = $ukuran;
        }

        public function get_ukuran() {
            return $this->_ukuran;
        }

        public function set_warna($warna) {
            $this->_warna = $warna;
        }

        public function get_warna() {
            return $this->_warna;
        }

        public static function get_diskon() {
            return parent::get_diskon();
        }

        public static function get_dijual() {
            return self::$_dijual;
        }

    }

?>

What must be emphasized in the code above is the use of self and the use of parent. In the get_diskon method, there is a parent keyword which functions to call the get_diskon method in the parent class, namely the Product class. So we can call the parent method of a particular class with the scope resolution operator.

While in the get_dijual method there is a self keyword that calls the $_dijual property where the property is of static type. {I will explain the static keyword in the next article, God willing}

Then I tried to run it with the code below:

<?php
    include_once("produk.php");
    include_once("baju.php");

    $oBaju = new baju;
    $oBaju->set_warna("biru");
    $oBaju->set_ukuran("M");

    echo $oBaju->get_warna() . "<br />";
    echo $oBaju->get_ukuran() . "<br />";

    echo baju::get_dijual() . "<br />";
    echo baju::get_diskon() . "%<br />";
?>

The output display can be seen in the image below.

That's the simple concept of Scope Resolution Operator in OOP PHP. The code example above is only to explain the simple flow so that friends can understand Scope Resolution Operator more easily.

Hope it is useful..🙂

What is the use of double colon (::) in Programming?

Sorry bro, sorry I want to ask, when I use a framework, let's say CodeIgniter, I find a sign that I don't recognize, like :: double colon / double colon, it's before the construct, so what's the use of this, thank you for the enlightenment, greetings newbietol

Answer

It is the Scope Resolution Operator (also called Paamayim Nekudotayim) or simply double colon, which is a token that directs access to static, constant and overriding properties or methods of a class.

Source:

https://terusbelajar.wordpress.com/2011/11/05/oop-pada-php-pengenalan-scope-resolution-operator/

Debugging PHP Using Error Suppression Operators

PHP displays an error message if the built-in function (function provided by PHP) experiences an error. For example, it cannot open a file, cannot access a database, and others. When creating an application, this error message is very helpful in solving and fixing programming errors. However, when the application is finished and used in general, this error message will disturb the user.

To turn off the error message, we use the @ operator.

Usage examples:

@chdir("temp");

Under normal conditions, the chdir function will generate an error message if the temp directory is not found or cannot be accessed. By using the @ operator, PHP will "just keep quiet" if the temp directory is not found or cannot be accessed.

Getting to Know PHP Branching Operators

Branching is generally done with an if-else structure, as in the following example:

if($user == "wiro"){
echo("Welcome Wiro");
}else{
    echo("Wrong username!");
}

The same thing can be done by:

echo($user == wiro ? "Welcome Wiro" : "Wrong username");

Pay attention to the ? and : signs.

PHP checks whether the statement to the left of the "?" is true or false. If true, the statement to the left of the ":" is executed. If false, the statement to the right of the ":" is executed.

More details about branching can be learned in the Control Flow section.


Post a Comment

Previous Next

نموذج الاتصال