SlideShare a Scribd company logo
Web Programming– PHP PHP Hypertext Preprocessor Pemrograman Web (2010/2011) Teknik Informatika, Universitas Islam Indonesia Follow  Hari Setiaji  on Twitter
Sekilas Pemograman Web (2010/2011) – Hari Setiaji, S.Kom Bahasa pemrograman script di sisi server (server-side scripting) Berupa script yang disisipkan di dalam dokumen HTML,   embedded script  yang diinterpretasi (bukan di-compile) Dibuat pertama kali oleh Rasmus Lerdorf PHP singkatan dari: P ersonal  H ome  P age (awalnya) P rofessional  H ome  P age (kemudian) P HP:  H ypertext  P reprocessor (resminya) Official Website :  http://www.php.net PHP versi terakhir : 5.x Free & opensource Multi platform: Windows, Linux, Mac Menyediakan Library/API yang menyeluruh: Database : MySQL, Oracle, postgreSQL, IBM DB2, ODBC, dll Protocol : HTTP, FTP, POP3, IMAP, LDAP, SNMP, dll Output : HTML, XML, JPEG, GIF, PNG, PDF, SWF dll
Alur Kerja PHP Pemograman Web (2010/2011) – Hari Setiaji, S.Kom
Contoh Penyisipan Script PHP <html> <? /* Cara I: script PHP dideklarasikan di sini*/ echo &quot;halo 1<br>&quot;; ?> <?php /* Cara II: script PHP dideklarasikan di sini*/ echo &quot;halo 2<br>&quot;; ?> <script language=&quot;php&quot;> /* Cara III: script PHP dideklarasikan di sini*/ echo &quot;halo 3&quot;; </script> </html> <html> halo 1<br> halo 2<br> halo 3</html> PHP HTML Browser Pemograman Web (2010/2011) – Hari Setiaji, S.Kom
Variabel & Konstanta  case sensitive variable dengan prefiks:  $ variable tidak perlu dideklarasi  variable scope: local (default) global, dengan menyebutkan:  global $namavar; Static, dengan menyebutkan:  static $namavar; Predefined variables (didefinisikan oleh web server), berupa variabel Array Server variables:  $_SERVER Environment variables:  $_ENV Cookie:  $_COOKIE Request parameter:  $_GET ,  $_POST ,  $_FILES konstanta Pendefinisian:  define(&quot;pi&quot;, 3.14); Konstanta terdefinisi, contoh: PHP_VERSION  (versi PHP), __FILE__  (nama file yang sedang dijalankan), __LINE__  (nomor baris program yang sedang dijalankan), dll Pemograman Web (2010/2011) – Hari Setiaji, S.Kom
Operator Assignment:  = ,   += ,   -= ,   *= ,   /= ,   %=  ( $ a += 1  artinya  $a = $a + 1 ) Aritmatika:  + ,  - ,  * ,  / ,  % Operator pre/post increment/decrement:  ++$a ,  --$a ,  $b++ ,  $b-- Perbandingan:  ==  (equal),  ===  (identical, sama nilai dan sama tipe),  != ,  > ,  < ,  >= ,  <= Operator logika:  &&  (and),  ||  (or),  !  (not) Operator kondisi:  ? $jenis = ($bil % 2 == 0 ? ”Genap” : ”Ganjil”); Operator string:  .  (concat) Operator new (untuk object):  new Pemograman Web (2010/2011) – Hari Setiaji, S.Kom
Examples <?php echo &quot;<h3>Postincrement</h3>&quot;; $a = 5; echo &quot;Should be 5: &quot; . $a++ . &quot;<br />\n&quot;; echo &quot;Should be 6: &quot; . $a . &quot;<br />\n&quot;; echo &quot;<h3>Preincrement</h3>&quot;; $a = 5; echo &quot;Should be 6: &quot; . ++$a . &quot;<br />\n&quot;; echo &quot;Should be 6: &quot; . $a . &quot;<br />\n&quot;; echo &quot;<h3>Postdecrement</h3>&quot;; $a = 5; echo &quot;Should be 5: &quot; . $a-- . &quot;<br />\n&quot;; echo &quot;Should be 4: &quot; . $a . &quot;<br />\n&quot;; echo &quot;<h3>Predecrement</h3>&quot;; $a = 5; echo &quot;Should be 4: &quot; . --$a . &quot;<br />\n&quot;; echo &quot;Should be 4: &quot; . $a . &quot;<br />\n&quot;; ?> PHP Browser Pemograman Web (2010/2011) – Hari Setiaji, S.Kom
Examples <?php $a=5; $b=&quot;5&quot;; $hasil = $a==$b; echo &quot;$hasil <br />&quot;; $a=5; $c=5; $hasil = $a===$c; echo &quot;$hasil <br />&quot;; $jenis = ($a % 2 == 0 ? Genap : Ganjil); echo $a.&quot; adalah bilangan $jenis&quot;; ?> PHP Browser Pemograman Web (2010/2011) – Hari Setiaji, S.Kom
Konstruksi dasar program PHP Pemilihan if if .. else, If.. else if ..else switch .. case, break Pengulangan while do .. while For foreach (iterasi setiap elemen array) Pencabangan break (keluar dari loop) continue (loncat ke iterasi loop berikutnya) Pemograman Web (2010/2011) – Hari Setiaji, S.Kom
Examples <?php // Konstruksi If $bulan=date(&quot;m&quot;); if ($bulan==4)  echo &quot;Bulan April&quot;; // Konstruksi If .. Else echo &quot;<br />&quot;; $today=date(&quot;w&quot;); if ($today==1)  echo &quot;Hari Senin&quot;; else  echo &quot;Bukan Hari Senin &quot;; // Konstruksi If .. Elseif.. Else echo &quot;<br />&quot;; $today=date(&quot;w&quot;); if ($today==1)  echo &quot;Hari Senin&quot;; elseif ($today==2) echo &quot;Hari Selasa&quot;; else  echo &quot;Bukan Hari Senin atau hari selasa &quot;; ?> PHP Browser Pemograman Web (2010/2011) – Hari Setiaji, S.Kom
Examples <?php $today=date(&quot;l&quot;); switch ($today) { case &quot;Sunday&quot; : echo &quot;Hari Minggu&quot;; break; case &quot;Monday&quot; : echo &quot;Hari Senin&quot;; break; case &quot;Tuesday&quot; : echo &quot;Hari Selasa&quot;; break; case &quot;Wednesday&quot; : echo &quot;Hari Rabu&quot;; break; default : echo &quot;Hari Sabtu&quot;; } ?> PHP Pemograman Web (2010/2011) – Hari Setiaji, S.Kom
Examples Hasil While <br /> <?php $bilangan =1; while ($bilangan <= 25) { echo $bilangan.&quot; &quot;; $bilangan++; } echo &quot;<br /><br />&quot;; ?> Hasil Do..While <br /> <?php $a=10; do { echo $a.&quot; &quot;; $a--; }  while ($a > 5); ?> <br /><br /><b>Hasil For</b> <br /> <?php for ($i=1;$i<=6;$i++) { echo &quot;<h&quot;.$i.&quot;>Header $i </h&quot;.$i.&quot;>&quot;; } ?>  Browser PHP Pemograman Web (2010/2011) – Hari Setiaji, S.Kom
Examples <b>Penggunaan Continue</b> <br /> <?php for ($i=1;$i<=15;$i++) { if ($i >5 && $i<=11)    continue; echo $i.&quot;<br /> &quot;; } ?> PHP Browser Pemograman Web (2010/2011) – Hari Setiaji, S.Kom
Array Array  adalah himpunan nilai yang elemennya dapat diacu berdasarkan indeks (angka) atau nama (string) Indeks Array (jika menggunakan angka) dimulai dari 0, meskipun tidak harus Pembuatan Array : 1. Nama_array   = new Array() ; 2. Nama_array[]=nilai_1; Nama_array[]=nilai_2; ; Nama_array[]=nilai_n; Pemograman Web (2010/2011) – Hari Setiaji, S.Kom
Examples <?php //Pembuatan Array cara I $mahasiswa = array (&quot;Dani&quot;,&quot;Andini&quot;,&quot;Sheila&quot;,&quot;Rudi&quot;); //Pembuatan Array cara II $mahasiswa[] = &quot;Dani&quot;; $mahasiswa[] = &quot;Andini&quot;; $mahasiswa[] = &quot;Sheila&quot;; $mahasiswa[] = &quot;Rudi&quot;; //Pembuatan Array dengan indeks yang tidak terurut $musik=array (&quot;Jazz&quot;,5=>&quot;Blues&quot;,&quot;Rock&quot;,10=>&quot;Dankdut&quot;); //Pengaksesan Array mahasiswa echo &quot;Elemen Array mahasiswa pertama adalah : $mahasiswa[0] <br />&quot;; echo &quot;Elemen Array mahasiswa ketiga adalah : $mahasiswa[2] <br />&quot;; //Pengaksesan Array musik echo &quot;Elemen Array musik pertama adalah : $musik[0] <br />&quot;; echo &quot;Elemen Array musik ketiga adalah : $musik[6] <br />&quot;; echo &quot;Elemen Array musik keempat adalah : $musik[10] <br />&quot;; ?> PHP Browser
Examples <?php //Pembuatan Array dengan indeks string $hari=array (&quot;Sunday&quot;=>&quot;Minggu&quot;,   &quot;Monday&quot;=>&quot;Senin&quot;,   &quot;Tuesday&quot;=>&quot;Selasa&quot;,   &quot;Wednesday&quot;=>&quot;Rabu&quot;,   &quot;Thursday&quot;=>&quot;Kamis&quot;,   &quot;Friday&quot;=>&quot;Jumat&quot;,   &quot;Saturday&quot;=>&quot;Sabtu&quot;   ); echo &quot;jumlah elemen Array : &quot;.count($hari).&quot;<br />&quot;; $hari_inggris=date(&quot;l&quot;); echo &quot;Hari ini adalah hari $hari[$hari_inggris]&quot;; ?> PHP Browser Pemograman Web (2010/2011) – Hari Setiaji, S.Kom
Examples <?php //Array Multidimensi $peserta = array ( &quot;PHP&quot; => array(&quot;Anto&quot;,&quot;Dani&quot;,&quot;Rendra&quot;,&quot;Lani&quot;), &quot;MySQL&quot; => array(&quot;Bayu&quot;,&quot;Lina&quot;,&quot;Diana&quot;,&quot;Rinto&quot;), &quot;Delphi&quot; => array(&quot;Doni&quot;,&quot;Dini&quot;,&quot;Ela&quot;,&quot;Aryo&quot;), &quot;JSP&quot; => array(&quot;Fia&quot;,&quot;Rina&quot;,&quot;Roni&quot;,&quot;Dian&quot;)); //Pengaksesan dengan While while (list($indeks_1, $nilai_1) = each($peserta)) { echo &quot;<b>Peserta $indeks_1 </b>: <br />\n&quot;; $nomor=1; while (list($indeks_2,$nilai_2) = each($nilai_1)) { echo $nomor.&quot;.&quot;.$nilai_2.&quot;<br />\n&quot;; $nomor++; } } Browser PHP Pemograman Web (2010/2011) – Hari Setiaji, S.Kom
Examples <?php //Array Multidimensi $peserta = array ( &quot;PHP&quot; => array(&quot;Anto&quot;,&quot;Dani&quot;,&quot;Rendra&quot;,&quot;Lani&quot;), &quot;MySQL&quot; => array(&quot;Bayu&quot;,&quot;Lina&quot;,&quot;Diana&quot;,&quot;Rinto&quot;), &quot;Delphi&quot; => array(&quot;Doni&quot;,&quot;Dini&quot;,&quot;Ela&quot;,&quot;Aryo&quot;), &quot;JSP&quot; => array(&quot;Fia&quot;,&quot;Rina&quot;,&quot;Roni&quot;,&quot;Dian&quot;)); //Pengaksesan dengan Foreach foreach ($peserta as $indeks_1 => $nilai_1) { echo “<b>Peserta $indeks_1 </b>: <br />\n&quot;; $nomor=1; foreach ($nilai_1 as $indeks_2 => $nilai_2) {   echo $nomor.&quot;.&quot;.$nilai_2.&quot;<br />\n&quot;;   $nomor++; } } ?> Browser PHP Pemograman Web (2010/2011) – Hari Setiaji, S.Kom
PHP pada pemrograman web Alternatif Sumber Data Input Parameter URL:  $_GET Form handling:  $_GET, $_POST, $_FILES Cookie:  $_COOKIE Session:  session_start(), $_SESSION File:  fopen(), fread(), fclose(), readfile( ) ,   file_get_contents() Database:  connect, select_db, query, fetch, close Pemograman Web (2010/2011) – Hari Setiaji, S.Kom
PHP pada pemrograman web Alternatif Output Image (ke browser):  fpassthru(), imagejpeg(), imagegif(), imagepng() Dengan cara meng-output-kan isi file image, atau  create image from scratch File:  fopen(), fwrite(), fclose() Cookie:  setcookie() Session:  session_start(), $_SESSION Database:  connect, select_db, query, fetch, close Proses Script PHP dieksekusi ketika file dokumen web di- request  oleh browser Atau dipanggil oleh aplikasi/script lain sebagai command pada OS shell Pemograman Web (2010/2011) – Hari Setiaji, S.Kom
Input dari Parameter URL Dapat digunakan untuk mengetahui link mana yang di-klik oleh user Sebuah link dapat mewakili  data  dan/atau  aksi <? $aksi =  $_GET [&quot; act &quot;]; $id =  $_GET [&quot; id &quot;]; if ($aksi == &quot;edit&quot;) { //lakukan edit terhadap data dengan ID = $id } else if ($aksi == &quot;Delete&quot;) { //lakukan delete terhadap data dengan ID = $id } ?> <html> 1. Jaket <a href='go.php? id = 1 & act = edit '>Edit</a> <a href='go.php? id = 1 & act = delete '>Delete</a><br> 2. Sepatu <a href='go.php? id = 2 & act = edit '>Edit</a> <a href='go.php? id = 2 & act = delete '>Delete</a> </html> HTML Browser PHP:  go.php
Input dari Form HTML Dapat digunakan untuk mendapatkan data yang dimasukkan oleh user <? $nama =  $_POST [&quot; nama &quot;];  //berisi string nama $jenis =  $_POST [&quot; jenis &quot;];  //berisi &quot;L&quot; atau &quot;P&quot; //simpan data $nama dan $jenis ?> <html> <form action='save.php' method=' POST '> Nama<br> <input type='text' name=' nama '><br> Jenis<br> <input type='radio' name=' jenis ' value=' L '>Laki-laki<br> <input type='radio' name=' jenis ' value=' P '>Perempuan<br> <input type='submit' value='Simpan'> </form> </html> HTML Browser PHP:  save.php
Input dari Cookie Dapat digunakan untuk mendapatkan data yang dimasukkan oleh user pada halaman sebelumnya <html> <form action='login.php' method='POST'> User <input type='text' name=' user '><br> Password <input type=‘password' name='pass'><br> <input type='submit' value='Login'> </form> </html> Browser <? $user =  $_COOKIE [&quot; login &quot;];  //berisi string username if ($user == &quot;&quot;) {  //belum melakukan login header(&quot;Location: login.html&quot;);  //redirect ke halaman login } else { // User sudah login, boleh melakukan sesuatu } ?> PHP:  anypage.php <? $user = $_POST[&quot; user &quot;];  //berisi string username $pass = $_POST[&quot;pass&quot;];  //berisi string password if ( )) { //simpan $user di cookie setcookie (&quot; login &quot;, $user); } ?> PHP:  login.php HTML:  login.html
Input dari Session Dapat digunakan untuk mendapatkan data yang dimasukkan oleh user pada halaman sebelumnya <html> <form action=‘anypage.php' method='POST'> User <input type='text' name=' user '><br> Password <input type='text' name='pass'><br> <input type='submit' value='Login'> </form> </html> HTML:  login.html Browser <? Include (“login.php”); $user =  $_SESSION [&quot; login &quot;];  //berisi string username if ($user == &quot;&quot;) {  //belum melakukan login header(&quot;Location: login.html&quot;);  //redirect ke halaman login } else { // User sudah login, boleh melakukan sesuatu } ?> PHP:  anypage.php <? $user = $_POST[&quot; user &quot;];  //berisi string username $pass = $_POST[&quot;pass&quot;];  //berisi string password if (cekPass($user, $pass)) { //simpan $user di session session_start(); $_SESSION [&quot; login &quot;] = $user; } ?> PHP:  login.php
Finally Pemograman Web (2010/2011) – Hari Setiaji, S.Kom

More Related Content

More from Hari Setiaji (17)

PDF
Project Management Tools
Hari Setiaji
 
PDF
Database Jaman Now
Hari Setiaji
 
PDF
Introduction to ajax
Hari Setiaji
 
PPT
Web Programming - Javascript
Hari Setiaji
 
DOCX
Tutorial Postgre SQL
Hari Setiaji
 
PPT
DTD - Atribut dan Entities
Hari Setiaji
 
PPT
Teknologi XML - Pengenalan DTD
Hari Setiaji
 
PPT
Teknologi XML - Pengenalan Tree
Hari Setiaji
 
PPT
HTML - Form
Hari Setiaji
 
PPT
HTML Dasar
Hari Setiaji
 
PPT
Pengenalan XML
Hari Setiaji
 
PPT
Internet dan Web
Hari Setiaji
 
PPTX
Perkembangan Web
Hari Setiaji
 
PPT
Bab II Use Case Diagram
Hari Setiaji
 
DOCX
Native Xml Tutorial
Hari Setiaji
 
PPTX
Persentasi Ajax Native Xml
Hari Setiaji
 
DOC
Distributed Database Using Oracle
Hari Setiaji
 
Project Management Tools
Hari Setiaji
 
Database Jaman Now
Hari Setiaji
 
Introduction to ajax
Hari Setiaji
 
Web Programming - Javascript
Hari Setiaji
 
Tutorial Postgre SQL
Hari Setiaji
 
DTD - Atribut dan Entities
Hari Setiaji
 
Teknologi XML - Pengenalan DTD
Hari Setiaji
 
Teknologi XML - Pengenalan Tree
Hari Setiaji
 
HTML - Form
Hari Setiaji
 
HTML Dasar
Hari Setiaji
 
Pengenalan XML
Hari Setiaji
 
Internet dan Web
Hari Setiaji
 
Perkembangan Web
Hari Setiaji
 
Bab II Use Case Diagram
Hari Setiaji
 
Native Xml Tutorial
Hari Setiaji
 
Persentasi Ajax Native Xml
Hari Setiaji
 
Distributed Database Using Oracle
Hari Setiaji
 

Web Programming - PHP

  • 1. Web Programming– PHP PHP Hypertext Preprocessor Pemrograman Web (2010/2011) Teknik Informatika, Universitas Islam Indonesia Follow Hari Setiaji on Twitter
  • 2. Sekilas Pemograman Web (2010/2011) – Hari Setiaji, S.Kom Bahasa pemrograman script di sisi server (server-side scripting) Berupa script yang disisipkan di dalam dokumen HTML, embedded script yang diinterpretasi (bukan di-compile) Dibuat pertama kali oleh Rasmus Lerdorf PHP singkatan dari: P ersonal H ome P age (awalnya) P rofessional H ome P age (kemudian) P HP: H ypertext P reprocessor (resminya) Official Website : http://www.php.net PHP versi terakhir : 5.x Free & opensource Multi platform: Windows, Linux, Mac Menyediakan Library/API yang menyeluruh: Database : MySQL, Oracle, postgreSQL, IBM DB2, ODBC, dll Protocol : HTTP, FTP, POP3, IMAP, LDAP, SNMP, dll Output : HTML, XML, JPEG, GIF, PNG, PDF, SWF dll
  • 3. Alur Kerja PHP Pemograman Web (2010/2011) – Hari Setiaji, S.Kom
  • 4. Contoh Penyisipan Script PHP <html> <? /* Cara I: script PHP dideklarasikan di sini*/ echo &quot;halo 1<br>&quot;; ?> <?php /* Cara II: script PHP dideklarasikan di sini*/ echo &quot;halo 2<br>&quot;; ?> <script language=&quot;php&quot;> /* Cara III: script PHP dideklarasikan di sini*/ echo &quot;halo 3&quot;; </script> </html> <html> halo 1<br> halo 2<br> halo 3</html> PHP HTML Browser Pemograman Web (2010/2011) – Hari Setiaji, S.Kom
  • 5. Variabel & Konstanta case sensitive variable dengan prefiks: $ variable tidak perlu dideklarasi variable scope: local (default) global, dengan menyebutkan: global $namavar; Static, dengan menyebutkan: static $namavar; Predefined variables (didefinisikan oleh web server), berupa variabel Array Server variables: $_SERVER Environment variables: $_ENV Cookie: $_COOKIE Request parameter: $_GET , $_POST , $_FILES konstanta Pendefinisian: define(&quot;pi&quot;, 3.14); Konstanta terdefinisi, contoh: PHP_VERSION (versi PHP), __FILE__ (nama file yang sedang dijalankan), __LINE__ (nomor baris program yang sedang dijalankan), dll Pemograman Web (2010/2011) – Hari Setiaji, S.Kom
  • 6. Operator Assignment: = , += , -= , *= , /= , %= ( $ a += 1 artinya $a = $a + 1 ) Aritmatika: + , - , * , / , % Operator pre/post increment/decrement: ++$a , --$a , $b++ , $b-- Perbandingan: == (equal), === (identical, sama nilai dan sama tipe), != , > , < , >= , <= Operator logika: && (and), || (or), ! (not) Operator kondisi: ? $jenis = ($bil % 2 == 0 ? ”Genap” : ”Ganjil”); Operator string: . (concat) Operator new (untuk object): new Pemograman Web (2010/2011) – Hari Setiaji, S.Kom
  • 7. Examples <?php echo &quot;<h3>Postincrement</h3>&quot;; $a = 5; echo &quot;Should be 5: &quot; . $a++ . &quot;<br />\n&quot;; echo &quot;Should be 6: &quot; . $a . &quot;<br />\n&quot;; echo &quot;<h3>Preincrement</h3>&quot;; $a = 5; echo &quot;Should be 6: &quot; . ++$a . &quot;<br />\n&quot;; echo &quot;Should be 6: &quot; . $a . &quot;<br />\n&quot;; echo &quot;<h3>Postdecrement</h3>&quot;; $a = 5; echo &quot;Should be 5: &quot; . $a-- . &quot;<br />\n&quot;; echo &quot;Should be 4: &quot; . $a . &quot;<br />\n&quot;; echo &quot;<h3>Predecrement</h3>&quot;; $a = 5; echo &quot;Should be 4: &quot; . --$a . &quot;<br />\n&quot;; echo &quot;Should be 4: &quot; . $a . &quot;<br />\n&quot;; ?> PHP Browser Pemograman Web (2010/2011) – Hari Setiaji, S.Kom
  • 8. Examples <?php $a=5; $b=&quot;5&quot;; $hasil = $a==$b; echo &quot;$hasil <br />&quot;; $a=5; $c=5; $hasil = $a===$c; echo &quot;$hasil <br />&quot;; $jenis = ($a % 2 == 0 ? Genap : Ganjil); echo $a.&quot; adalah bilangan $jenis&quot;; ?> PHP Browser Pemograman Web (2010/2011) – Hari Setiaji, S.Kom
  • 9. Konstruksi dasar program PHP Pemilihan if if .. else, If.. else if ..else switch .. case, break Pengulangan while do .. while For foreach (iterasi setiap elemen array) Pencabangan break (keluar dari loop) continue (loncat ke iterasi loop berikutnya) Pemograman Web (2010/2011) – Hari Setiaji, S.Kom
  • 10. Examples <?php // Konstruksi If $bulan=date(&quot;m&quot;); if ($bulan==4) echo &quot;Bulan April&quot;; // Konstruksi If .. Else echo &quot;<br />&quot;; $today=date(&quot;w&quot;); if ($today==1) echo &quot;Hari Senin&quot;; else echo &quot;Bukan Hari Senin &quot;; // Konstruksi If .. Elseif.. Else echo &quot;<br />&quot;; $today=date(&quot;w&quot;); if ($today==1) echo &quot;Hari Senin&quot;; elseif ($today==2) echo &quot;Hari Selasa&quot;; else echo &quot;Bukan Hari Senin atau hari selasa &quot;; ?> PHP Browser Pemograman Web (2010/2011) – Hari Setiaji, S.Kom
  • 11. Examples <?php $today=date(&quot;l&quot;); switch ($today) { case &quot;Sunday&quot; : echo &quot;Hari Minggu&quot;; break; case &quot;Monday&quot; : echo &quot;Hari Senin&quot;; break; case &quot;Tuesday&quot; : echo &quot;Hari Selasa&quot;; break; case &quot;Wednesday&quot; : echo &quot;Hari Rabu&quot;; break; default : echo &quot;Hari Sabtu&quot;; } ?> PHP Pemograman Web (2010/2011) – Hari Setiaji, S.Kom
  • 12. Examples Hasil While <br /> <?php $bilangan =1; while ($bilangan <= 25) { echo $bilangan.&quot; &quot;; $bilangan++; } echo &quot;<br /><br />&quot;; ?> Hasil Do..While <br /> <?php $a=10; do { echo $a.&quot; &quot;; $a--; } while ($a > 5); ?> <br /><br /><b>Hasil For</b> <br /> <?php for ($i=1;$i<=6;$i++) { echo &quot;<h&quot;.$i.&quot;>Header $i </h&quot;.$i.&quot;>&quot;; } ?> Browser PHP Pemograman Web (2010/2011) – Hari Setiaji, S.Kom
  • 13. Examples <b>Penggunaan Continue</b> <br /> <?php for ($i=1;$i<=15;$i++) { if ($i >5 && $i<=11) continue; echo $i.&quot;<br /> &quot;; } ?> PHP Browser Pemograman Web (2010/2011) – Hari Setiaji, S.Kom
  • 14. Array Array adalah himpunan nilai yang elemennya dapat diacu berdasarkan indeks (angka) atau nama (string) Indeks Array (jika menggunakan angka) dimulai dari 0, meskipun tidak harus Pembuatan Array : 1. Nama_array = new Array() ; 2. Nama_array[]=nilai_1; Nama_array[]=nilai_2; ; Nama_array[]=nilai_n; Pemograman Web (2010/2011) – Hari Setiaji, S.Kom
  • 15. Examples <?php //Pembuatan Array cara I $mahasiswa = array (&quot;Dani&quot;,&quot;Andini&quot;,&quot;Sheila&quot;,&quot;Rudi&quot;); //Pembuatan Array cara II $mahasiswa[] = &quot;Dani&quot;; $mahasiswa[] = &quot;Andini&quot;; $mahasiswa[] = &quot;Sheila&quot;; $mahasiswa[] = &quot;Rudi&quot;; //Pembuatan Array dengan indeks yang tidak terurut $musik=array (&quot;Jazz&quot;,5=>&quot;Blues&quot;,&quot;Rock&quot;,10=>&quot;Dankdut&quot;); //Pengaksesan Array mahasiswa echo &quot;Elemen Array mahasiswa pertama adalah : $mahasiswa[0] <br />&quot;; echo &quot;Elemen Array mahasiswa ketiga adalah : $mahasiswa[2] <br />&quot;; //Pengaksesan Array musik echo &quot;Elemen Array musik pertama adalah : $musik[0] <br />&quot;; echo &quot;Elemen Array musik ketiga adalah : $musik[6] <br />&quot;; echo &quot;Elemen Array musik keempat adalah : $musik[10] <br />&quot;; ?> PHP Browser
  • 16. Examples <?php //Pembuatan Array dengan indeks string $hari=array (&quot;Sunday&quot;=>&quot;Minggu&quot;, &quot;Monday&quot;=>&quot;Senin&quot;, &quot;Tuesday&quot;=>&quot;Selasa&quot;, &quot;Wednesday&quot;=>&quot;Rabu&quot;, &quot;Thursday&quot;=>&quot;Kamis&quot;, &quot;Friday&quot;=>&quot;Jumat&quot;, &quot;Saturday&quot;=>&quot;Sabtu&quot; ); echo &quot;jumlah elemen Array : &quot;.count($hari).&quot;<br />&quot;; $hari_inggris=date(&quot;l&quot;); echo &quot;Hari ini adalah hari $hari[$hari_inggris]&quot;; ?> PHP Browser Pemograman Web (2010/2011) – Hari Setiaji, S.Kom
  • 17. Examples <?php //Array Multidimensi $peserta = array ( &quot;PHP&quot; => array(&quot;Anto&quot;,&quot;Dani&quot;,&quot;Rendra&quot;,&quot;Lani&quot;), &quot;MySQL&quot; => array(&quot;Bayu&quot;,&quot;Lina&quot;,&quot;Diana&quot;,&quot;Rinto&quot;), &quot;Delphi&quot; => array(&quot;Doni&quot;,&quot;Dini&quot;,&quot;Ela&quot;,&quot;Aryo&quot;), &quot;JSP&quot; => array(&quot;Fia&quot;,&quot;Rina&quot;,&quot;Roni&quot;,&quot;Dian&quot;)); //Pengaksesan dengan While while (list($indeks_1, $nilai_1) = each($peserta)) { echo &quot;<b>Peserta $indeks_1 </b>: <br />\n&quot;; $nomor=1; while (list($indeks_2,$nilai_2) = each($nilai_1)) { echo $nomor.&quot;.&quot;.$nilai_2.&quot;<br />\n&quot;; $nomor++; } } Browser PHP Pemograman Web (2010/2011) – Hari Setiaji, S.Kom
  • 18. Examples <?php //Array Multidimensi $peserta = array ( &quot;PHP&quot; => array(&quot;Anto&quot;,&quot;Dani&quot;,&quot;Rendra&quot;,&quot;Lani&quot;), &quot;MySQL&quot; => array(&quot;Bayu&quot;,&quot;Lina&quot;,&quot;Diana&quot;,&quot;Rinto&quot;), &quot;Delphi&quot; => array(&quot;Doni&quot;,&quot;Dini&quot;,&quot;Ela&quot;,&quot;Aryo&quot;), &quot;JSP&quot; => array(&quot;Fia&quot;,&quot;Rina&quot;,&quot;Roni&quot;,&quot;Dian&quot;)); //Pengaksesan dengan Foreach foreach ($peserta as $indeks_1 => $nilai_1) { echo “<b>Peserta $indeks_1 </b>: <br />\n&quot;; $nomor=1; foreach ($nilai_1 as $indeks_2 => $nilai_2) { echo $nomor.&quot;.&quot;.$nilai_2.&quot;<br />\n&quot;; $nomor++; } } ?> Browser PHP Pemograman Web (2010/2011) – Hari Setiaji, S.Kom
  • 19. PHP pada pemrograman web Alternatif Sumber Data Input Parameter URL: $_GET Form handling: $_GET, $_POST, $_FILES Cookie: $_COOKIE Session: session_start(), $_SESSION File: fopen(), fread(), fclose(), readfile( ) , file_get_contents() Database: connect, select_db, query, fetch, close Pemograman Web (2010/2011) – Hari Setiaji, S.Kom
  • 20. PHP pada pemrograman web Alternatif Output Image (ke browser): fpassthru(), imagejpeg(), imagegif(), imagepng() Dengan cara meng-output-kan isi file image, atau create image from scratch File: fopen(), fwrite(), fclose() Cookie: setcookie() Session: session_start(), $_SESSION Database: connect, select_db, query, fetch, close Proses Script PHP dieksekusi ketika file dokumen web di- request oleh browser Atau dipanggil oleh aplikasi/script lain sebagai command pada OS shell Pemograman Web (2010/2011) – Hari Setiaji, S.Kom
  • 21. Input dari Parameter URL Dapat digunakan untuk mengetahui link mana yang di-klik oleh user Sebuah link dapat mewakili data dan/atau aksi <? $aksi = $_GET [&quot; act &quot;]; $id = $_GET [&quot; id &quot;]; if ($aksi == &quot;edit&quot;) { //lakukan edit terhadap data dengan ID = $id } else if ($aksi == &quot;Delete&quot;) { //lakukan delete terhadap data dengan ID = $id } ?> <html> 1. Jaket <a href='go.php? id = 1 & act = edit '>Edit</a> <a href='go.php? id = 1 & act = delete '>Delete</a><br> 2. Sepatu <a href='go.php? id = 2 & act = edit '>Edit</a> <a href='go.php? id = 2 & act = delete '>Delete</a> </html> HTML Browser PHP: go.php
  • 22. Input dari Form HTML Dapat digunakan untuk mendapatkan data yang dimasukkan oleh user <? $nama = $_POST [&quot; nama &quot;]; //berisi string nama $jenis = $_POST [&quot; jenis &quot;]; //berisi &quot;L&quot; atau &quot;P&quot; //simpan data $nama dan $jenis ?> <html> <form action='save.php' method=' POST '> Nama<br> <input type='text' name=' nama '><br> Jenis<br> <input type='radio' name=' jenis ' value=' L '>Laki-laki<br> <input type='radio' name=' jenis ' value=' P '>Perempuan<br> <input type='submit' value='Simpan'> </form> </html> HTML Browser PHP: save.php
  • 23. Input dari Cookie Dapat digunakan untuk mendapatkan data yang dimasukkan oleh user pada halaman sebelumnya <html> <form action='login.php' method='POST'> User <input type='text' name=' user '><br> Password <input type=‘password' name='pass'><br> <input type='submit' value='Login'> </form> </html> Browser <? $user = $_COOKIE [&quot; login &quot;]; //berisi string username if ($user == &quot;&quot;) { //belum melakukan login header(&quot;Location: login.html&quot;); //redirect ke halaman login } else { // User sudah login, boleh melakukan sesuatu } ?> PHP: anypage.php <? $user = $_POST[&quot; user &quot;]; //berisi string username $pass = $_POST[&quot;pass&quot;]; //berisi string password if ( )) { //simpan $user di cookie setcookie (&quot; login &quot;, $user); } ?> PHP: login.php HTML: login.html
  • 24. Input dari Session Dapat digunakan untuk mendapatkan data yang dimasukkan oleh user pada halaman sebelumnya <html> <form action=‘anypage.php' method='POST'> User <input type='text' name=' user '><br> Password <input type='text' name='pass'><br> <input type='submit' value='Login'> </form> </html> HTML: login.html Browser <? Include (“login.php”); $user = $_SESSION [&quot; login &quot;]; //berisi string username if ($user == &quot;&quot;) { //belum melakukan login header(&quot;Location: login.html&quot;); //redirect ke halaman login } else { // User sudah login, boleh melakukan sesuatu } ?> PHP: anypage.php <? $user = $_POST[&quot; user &quot;]; //berisi string username $pass = $_POST[&quot;pass&quot;]; //berisi string password if (cekPass($user, $pass)) { //simpan $user di session session_start(); $_SESSION [&quot; login &quot;] = $user; } ?> PHP: login.php
  • 25. Finally Pemograman Web (2010/2011) – Hari Setiaji, S.Kom

Editor's Notes

  • #2: Pemrograman Web