Pemrograman Internet Mobile 7

51
Pemrograman Internet Mobile 7 Antonius Rachmat C, S.Kom, M.Cs PHP: Hypertext Preprocessors

description

Antonius Rachmat C, S.Kom, M.Cs. Pemrograman Internet Mobile 7. PHP: Hypertext Preprocessors. PHP. PHP (PHP: Hypertext Preprocessor) dikembangkan oleh Rasmus Lerdorf (1994), sebagai awal pengembangan untuk HTTP usage logging dan server-side form generation pada Unix. - PowerPoint PPT Presentation

Transcript of Pemrograman Internet Mobile 7

Page 1: Pemrograman Internet Mobile 7

Pemrograman Internet Mobile 7

Antonius Rachmat C, S.Kom, M.Cs

PHP: Hypertext Preprocessors

Page 2: Pemrograman Internet Mobile 7

PHP

• PHP (PHP: Hypertext Preprocessor) dikembangkan oleh Rasmus Lerdorf (1994), sebagai awal pengembangan untuk HTTP usage logging dan server-side form generation pada Unix.

• PHP 2 (1995) diubah menjadi bahasa Server-side embedded scripting. – Ditambahkan kemampuan database support,

file uploads, variabel, array, rekursif, kondisi, iteration, regular expressions, dll.

Page 3: Pemrograman Internet Mobile 7

Sejarah PHP

• PHP 3 (1998) ditambah dukungan untuk ODBC, multiple platform support, protokol email (SNMP,IMAP), dan parser baru oleh Zeev Suraski and Andi Gutmans .

• PHP 4 (2000) menjadi komponen independen dari web server agar lebih efisien. Parser diganti nama menjadi Zend Engine. – Latest stable: 4.4.9 (www.php.net)

• PHP 5 (2004) ditambahkan Zend Engine II dengan OOP, dukungan XML dengan pustaka libxml2, SOAP untuk Web Services, SQLite telah ditambahkan dalam PHP– Versi terbaru 5.2.9 (www.php.net)

Page 4: Pemrograman Internet Mobile 7

Sejarah PHP

• Pada August 2004, PHP digunakan pada 16,946,328 domains, 1,348,793 alamat IP (http://www.php.net/usage.php) Hampir 32% dari seluruh domain web.

Page 5: Pemrograman Internet Mobile 7

Mengapa PHP digunakan?

• Bahasa mirip C dan Java• Mudah Pemakaiannya

– Kode “ditanamkan” pada HTML/WML/XHTML.– Kode php dimulai dan diakhiri dengan tanda (tag)

<html>   <head>       <title>Example</title>   </head>   <body>       <?php        echo "Hi, I'm a PHP script!";        ?>   </body></html>

Page 6: Pemrograman Internet Mobile 7

Why PHP?

• Cross Platform– Dapat jalan hampir di semua web server pada beberapa sistem

operasi– Web server: Apache, Microsoft IIS, Caudium, Netscape

Enterprise Server– Sistem operasi: NIX (HP-UX,OpenBSD,Solaris,Linux), Mac

OSX, Windows NT/98/2000/XP/2003– Database: Adabas D, dBase,Empress, FilePro (read-only),

Hyperwave,IBM DB2, Informix, Ingres, InterBase, FrontBase, mSQL, Direct MS-SQL, MySQL, ODBC, Oracle (OCI7 and OCI8), Ovrimos, PostgreSQL, SQLite, Solid, Sybase, Velocis,Unix dbm

• Keuntungan dalam biaya– PHP is free.

Page 7: Pemrograman Internet Mobile 7

Dukungan PHP

• GD (GIF, JPEG, PNG)

• SNMP• IMAP (POP, NNTP)• FTP• XML parser• PDF generation• DCOM (Win32 only) • SWF (Flash)

•zlib (compressed IO)

•Charset/text conversion (UTF-8, Cyrillic, Hebrew)

•SOAP

•Cybercash

•ASPELL/PSPELL

Page 8: Pemrograman Internet Mobile 7

PHP Model

• Source:

<html>

<?php

echo(“Hello World!”);

?>

</html>

• Menjadi:

<html>Hello World!

</html>

Page 9: Pemrograman Internet Mobile 7

Notasi PHP

Page 10: Pemrograman Internet Mobile 7

Tipe data

• Scalar types– Boolean– Integer– Float– String

• Compound types– Array– Object

• Diawali dgn tanda $

Page 11: Pemrograman Internet Mobile 7

Konstanta

<?define(“USERNAME”,”Anton”);echo “User Name :”.USERNAME;?>

Page 12: Pemrograman Internet Mobile 7

Boolean

• Untuk literal boolean: true atau false• Berikut dianggap bernilai false, jika

– Nilai boolean FALSE– Nilai integer 0 atau float 0.0– String yang kosong, atau string “0”– Array dengan elemen kosong– NULL (termasuk unset variabel)

Page 13: Pemrograman Internet Mobile 7

Contoh Integer

• Nilai literal integer yang valid: – $a = 1234;– $a = -123;– $a = 0123; #octal number– $a = 0x123; #hexa number

• Jika nilai literal integer melebihi range Integer, otomatis PHP akan mengkonversi ke tipe float

• Konversi ke Integer:– Nilai boolean FALSE => 0, TRUE => 1– Casting (int). Contoh:

• $a = (int) (25/7); # int(3)• $a = round(25/7); # float(4)

Page 14: Pemrograman Internet Mobile 7

Float

• Ukuran float tergantung pada platform, walaupun maksimum ~1.8e208 (64 bit format IEEE)– $a = 1.234;– $b = 1.2e4;– $c = 7E-10;

Page 15: Pemrograman Internet Mobile 7

String

• Literal string dideklarasikan baik dengan – Petik ganda (“ “). contoh: $a = “Nama: $nama\n”;– Petik tunggal (' '). contoh: $b = 'c:\*.*';– Heredoc. Contoh:

$c = <<<EOD

Ini testingEOD;

• Pengaksesan karakter string– $a = “ini test”;– $pertama = $a{0};– $ketiga = $a{2};– $akhir = $a{strlen($a)-1};

Page 16: Pemrograman Internet Mobile 7

Konversi String ke Angka

• Contoh:– $a = 1 + “10.5”;– $a = 1 + “-1.3e3”;– $a = 1 + “bob-1.3e3”;– $a = 1 + “bob3”;– $a = 1 + “10 ayam kate”;– $a = 1 + “10.2 ayam kate”;– $a = “10.0 ayam” + 1;

Page 17: Pemrograman Internet Mobile 7

Tipe Data

<?$testing = 5; //integerecho gettype($testing);$testing = "five"; //stringecho gettype($testing);$testing = 5.0; //doubleecho gettype($testing);$testing = true; //booleanecho gettype($testing);?>

<?$varumum = 3.14;$vardouble = (double)$varumum;echo gettype($vardouble); //double$varstring = (string)$varumum;echo gettype($varstring); //string?>

Page 18: Pemrograman Internet Mobile 7

array

Page 19: Pemrograman Internet Mobile 7

Custom Array

• Contoh pembuatan array dengan custom key <?php// This array is the same as ...array(5 => 43, 32, 56, "b" => 12);// ...this arrayarray(5 => 43, 6 => 32, 7 => 56, "b" => 12);?>

• Size of array is not defined• If you add a new element the maximum of the

integer indices is taken, and the new key will be that maximum value + 1

Page 20: Pemrograman Internet Mobile 7

Array Multidimensi

Page 21: Pemrograman Internet Mobile 7

Fungsi-fungsi ttg Array

• Implode() -> menggabungkan array menjadi string

• explode() -> memecah string menjadi array• sort() -> mengurutkan array scr asc• rsort() -> mengurutkan array scr desc• ksort() -> mengurutkan index string array scr asc• array_pop() -> menghapus array dr urutan

terakhir• array_push() -> menambah array

Page 22: Pemrograman Internet Mobile 7

Contoh penggunaan fungsi

<? //explodeimplode.php$city[0]="Jogja";$city[1]="Bandung";$city[3]="Surabaya";$strgabung = implode("-",$city);echo "Stlh digabung: ".$strgabung."<br>";$cityarray = explode("-",$strgabung);foreach ($cityarray as $key=>$value){

echo "Array ke-".$key." = ".$value."<br>";}?>

Page 23: Pemrograman Internet Mobile 7

NULL

• NULL menyatakan variabel yang tidak ada nilainya

• Sebuah variabel NULL, jika– Dinyatakan sebagai NULL dengan opertor =– Belum pernah diberikan suatu nilai literal– Telah di unset()

• Untuk mengecek apakah variabel NULL atau tidak, dapat digunakan fungsi is_null()

Page 24: Pemrograman Internet Mobile 7

Konvensi PHP

• Aturan nama variabel PHP mengikuti konvensi seperti bahasa pemrograman lainnya

• Untuk membuat referensi ke variabel lain$a = “test”;$b = &$a; # referensi$b = “ayam”;echo ($a . ' == ' . $b);

• Variabel dari variabel$a = “hello”;$$a = “world”;echo “$a ${$a}”;echo “$a $hello”;

Page 25: Pemrograman Internet Mobile 7

Unset dan Bracket

Page 26: Pemrograman Internet Mobile 7

Ruang Lingkup• Contoh berikut, variabel $a dapat diakses dari dalam file include

atau require<?php

$a = 1;include "b.inc";

?>• Variabel bersifat lokal dalam suatu fungsi

<?php$a = 1; /* global scope */ function Test() {

echo $a; /* menunjuk ke lokal variabel */ } Test();

?>

Page 27: Pemrograman Internet Mobile 7

Operator

• Aritmatika

• Assignment

Page 28: Pemrograman Internet Mobile 7

Operator Perbandingan

Page 29: Pemrograman Internet Mobile 7

Ternary operator

<?php// Contoh Ternary Operator$action = (empty($_POST['action'])) ? 'default' : $_POST['action'];// sama dengan if/else berikut:if (empty($_POST['action'])) { $action = 'default';} else { $action = $_POST['action'];}?>

Page 30: Pemrograman Internet Mobile 7

Perbandingan

<?php

if ($a > $b) {

echo "a is bigger than b";

} elseif ($a == $b) {

echo "a is equal to b";

} else {

echo "a is smaller than b";

}

?>

Page 31: Pemrograman Internet Mobile 7

<?$mood = "sad";switch($mood){

case "happy":echo "Mood anda bagus !";break;

case "sad":echo "Mood anda sedang jelek...";break;

default:echo "Mood anda $mood";

} ?>

Page 32: Pemrograman Internet Mobile 7

Struktur While

<?php$i = 1;while ($i <= 10) { echo $i++; }$i = 1;while ($i <= 10): echo $i; $i++;endwhile;

?>

<?php$i = 10;do{

echo $i;}while($i>=1);

?>

Page 33: Pemrograman Internet Mobile 7

Struktur for

Page 34: Pemrograman Internet Mobile 7

Foreach (1)

<?php$arr = array("one", "two", "three");reset($arr);while (list($key, $value) = each ($arr)) { echo "Key: $key; Value: $value<br />\n";}foreach ($arr as $key => $value) { echo "Key: $key; Value: $value<br />\n";}

?>

Page 35: Pemrograman Internet Mobile 7

Foreach (2)

<?php$arr = array("one", "two", "three");reset ($arr);while (list(, $value) = each ($arr)) { echo "Value: $value<br />\n";}foreach ($arr as $value) { echo "Value: $value<br />\n";}

?>

Page 36: Pemrograman Internet Mobile 7

Switch

Page 37: Pemrograman Internet Mobile 7

Switch (2)

Page 38: Pemrograman Internet Mobile 7

Break

<?$counter=1;while($counter<10){

if($counter==5){echo "berhenti jika counter bernilai 5 <br>";break;

}echo $counter."<br>";$counter++;

}?>

Page 39: Pemrograman Internet Mobile 7

Continue

<?for($counter=1;$counter<=10;$counter++){

if($counter==5) continue;echo "counter bernilai: $counter<br>";

}?>

Page 40: Pemrograman Internet Mobile 7

Session

• session_start() //dipaling atas• session_register(<nama,nama,nama>)• session_unregister(<nama,nama,nama>• if (session_is_registered(<nama>))• unset(<nama>)• session_destroy()

Page 41: Pemrograman Internet Mobile 7

File upload

• If(copy(source, destination))

Page 42: Pemrograman Internet Mobile 7

Function (by value)

• New functionfunction my_function(){ echo 'My function was called';}

function fungsi_return($a,$b){return a + b;

}• Calling function

my_function();$c = fungsi_return(1,2);

Page 43: Pemrograman Internet Mobile 7

Function By Reference

<? //functionbyreference.php

function tax(&$salary){ //definisi

$salary = $salary-(($salary/100)*20);

return $salary;

}

$salary = 2000;

echo tax($salary); //hasil 1600

echo $salary; //hasil 1600

?>

Page 44: Pemrograman Internet Mobile 7

Parameter default function

<?function tax($salary=2000){ //definisi

$salary = $salary-(($salary/100)*20);return $salary;

}echo tax(); ?>

Page 45: Pemrograman Internet Mobile 7

Nested function

Page 46: Pemrograman Internet Mobile 7

Koneksi Database MySQL

• mysql_connect(<host>,<username>,<password>

• mysql_select_db(<nama_db>)• mysql_query(<query>)• mysql_fetch_array(<hasil_query>)• mysql_close()

Page 47: Pemrograman Internet Mobile 7

SERVER variable

• $_SERVER is an array containing information such as– Headers– Paths– Script locations

• The entries in this array are created by the webserver. There is no guarantee that every webserver will provide any of these; servers may omit some, or provide others

Page 48: Pemrograman Internet Mobile 7

contoh

Page 49: Pemrograman Internet Mobile 7

Server Variable

• 'argv' – Array of arguments passed to the script.

When the script is run on the command line, this gives C-style access to the command line parameters. When called via the GET method, this will contain the query string.

• 'argc' – Contains the number of command line

parameters passed to the script (if run on the command line).

Page 50: Pemrograman Internet Mobile 7

SERVER variable

• 'REMOTE_ADDR' – The IP address from which the user is viewing the current page.

• 'REMOTE_HOST' – The Host name from which the user is viewing the current page.

The reverse dns lookup is based off the REMOTE_ADDR of the user.

• 'REMOTE_PORT' – The port being used on the user's machine to communicate with

the web server. • $_COOKIE

– An associative array of variables passed to the current script via HTTP cookies. Automatically global in any scope.

• $_POST– An associative array of variables passed to the current script via

the HTTP POST method.

Page 51: Pemrograman Internet Mobile 7

NEXT