pertemuan2-pemrogramanwebphp

25
Pemrograman Web Dasar-dasar PHP: Pengantar PHP • sebuah file PHP, PHP workings, PHP feathers, menjalankan PHP. Sintaks Dasar PHP • variable, operator, if...else...and switch, while, do while, dan for. Beberapa fungsi PHP yang berguna Bagaimana bekerja dengan : • HTML forms, cookies, files, time dan date. Bagaimana membuat pemeriksa dasar untuk data yang diinput penguna

Upload: taufiq-rahman

Post on 16-Sep-2015

214 views

Category:

Documents


0 download

DESCRIPTION

as

TRANSCRIPT

  • Pemrograman WebDasar-dasar PHP: Pengantar PHP sebuah file PHP, PHP workings, PHP feathers, menjalankan PHP. Sintaks Dasar PHP variable, operator, if...else...and switch, while, do while, dan for. Beberapa fungsi PHP yang berguna Bagaimana bekerja dengan : HTML forms, cookies, files, time dan date. Bagaimana membuat pemeriksa dasar untuk data yang diinput penguna

  • PHPPHP mirip dengan JavaScript, hanya server-sideKode PHP adalah embedded dalam HTML menggunakan tagKetika sebuah page yang diminta tiba, server mengenali isi dari PHP melalui ekstensi dari file (.php , .php3, or .phtml)server mengeksekusi kode PHP, mengganti output menjadi HTMLpage yang dihasilkan kemudian dikirim ke clientPengguna tidak pernah melihat kode PHP, hanya output berupa pageDibuat tahun 1995 oleh Rasmus Lerdorf (anggota dari Apache Group)Awalnya didesain sebagai sebuah tool untuk mentracking pengunjung pada Web site LerdorfDalam 2 tahun, digunakan secara luas bergabung dengan server Apache Dikembangkan menjadi lengkap, bahasa script untuk pemrograman server-sideGratis (free), kode sumber terbuka (open-source)server plug-ins ada untuk bermacam server

  • Apa yang anda butuhkan?

    server yang mendukung PHP Tidak perlu melakukan apapun! Tidak perlu mengcompile apapun atau instal tools lain! Buat beberapa file .php dalam web directory - dan server akan melakukannya.

    Kebanyakan server mendukung PHPDownload PHP (gratis) di: http://www.php.net/downloads.phpDownload MySQL (gratis) di : http://www.mysql.com/downloads/index.htmlDownload Apache (gratis) di : http://httpd.apache.org/download.cgi

  • Sintaks Dasar PHPBlok script PHP diawali dengan . Blok script PHP dapat ditempatkan dimanapun dalam dokumen.

    Hello World

    This is going to be ignored. This will also be ignored.

    Server akan menjalankan statement print dan echo, substitutes output.print dan echountuk outputa semicolon (;) diakhir setiap statement (dapat dihilangkan pada akhir block/file)// untuk komentar 1 baris/* dan */ untuk blok komentar.

  • ScalarsSemua variable dalam PHP dimulai dengan simbol $. Tipe variable ditentukan oleh konteks dimana variable digunakan.

  • ArraysArray dalam PHP adalah map yang terurut. Map adalah tipe yang memetakan value ke key.array() = membuat array

    key = integer atau string. value = tipe PHP.

  • ConstantsConstant adalah identifier (nama) untuk value sederhana. Constant adaah case-sensitive. Berdasarkan konvensi, nama constant selalu uppercase.

    Anda dapat memakai constants dimanapun dalam script without regard to scope.

  • OperatorsOperator Arithmetic : +, -, *,/ , %, ++, --Operator Assignment : =, +=, -=, *=, /=, %=

    Operator Comparison: ==, !=, >, =,

  • Conditionals: if elseCan execute a set of code depending on a condition

    if (kondisi)kode yg akan dieksekusi jika kondisi adalah benar (true);elsekode yg akan dieksekusi jika kondisi adalah salah (false);

  • Conditionals: switchDapat memilih satu dari banyak pilihan untuk dieksekusi

    switch (expression){case label1: kode yang akan dieksekusi jika expression = label1; break; case label2: kode yang akan dieksekusi jika expression = label2; break;default: kode yang akan dieksekusi jika expression is different from both label1 and label2;}

  • Looping: while dan do-whileDapat loop bergantung pada kondisi

  • Lopping: for and foreachCan loop depending on a "counter"

    loop ke dalam sebuah blok kode sebanyak yang ditentukan

  • User Defined FunctionsDapat mendefinisikan sebuah fungsi menggunakan sintaks seperti berikut :

    Dapat juga mendefinisikan fungsi conditional, fungsi dalam fungsi, dan fungsi rekursif.

    Dapat mengembalikan nilai dari tipe apapun

  • Variable ScopeThe scope of a variable is the context within which it is defined.

    The scope is local within functions.

  • Including FilesStatemen include() mengikutsertakan dan mengevaluasi file tertentu.vars.php

    test.php

    *Scope bergantung pada tempat penambahan!

  • PHP InformationFungsi phpinfo()digunakan untuk menyampaikan informasi PHP.

    INFO_GENERALBaris Konfigurasi, lokasi php.ini, tanggal build, Server Web, Sistem dan lainnyaINFO_CREDITSPHP 4 creditsINFO_CONFIGURATIONLocal and master values for php directivesINFO_MODULESmodul yang diLoadINFO_ENVIRONMENTInformasi variabel EnvironmentINFO_VARIABLESSemua variable yang telah didefinisikan sebelumnya dari EGPCSINFO_LICENSEInformasi lisensi PHP INFO_ALLMenyampaikan semua yang diatas

  • Server VariablesThe $_SERVER is a reserved variable that contains all server information.

  • File OpenThe fopen("file_name","mode") function is used to open files in PHP.

    rRead only. r+Read/Write.wWrite only. w+Read/Write. aAppend. a+Read/Append.xCreate and open for write only. x+Create and open for read/write.If the fopen() function is unable to open the specified file, it returns 0 (false).

    For w, and a, if no file exists, it tries to create it.For x if a file exists, it returns an error.

  • Bekerja dengan File fclose() closes a file. feof() determines if the end is true.fgetc() reads a single character

    fgets() reads a line of datafwrite(), fputs writes a string with and without \n

  • Form HandlingAny form element is automatically be available.

    Enter your name: Enter your age:

    Welcome .You are years old!

    $_POST contains all POST data.

    $_GET contains all GET data.

  • Cookie Workingssetcookie(name,value,expire,path,domain) creates cookies.

    Dear , a cookie was set on thispage! The cookie will be active when the client has sent thecookie back to the server.

    setcookie() must appear BEFORE

  • Mendapatkan Time dan Datedate() and time () formats a time or a date.

    date() returns a string formatted according to the specified format.*Here more on formats: http://uk.php.net/manual/en/function.date.php

    time() returns current Unix timestamp

  • Required Fields in User-Entered DataA multipurpose script which asks users for some basic contact information and then checks to see that the required fields have been entered.

    First Name:

  • Fungsi Check dan Confirmfunction check_form($f_name, $l_name, $email, $os){ if (!$l_name||!$email){ echo "You are missing some required fields!"; print_form($f_name, $l_name, $email, $os); } else{ confirm_form($f_name, $l_name, $email, $os); }}function confirm_form($f_name, $l_name, $email, $os){?>

    Thanks! Below is the information you have sent to us.Contact Info

  • Program Utama/*Main Program*/

    if (!$_POST["submit"]){?>

    Please enter your information Fields with a "*" are required.