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.

  • ๐Ÿ”ฃ Loose Typing: PHP is loosely typed, so it infers a variableโ€™s data type from the value assigned rather than an explicit declaration.
  • ๐Ÿงฑ Tipe Inti: The main scalar types are integer, floating point, string, and Boolean, each detected automatically at runtime.
  • ๐Ÿ“ฆ Variabel: A variable is a named memory location whose scope, global or local, decides where it is visible in the script.
  • ๐Ÿ” Type Casting: Casting converts a value to another type, either implicitly by the interpreter or explicitly with syntax like (int).
  • ๐Ÿ“Œ Konstanta: A constant defined with define() holds a fixed value, such as PI, that cannot change while the script runs.
  • โž• Operatorsi: Arithmetic, assignment, comparison, and logical operators manipulate and compare values, with === also checking data type.
  • ๐Ÿค– Bantuan AI: AI tools can explain PHP type juggling, flag risky loose comparisons, and clarify operator precedence with quick examples.

PHP Data Types, Variables and Operatorso

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.
  • Variabel PHP
  • Nama variabel peka huruf besar-kecil; ini berarti $my_var berbeda dari $MY_VAR
  • Variabel PHP
  • All variable names must start with a letter, followed by other characters e.g. $my_var1. $1my_var is not a legal variable name.
  • Variabel PHP
  • 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.
  • Variabel PHP

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#.

Pengecoran Tipe Variabel

Diagram di bawah menunjukkan implementasi PHP dari contoh di atas.

Pengecoran Tipe Variabel

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

Pertanyaan Umum Demo Slot

PHP has eight data types: four scalar types (integer, float, string, Boolean), two compound types (array and object), and two special types (NULL and resource). The tutorial focuses on the four scalar types.

The == operator compares only values after converting types, so 1 == โ€œ1โ€ is true. The === operator compares value and data type, so 1 === โ€œ1โ€ is false because one is an integer and one is a string.

Type juggling is PHP automatically converting a value from one type to another during an operation or loose comparison. It is convenient but can cause surprising results, which is why strict comparison with === is safer.

Yes. AI can scan comparisons for risky loose equality, suggest switching to === or explicit casts, and explain why a value evaluated as true or false, helping prevent subtle logic errors.

Yes. AI can show how PHP evaluates an expression step by step, clarify which operators bind first, and recommend parentheses to make the intended order explicit and the code easier to read.

Ringkaslah postingan ini dengan: