Apa yang harus
Anda sudah tahu ?
HTML
CSS
Apa itu PHP ?
PHP
Open source scripting language Script PHP dieksekusi pada server
PHP gratis untuk diunduh dan digunakan
PHP sederhana untuk pemula namun
File PHP
Dapat berisi teks, HTML, CSS, Javascript, dan kode PHP
Dijalankan di server dan hasilnya dikembalikan ke browser sebagai HTML biasa
Apa yang bisa
dilakukan PHP ?
Dengan PHP keluaran tidak dibatasi hanya HTML. Namun
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
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
Apa yang
dibutuhkan untuk
mulai menggunakan
PHP ?
Web Server
(Apache, IIS, dll)
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
Contoh Syntax PHP
<!DOCTYPE html> <html>
<body>
<h1>My first PHP page</h1> <?PHP
echo "Hello World!"; ?>
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
Hanya dapat menampilkan satu string
Selalu mengembalikan nilai 1
Merupakan language construct, dan
dapat digunakan dengan ataupun tanpa parantheses (print atau print())
Komentar di PHP
<!DOCTYPE html> <html>
<body> <?PHP
//This is a PHP comment line /*
This is
a PHP comment block
String
Sebuah string adalah rangkaian karakter
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
Float
Angka floating point adalah nomor dengan titik desimal atau angka dalam bentuk
Boolean
Dapat berupa TRUE atau FALSE
Object
Sebuah object adalah tipe data yang menyimpan data dan informasi tentang
bagaimana memproses data
NULL
Nilai NULL khusus menyatakan bahwa suatu variabel tidak memiliki nilai
Berguna untuk membedakan antara string kosong dan nilai NULL dari database
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
Contoh Variabel di PHP
<?PHP $x = 5; $y = 6; $z = $x + $y; echo $z; ?>Variabel String
<?PHP
$txt = "Hello world!"; echo $txt;
strlen()
<?PHP
echo strlen("Hello world!"); ?>
Digunakan untuk mengetahui panjang
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
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)
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
Contoh Konstanta
<?php
define("GREETING", "Welcome to W3Schools.com!", true); echo greeting;
PHP merupakan
Loosely Typed
Language
PHP secara otomatis mengkonversi
variabel dengan tipe data yang
sesuai, tergantung pada nilainya
Scopes Variabel
Local
Global
Static
Local Scope
<?PHP
$x = 5; // global scope function myTest()
{
echo $x; // local scope }
Global Scope
<?PHP $x = 5; // global scope $y = 10; // global scope function myTest() { global $x, $y; $y = $x + $y; }Global Scope
<?PHP $x = 5; $y = 10; function myTest() {$GLOBALS['y'] = $GLOBALS['x'] + $GLOBALS['y']; }
Static Scope
<?PHP function myTest() { static $x = 0; echo $x; $x++; } myTest();Parameter Scope
<?PHP function myTest($x) { echo $x; } myTest(5);Concatenation Operator
<?PHP
$txt1 = "Hello world!";
$txt2 = "What a nice day!"; echo $txt1 . " " . $txt2;
?>
Digunakan untuk menggabungkan dua
Operator
Assignment Operator digunakan untuk memberikan nilai pada variabel
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
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
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
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
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
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
Conditional Statements
if statement
if … else statement
if … else if … else statement
if statement
if (condition) {
code to be executed if condition is true; }
Contoh if statement
<?PHP
$t = date("H"); if ($t <"20") {
echo "Have a good day!"; }
if … else statement
if (condition) {
code to be executed if condition is true; }
else {
code to be executed if condition is false;
Contoh if … else statement
<?PHP
$t = date("H"); if ($t < "20") {
echo "Have a good day!"; }
else {
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 {
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!"; }
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:
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":
Loops
while
do … while
for
while Loop
while (condition) {
code to be executed;
Contoh while Loop
<html> <body> <?PHP $i = 1; while($i <= 5) {echo "The number is " . $i . "<br>"; $i++;
} ?>
do … while Loop
do { code to be executed; } while (condition);Contoh do … while Loop
<html> <body> <?PHP $i = 1; do { $i++;echo "The number is " . $i . "<br>"; }
while ($i <= 5); ?>
for Loop
for (init; condition; increment) {
code to be executed; }
Contoh for Loop
<html> <body> <?PHP
for ($i = 1; $i <= 5; $i++) {
echo "The number is " . $i . "<br>"; }
foreach Loop
foreach ($array as $value) {
code to be executed; }
Contoh foreach Loop
<html> <body> <?PHP
$x = array("one", "two", "three"); foreach ($x as $value)
{
echo $value . "<br>"; }