Tipe Data, Variabel, dan Konstanta PHP Operatorso
โก Ringkasan Cerdas
PHP Data Types, Variables, and Operators form the foundation of every PHP script. This walkthrough explains how PHP infers integer, float, string, and Boolean types, how variables store data, how type casting works, and how arithmetic, assignment, comparison, and logical operators behave.

Tipe Data PHP
A data type is the classification of data into a category according to its attributes:
- Karakter alfanumerik diklasifikasikan sebagai string
- Whole numbers are classified as integers
- Numbers dengan titik desimal diklasifikasikan sebagai titik mengambang.
- Nilai benar atau salah diklasifikasikan sebagai Boolean.
PHP is a loosely typed language; it does not have explicitly defined data types. PHP determines the data types by analyzing the attributes of the data supplied. PHP implicitly supports the following data types:
- Integer โ whole numbers e.g. -3, 0, 69. The maximum value of an integer is platform-dependent. On a 32 bit machine, it is usually around 2 billion. 64 bit machines usually have larger values. The constant PHP_INT_MAX is used to determine the maximum value.
<?php
echo PHP_INT_MAX;
?>
Keluaran:
9223372036854775807
- Floating point number โ decimal numbers e.g. 3.14. They are also known as double or real numbers. The maximum value of a float is platform-dependent. Floating point numbers are larger than integers.
- String karakter โ misalnya Hello World
- Boolean โ misalnya Benar atau salah.
Before we go into more detail discussing PHP data types, let us first discuss variables.
Variabel PHP
Variabel adalah nama yang diberikan ke lokasi memori yang menyimpan data saat runtime.
Cakupan suatu variabel menentukan visibilitasnya.
A PHP global variable is accessible to all the scripts in an application.
Variabel lokal hanya dapat diakses oleh skrip yang telah didefinisikan.
Think of a variable as a glass containing water. You can add water into the glass, drink all of it, refill it again etc. The same applies to variables.
Variables are used to store data and provide the stored data when needed. Just like in other programming languages, PHP supports variables too. Let us now look at the rules followed when creating variables in PHP.
- All variable names must start with the dollar sign, e.g.
- Nama variabel peka huruf besar-kecil; ini berarti $my_var berbeda dari $MY_VAR
- All variable names must start with a letter, followed by other characters e.g. $my_var1. $1my_var is not a legal variable name.
- Variable names must not contain any spaces. โ$first nameโ is not a legal variable name. You can instead use an underscore in place of the space e.g. $first_name. You cannot use characters such as the dollar or minus sign to separate variable names.
Let us now look at how PHP determines the data type depending on the attributes of the supplied data.
<?php
$my_var = 1;
echo $my_var;
?>
Keluaran:
1
Angka titik mengambang
<?php
$my_var = 3.14;
echo $my_var;
?>
Keluaran:
3.14
String karakter
<?php $my_var = "Hypertext Pre Processor"; echo $my_var; ?>
Keluaran:
Hypertext Pre Processor
Penggunaan Variabel
Variabel membantu memisahkan data dari algoritma program.
Algoritma yang sama dapat digunakan untuk nilai data masukan yang berbeda.
Misalnya, anggaplah Anda sedang mengembangkanping a calculator program that adds up two numbers. You can create two variables that accept the numbers, then you use the variable names in the expression that does the addition.
Pengecoran Tipe Variabel
Melakukan perhitungan aritmatika menggunakan variabel dalam bahasa seperti C# mengharuskan variabel bertipe data sama.
Type casting adalah mengubah suatu variabel atau nilai menjadi tipe data yang diinginkan.
Ini sangat berguna ketika melakukan perhitungan aritmatika yang memerlukan variabel bertipe data yang sama.
Type casting di PHP dilakukan oleh interpreter.
Dalam bahasa lain seperti C#, Anda harus menggunakan variabel. Kode di bawah ini menunjukkan tipe casting di C#.
Diagram di bawah menunjukkan implementasi PHP dari contoh di atas.
PHP also allows you to cast the data type. This is known as explicit casting. The code below demonstrates explicit type casting.
<?php
$a = 1;
$b = 1.5;
$c = $a + $b;
$c = $a + (int) $b;
echo $c;
?>
Keluaran:
2
The above code outputs 2. The var_dump function is used to determine the data type. The code below demonstrates how to use the var_dump function.
<?php $a = 1; var_dump($a); $b = 1.5; var_dump($b); $c = "I Love PHP"; var_dump($c); $d = true; var_dump($d); ?>
Keluaran:
int(1) float(1.5) string(10) "I Love PHP" bool(true)
Konstanta PHP
Definisikan konstanta โ a constant is a variable whose value cannot be changed at runtime.
Misalkan kita sedang mengembangkanping a program that uses the value of PI 3.14. We can use a constant to store its value.
Let us now look at an example that defines a constant.
<?php define('PI', 3.14); // creates a constant with a value of 3.14 echo PI; ?>
Once you define PI as 3.14, writing code like PI = 4; will generate an error, because PI has been defined as a constant and therefore assigning a value to it is not permissible.
PHP Operatorso
Operator aritmatika
Arithmetic operators are used to perform arithmetic operations on numeric data. The concatenation operator works on string nilai juga. PHP mendukung operator berikut.
| Operator | Nama | Deskripsi | Example | Keluaran |
|---|---|---|---|---|
| + | Tambahan | Penjumlahan x dan y | 1+1; | 2 |
| - | Subtracproduksi | Selisih antara x dan y | 1 - 1; | 0 |
| * | Perkalian | Kalikan x dan y | 3*7; | 21 |
| / | Divisi | Hasil bagi x dan y | 45/5; | 9 |
| % | Modul PHP | Gives the remainder of dividing x and y | 10% 3; | 1 |
| -n | yang negasi | Mengubah n menjadi bilangan negatif | -(-5); | 5 |
| X . kamu | Rangkaian | Gabungkan x dan y | โPHPโ. โ BATUโ;10 . 3; | PHP ROCKS103 |
Penugasan Operatorso
Operator penugasan digunakan untuk memberikan nilai pada variabel. Mereka juga dapat digunakan bersama dengan operator aritmatika.
| Operator | Nama | Deskripsi | Example | Keluaran |
|---|---|---|---|---|
| x = kamu | tugas | Assigns the value of y to x | $ x = 5; | 5 |
| x += kamu | tambahan | Increments the value of x by y | $x = 2;$x += 1; | 3 |
| x -= kamu | di bawahtracproduksi | Subtracts y from the value of x | $x = 3;$x -= 2; | 1 |
| x *= kamu | perkalian | Multiplies the value of x, y times | $x = 0;$x *= 9; | 0 |
| x /= kamu | divisi | Hasil bagi x dan y | $x = 6;$x /= 3; | 2 |
| x %= kamu | modulus | The remainder of dividing x by y | $x = 3;$x %= 2; | 1 |
| x .= y | menggabungkan | Menyatukan item | $x = โPrettyโ;$x .= โ Cool!โ; | Cukup Keren! |
Operator perbandingan
Operator perbandingan digunakan untuk membandingkan nilai dan tipe data.
| Operator | Nama | Deskripsi | Example | Keluaran |
|---|---|---|---|---|
| x == y | Sama | Compares x and y, then returns true if they are equal | 1 == โ1โ; | Benar atau 1 |
| x === y | identik | Membandingkan nilai dan tipe data. | 1 === โ1โ; | False or 0, since 1 is an integer and โ1โ is a string |
| x != y, x <> y | PHP Tidak sama | Compares values of x and y, returns true if the values are not equal | 2 != 1; | Benar atau 1 |
| x > y | Lebih besar dari | Compares values of x and y, returns true if x is greater than y | 3 > 1; | Benar atau 1 |
| x < y | Less dari | Compares values of x and y, returns true if x is less than y | 2 <1; | Salah atau 0 |
| x >= y | Lebih besar dari atau sama | Compares values of x and y, returns true if x is greater than or equal to y | 1 >= 1 | Benar atau 1 |
| x <= y | Less dari atau sama | Compares values of x and y, returns true if x is less than or equal to y | 8 <= 6 | Salah atau 0 |
operator logika
Saat bekerja dengan operator logika, angka apa pun yang lebih besar atau kurang dari nol (0) bernilai benar. Nol (0) bernilai salah.
| Operator | Nama | Deskripsi | Example | Keluaran |
|---|---|---|---|---|
| x and y, x && y | Dan | Returns true if both x and y are true | 1 and 4;True && False; | Benar atau 1Salah atau 0 |
| x or y, x || y | Or | Mengembalikan nilai benar jika x atau y benar | 6 atau 9;0 || 0; | Benar atau 1Salah atau 0 |
| x xor y | Eksklusif atau, xor | Mengembalikan nilai benar jika hanya x yang benar atau hanya y yang benar | 1 xor 1;1 xor 0; | Salah atau 0Benar atau 1 |
| !x | Tidak | Mengembalikan nilai benar jika x salah dan salah jika x benar | !0; | Benar atau 1 |


