Pemrograman Berbasis Web - moeslimar.files.wordpress.com · Pemrograman Berbasis Web Pertemuan 7...
-
Upload
duongkhuong -
Category
Documents
-
view
291 -
download
8
Embed Size (px)
Transcript of Pemrograman Berbasis Web - moeslimar.files.wordpress.com · Pemrograman Berbasis Web Pertemuan 7...

Pemrograman Berbasis Web
Pertemuan 7 – Dasar Pemrograman PHP I
Program Diploma IPB - Aditya Wicaksono, SKomp 1

Apa yang harus
Anda sudah tahu ?
HTML
CSS
Javascript
Program Diploma IPB - Aditya Wicaksono, SKomp 2

Apa itu PHP ?
PHP Pemberi Harapan Palsu
Program Diploma IPB - Aditya Wicaksono, SKomp 3

Apa itu PHP ?
PHP PHP Hypertext Preprocessor
Open source scripting language
Script PHP dieksekusi pada server
PHP gratis untuk diunduh dan digunakan
PHP sederhana untuk pemula namun
menawarkan banyak fitur canggih untuk
programmer profesional
Program Diploma IPB - Aditya Wicaksono, SKomp 4

File PHP
Dapat berisi teks, HTML, CSS, Javascript, dan kode PHP
Dijalankan di server dan hasilnya dikembalikan ke browser sebagai HTML biasa
Ekstensi file default *.php
Program Diploma IPB - Aditya Wicaksono, SKomp 5

Apa yang bisa
dilakukan PHP ?
Dengan PHP keluaran tidak dibatasi hanya HTML. Namun
dapat ditampilkan dalam gambar, file PDF, dan bahkan
Flash Movies. Dapat pula keluaran berupa teks, seperti XHTML
dan XML.
PHP dapat menghasilkan konten halaman dinamis
PHP dapat membuat, membuka, membaca, menulis, dan menutup file di server
PHP dapat mengumpulkan data form
PHP dapat mengirim dan menerima cookies
PHP dapat menambah, menghapus, memodifikasi data dalam database
PHP dapat membatasi pengguna untuk mengakses beberapa halaman di situs web
PHP dapat mengenkripsi data
Program Diploma IPB - Aditya Wicaksono, SKomp 6

Mengapa PHP ?
PHP dapat berjalan di berbagai platform yang berbeda (Windows, Linux, Unix, Mac OS X, dll)
PHP kompatibel dengan hampir semua server yang digunakan saat ini (Apache, IIS, dll)
PHP memiliki dukungan untuk berbagai database (MySQL, PosgreSQL, dll)
PHP itu gratis
Unduh dari official PHP resource : www. php.net
PHP mudah dipelajari dan berjalan efisien pada sisi server
Program Diploma IPB - Aditya Wicaksono, SKomp 7

Apa yang
dibutuhkan untuk
mulai menggunakan
PHP ?
Web Server (Apache, IIS, dll)
Program Diploma IPB - Aditya Wicaksono, SKomp 8

Syntax PHP
Script PHP dapat ditempatkan di manapun dalam dokumen
Script PHP dimulai dengan <?PHP dan diakhiri denga ?>
Setiap baris kode PHP harus diakhiri dengan titik koma (;)
Titik koma adalah pemisah dan digunakan untuk membedakan satu set instruksi dari yang
lain
Program Diploma IPB - Aditya Wicaksono, SKomp 9

Contoh Syntax PHP
<!DOCTYPE html>
<html>
<body>
<h1>My first PHP page</h1>
<?PHP
echo "Hello World!";
?>
</body>
</html>
Program Diploma IPB - Aditya Wicaksono, SKomp 10

echo dan print statement
echo
Dapat mengeluarkan satu atau lebih string
Tidak mengembalikan nilai apapun
Merupakan language construct, dan dapat digunakan dengan ataupun tanpa parantheses (echo atau echo())
String dapat berisi markup HTML
Sedikit lebih cepat dibandingkan dengan print
Hanya dapat menampilkan satu string
Selalu mengembalikan nilai 1
Merupakan language construct, dan
dapat digunakan dengan ataupun
tanpa parantheses (print atau print())
String dapat berisi markup HTML
Program Diploma IPB - Aditya Wicaksono, SKomp 11

Komentar di PHP
<!DOCTYPE html> <html> <body> <?PHP //This is a PHP comment line /* This is a PHP comment block */ ?> </body> </html>
Program Diploma IPB - Aditya Wicaksono, SKomp 12

String
Sebuah string adalah rangkaian karakter
Sebuah string dapat berupa teks dalam tanda kutip (kutip tunggal taupun ganda)
Program Diploma IPB - Aditya Wicaksono, SKomp 13

Integer
Sebuah integer adalah angka tanpa desimal
Aturan untuk integer :
Memiliki minimal satu digit (0-9)
Tidak dapat berisi koma atau kosong
Tidak harus memiliki titik desimal
Dapat berupa positif atau negatif
Integer dapat ditentukan dalam tiga format :
Desimal (basis 10)
Heksadesimal (basis 16, diawali dengan 0x)
Oktal (basis 8, diawali dengan 0)
Program Diploma IPB - Aditya Wicaksono, SKomp 14

Float
Angka floating point adalah nomor dengan titik desimal atau angka dalam bentuk
eksponensial
Program Diploma IPB - Aditya Wicaksono, SKomp 15

Boolean
Dapat berupa TRUE atau FALSE
Boolean sering digunakan dalam pengujian bersyarat
Program Diploma IPB - Aditya Wicaksono, SKomp 16

Object
Sebuah object adalah tipe data yang menyimpan data dan informasi tentang
bagaimana memproses data
Sebuah object harus dinyatakan secara eksplisit
Program Diploma IPB - Aditya Wicaksono, SKomp 17

NULL
Nilai NULL khusus menyatakan bahwa suatu variabel tidak memiliki nilai
Berguna untuk membedakan antara string kosong dan nilai NULL dari database
Variabel dapat dikosongkan dengan menetapkan nilai ke NULL
Program Diploma IPB - Aditya Wicaksono, SKomp 18

Variabel di PHP
Variabel dimulai dengan tanda $ diikuti dengan nama variabel
Nama variabel harus dimulai dengan huruf atau karakter garis bawah
Nama variabel hanya dapat berisi karakter alfanumerik dan garis bawah (A-z, 0-9, _)
Nama variabel tidak boleh mengandung spasi
Nama variable bersifat case sensitive
Program Diploma IPB - Aditya Wicaksono, SKomp 19

Contoh Variabel di PHP
<?PHP
$x = 5;
$y = 6;
$z = $x + $y;
echo $z;
?>
Program Diploma IPB - Aditya Wicaksono, SKomp 20

Variabel String
<?PHP
$txt = "Hello world!";
echo $txt;
?>
Program Diploma IPB - Aditya Wicaksono, SKomp 21

strlen()
<?PHP
echo strlen("Hello world!");
?>
Digunakan untuk mengetahui panjang
nilai string
Program Diploma IPB - Aditya Wicaksono, SKomp 22

strpos()
<?PHP
echo strpos("Hello world!","world");
?>
Digunakan untuk mencari karakter atau
teks tertentu dalam string
Jika kecocokan ditemukan, fungsi akan
mengembalikan posisi karakter pertama
yang cocok.
Jika tidak ditemukan yang cocok, maka
fungsi akan mengembalikan FALSE
Program Diploma IPB - Aditya Wicaksono, SKomp 23

Konstanta PHP
Sebuah konstanta adalah identifier (nama) untuk nilai sederhana
Nilai tidak dapat diubah selama script
Sebuah nama konstanta yang valid dimulai dengan huruf atau garis bawah (tidak ada
tanda $ sebelum nama konstanta)
Tidak seperti variabel, konstanta secara otomatis global di seluruh script
Program Diploma IPB - Aditya Wicaksono, SKomp 24

Mengatur Konstanta PHP
Untuk mengatur sebuah konstanta, gunakan fungsi define()
Dibutuhkan 3 parameter :
Parameter pertama mendefinisikan nama dari konstanta
Parameter kedua mendefinisikan nilai konstanta
Parameter ketiga optional, menentukan apakah nama konstanta harus case-insensitive
(defaultnya FALSE)
Program Diploma IPB - Aditya Wicaksono, SKomp 25

Contoh Konstanta
<?php
define("GREETING", "Welcome to W3Schools.com!", true);
echo greeting;
?>
Program Diploma IPB - Aditya Wicaksono, SKomp 26

PHP merupakan
Loosely Typed
Language
PHP secara otomatis mengkonversi
variabel dengan tipe data yang
sesuai, tergantung pada nilainya
Program Diploma IPB - Aditya Wicaksono, SKomp 27

Scopes Variabel
Local
Global
Static
Parameter
Program Diploma IPB - Aditya Wicaksono, SKomp 28

Local Scope
<?PHP
$x = 5; // global scope
function myTest()
{
echo $x; // local scope
}
myTest();
?>
Program Diploma IPB - Aditya Wicaksono, SKomp 29

Global Scope
<?PHP $x = 5; // global scope $y = 10; // global scope function myTest() { global $x, $y; $y = $x + $y; } myTest(); echo $y; // outputs 15 ?>
Program Diploma IPB - Aditya Wicaksono, SKomp 30

Global Scope
<?PHP
$x = 5;
$y = 10;
function myTest()
{
$GLOBALS['y'] = $GLOBALS['x'] + $GLOBALS['y'];
}
myTest();
echo $y;
?>
Program Diploma IPB - Aditya Wicaksono, SKomp 31

Static Scope
<?PHP function myTest() { static $x = 0; echo $x; $x++; } myTest(); myTest(); myTest(); ?>
Program Diploma IPB - Aditya Wicaksono, SKomp 32

Parameter Scope
<?PHP
function myTest($x)
{
echo $x;
}
myTest(5);
?>
Program Diploma IPB - Aditya Wicaksono, SKomp 33

Concatenation Operator
<?PHP
$txt1 = "Hello world!";
$txt2 = "What a nice day!";
echo $txt1 . " " . $txt2;
?>
Digunakan untuk menggabungkan dua
nilai string menjadi satu
Program Diploma IPB - Aditya Wicaksono, SKomp 34

Operator
Assignment Operator digunakan untuk memberikan nilai pada variabel
Arithmetic Operator digunakan untuk menambah nilai
Program Diploma IPB - Aditya Wicaksono, SKomp 35

Arithmetic Operator
Operator Nama Deskripsi Contoh Hasil
x + y Addition Penjumlahan x dan y 2 + 2 4
x - y Subtraction Pengurangan x dan y 5 - 2 3
x * y Multiplication Perkalian x dan y 5 * 2 10
x / y Division Pembagian x dan y 15 / 5 3
x % y Modulus Sisa x dibagi dengan y
5 % 2
10 % 8
10 % 2
1
2
0
- x Negation Kebalikkan x - 2
a . b Concatenation Menggabungkan dua string "Hi" . "Ha" HiHa
Program Diploma IPB - Aditya Wicaksono, SKomp 36

Assignment Operator
Assignment Same as... Deskripsi
x = y x = y Operan kiri akan diberikan nilai dari ekspresi di sebelah kanan
x += y x = x + y Addition
x -= y x = x - y Subtraction
x *= y x = x * y Multiplication
x /= y x = x / y Division
x %= y x = x % y Modulus
a .= b a = a . b Concatenate dua string
Program Diploma IPB - Aditya Wicaksono, SKomp 37

Increment & Decrement Operator
Operator Nama Deskripsi
++ x Pre-increment Tambah x dengan satu, kemudian kembalikan x
x ++ Post-increment Kembalikan x, kemudian tambah x dengan satu
-- x Pre-decrement Kurangi x dengan satu, kemudian kembalikan x
x -- Post-decrement Kembalikan x, kemudian kurangi x dengan satu
Program Diploma IPB - Aditya Wicaksono, SKomp 38

Comparison Operator
Operator Nama Deskripsi Contoh
x == y Equal True if x is equal to y 5==8 returns false
x === y Identical True if x is equal to y, and they are of same
type 5==="5" returns false
x != y Not equal True if x is not equal to y 5!=8 returns true
x <> y Not equal True if x is not equal to y 5<>8 returns true
x !== y Not identical True if x is not equal to y, or they are not of
same type 5!=="5" returns true
x > y Greater than True if x is greater than y 5>8 returns false
x < y Less than True if x is less than y 5<8 returns true
x >= y Greater than or equal to True if x is greater than or equal to y 5>=8 returns false
x <= y Less than or equal to True if x is less than or equal to y 5<=8 returns true
Program Diploma IPB - Aditya Wicaksono, SKomp 39

Logical Operator
Operator Nama Deskripsi Contoh
x and y And True if both x and y are true x=6
y=3
(x < 10 and y > 1) returns true
x or y Or True if either or both x and y are true x=6
y=3
(x==6 or y==5) returns true
x xor y Xor True if either x or y is true, but not both x=6
y=3
(x==6 xor y==3) returns false
x && y And True if both x and y are true x=6
y=3
(x < 10 && y > 1) returns true
x || y Or True if either or both x and y are true x=6
y=3
(x==5 || y==5) returns false
! x Not True if x is not true x=6
y=3
!(x==y) returns true
Program Diploma IPB - Aditya Wicaksono, SKomp 40

Array Operator
Operator Nama Deskripsi
x + y Union Union of x and y
x == y Equality True if x and y have the same key/value pairs
x === y Identity True if x and y have the same key/value pairs in the same
order and are of the same type
x != y Inequality True if x is not equal to y
x <> y Inequality True if x is not equal to y
x !== y Non-identity True if x is not identical to y
Program Diploma IPB - Aditya Wicaksono, SKomp 41

Conditional Statements
if statement
if … else statement
if … else if … else statement
Switch statement
Program Diploma IPB - Aditya Wicaksono, SKomp 42

if statement
if (condition)
{
code to be executed if condition is true;
}
Program Diploma IPB - Aditya Wicaksono, SKomp 43

Contoh if statement
<?PHP
$t = date("H");
if ($t <"20")
{
echo "Have a good day!";
}
?>
Program Diploma IPB - Aditya Wicaksono, SKomp 44

if … else statement
if (condition)
{
code to be executed if condition is true;
}
else
{
code to be executed if condition is false;
}
Program Diploma IPB - Aditya Wicaksono, SKomp 45

Contoh if … else statement
<?PHP
$t = date("H");
if ($t < "20")
{
echo "Have a good day!";
}
else
{
echo "Have a good night!";
}
?>
Program Diploma IPB - Aditya Wicaksono, SKomp 46

if … else if … else statement
if (condition)
{
code to be executed if condition is true;
}
else if (condition)
{
code to be executed if condition is true;
}
else
{
code to be executed if condition is false;
}
Program Diploma IPB - Aditya Wicaksono, SKomp 47

Contoh if … else if … else statement
<?PHP $t = date("H"); if ($t < "10") { echo "Have a good morning!"; } else if ($t < "20") { echo "Have a good day!"; } else { echo "Have a good night!"; } ?>
Program Diploma IPB - Aditya Wicaksono, SKomp 48

switch statement
switch (n)
{
case label1:
code to be executed if n=label1;
break;
case label2:
code to be executed if n=label2;
break;
default:
code to be executed if n is different from both label1 and label2;
}
Program Diploma IPB - Aditya Wicaksono, SKomp 49

Contoh switch statement
<?PHP $favcolor = "red"; switch ($favcolor) { case "red": echo "Your favorite color is red!"; break; case "blue": echo "Your favorite color is blue!"; break; case "green": echo "Your favorite color is green!"; break; default: echo "Your favorite color is neither red, blue, or green!"; } ?>
Program Diploma IPB - Aditya Wicaksono, SKomp 50

Loops
while
do … while
for
foreach
Program Diploma IPB - Aditya Wicaksono, SKomp 51

while Loop
while (condition)
{
code to be executed;
}
Program Diploma IPB - Aditya Wicaksono, SKomp 52

Contoh while Loop
<html> <body> <?PHP $i = 1; while($i <= 5) { echo "The number is " . $i . "<br>"; $i++; } ?> </body> </html>
Program Diploma IPB - Aditya Wicaksono, SKomp 53

do … while Loop
do
{
code to be executed;
}
while (condition);
Program Diploma IPB - Aditya Wicaksono, SKomp 54

Contoh do … while Loop
<html> <body> <?PHP $i = 1; do { $i++; echo "The number is " . $i . "<br>"; } while ($i <= 5); ?> </body> </html>
Program Diploma IPB - Aditya Wicaksono, SKomp 55

for Loop
for (init; condition; increment)
{
code to be executed;
}
Program Diploma IPB - Aditya Wicaksono, SKomp 56

Contoh for Loop
<html>
<body>
<?PHP
for ($i = 1; $i <= 5; $i++)
{
echo "The number is " . $i . "<br>";
}
?>
</body>
</html>
Program Diploma IPB - Aditya Wicaksono, SKomp 57

foreach Loop
foreach ($array as $value)
{
code to be executed;
}
Program Diploma IPB - Aditya Wicaksono, SKomp 58

Contoh foreach Loop
<html> <body> <?PHP $x = array("one", "two", "three"); foreach ($x as $value) { echo $value . "<br>"; } ?> </body> </html>
Program Diploma IPB - Aditya Wicaksono, SKomp 59

Terima Kasih
Program Diploma IPB - Aditya Wicaksono, SKomp 60