Pemrograman Web Lanjut 2017 Chapter 2 2.1. Variable (Variabel) In programming, a variable is a container (storage area) to hold data. Variable is something that may change the value, depending on condition or an information passed to the program. Variable has an identifier and value. The value of variable is temporary and can be lost when the program has been executed. The permanent data storage can use the database. Variable can be defined by identifier of PHP without describing the data type. Moreover, data type of PHP can save value of any data type. There are some rules of writing data types in PHP. 1. Variables must begin with a dollar sign ($). 2. Variable can not contain spaces. 3. Variable can not begin with a number. 4. Except using $, variables can not use the symbol character (except underscore). 5. Variables in PHP are case sensitive. Example: TRUE $mahasiswa $_nim $kelas01 $mata_kuliah $JenisKelamin
FALSE $maha siswa $-nim $01kelas $mata.kuliah $Jenis Kelamin
EXPLANATION There is a space Begin number character Begin with number Separated by dot There is a space
Data will be saved when there is valuing on the variables. This is the basic syntax of variable. $nama_variabel = nilai_variabel;
exercise1.php
Variable Kelas: $kelas"; ?>
Ld.Farida | Universitas AMIKOM Yogyakarta
Pemrograman Web Lanjut 2017 2.2. Constant (Konstanta) Constant is an identifier of program that can store data like a variable. However, the stored data is fixed and can not be change. In other world, constant is variable that have a fixed data. If the value does not change, the variable should be better as a constant. Constant can be defined with the define()function. PHP has defined some constants, such as: PHP_VERSIONis a constant that give the information about PHP version that is used. However, besides constant that have been available, the constant in PHP can make your own. The basic syntax to write the constants is: define(“constant_name”, “constant_value”);
Exercise2.php
Constant Luas Lingkaran = ".$luaslingkaran; ?>
2.3. Data Type Data type is not defined in PHP, but can be determined automatically by PHP interpreter. PHP has 5 data type, such as: 1. Integer (numeric) 2. Floating Point (numeric with fractional) 3. String (character) 4. Array (describe a collection of elements) 5. Object
Ld.Farida | Universitas AMIKOM Yogyakarta
Pemrograman Web Lanjut 2017 Example: Data Type Integer Floating-point String Boolean
Explanation $tahun = 2009; $umur = 20; $ipk = 3.75; $rerata = 7.5; $nama = “Farida”; $jurusan = “Sistem Informasi” Menghasilkan nilai true dan false
exercise3.php "; echo "NIM : " .$nim. "
"; echo "Nomor Presensi : " .$no_presensi; echo "
"; echo "IPK : ".$ipk."
"; printf ("IPK : %.2f
", $ipk); if ($status) echo "Status : Aktif"; else echo "Status : Tidak Aktif"; ?>
PHP supports object-oriented Programming (OOP), which can facilitate debugging and maintenance process and reusable code. The class become the most important unit. Class is the definition of structure that has properties and method. In PHP, class is defined by class. Example: class Person{ var $nama = “”; function nama($newname = NULL){ if(!is_null($newname)){ $this -> nama = $newname; } return $this->nama; } }
Ld.Farida | Universitas AMIKOM Yogyakarta
Pemrograman Web Lanjut 2017 2.4. Operator dan Arithmetic Operator is a symbol used to data manipulation, such as addition, subtraction, multiplication, and division.
1. 2. 3. 4.
PHP divides the operators in the following groups: Arithmetic Operators are used whit the numeric values to perform common arithmetical operations. Relational Operators are used to compare two values Logical Operators are used to combine conditional statements, have TRUE or FALSE value. etc.
Example: Type of Operator
Arithmetic
Relational
Logical
String
Operator + * / % ++ -== != < > <= >=
Example $a + $b $a - $b $a * $b $a / $b $a % $b $a++ $a-$a == $b $a != $b $a < $b $a > $b $a <= $b $a >= $b
&& || ! .
$a && %b $a || $b !$a $text1.$text2
Explanation addition Subtraction multiplication division Modulus, result of division remainder Increment, by 1 Decrement, by 1 Returns true if $a is equal to $b Returns true if $a is not equal to $b Returns true if $a is less than $b Returns true if $a is greater than $b Returns true if $a is less than or equal to $b Returns true if $a is greater than or equal to $b TRUE if $a and $b TRUE TRUE if $a or $b TRUE TRUE if $a FALSE Concatenation of $text1 and $text2
exercise4.php
"
Perhitungan Aritmetika
"; "$angka1 + $angka2 = $tambah"; "
$angka1 - $angka2 = $kurang"; "
$angka1 * $angka2 = $perkalian"; "
$angka1 / $angka2 = $bagi"; "
$angka1 % $angka2 = $modulus";
?>
Ld.Farida | Universitas AMIKOM Yogyakarta
Pemrograman Web Lanjut 2017 2.5.
Method Get dan Post
Method used to decide how the information sent from the form to the destination (with an action). PHP has two methods, that is POST dan GET. Both of them have a function to collect and send of the value. The difference is, when using GET method information submitted will be displayed in URL. Meanwhile, when using POST method information will not be displayed. There is an example of using the method. form.php
Latihan Membuat Form
action.php "; echo "Email :".$email."
"; ?>
2.6. Assignment Create a form that can be run the arithmetic operator, as a picture below.
Ld.Farida | Universitas AMIKOM Yogyakarta
Pemrograman Web Lanjut 2017 Referensi
Arief, M. R. 2011. Pemrograman Web Dinamis menggunakan PHP dan MySQL. Yogyakarta: Andi Offset. Hakim, Lukmanul. 2014. Rahasia Inti Master PHP &MySQLi(improved). Yogyakarta, Lokomedia Paranginan, Kasiman. 2012. Aplikasi Web dengan PHP dan MySQL. Yogyakarta: Andi Offset. Raharjo, Budi., dkk. 2014. Modul Pemrograman Web HTML, PHP, & MySQL. Bandung: Modula. Arifin, Oki. 2015. Modul Pemrograman Web Lanjut. STMIK AMIKOM Yogyakarta. Yunmar, Rajif Agung. 2011. Modul Pemrograman Web Lanjut. STMIK AMIKOM Yogyakarta.
Ld.Farida | Universitas AMIKOM Yogyakarta