Upload presentasi
Presentasi sedang didownload. Silahkan tunggu
Diterbitkan olehMeilani Adja Telah diubah "9 tahun yang lalu
1
WEEK #03 – PHP
2
Review
3
Review
4
4 Review Server Side Scripting (keunggulan) No browser compability issues Access to server-side resources Reduced load on the client PHP vs HTML Dynamic VS Static databases VS no database User submit and fetch data to/from database VS fetch webpages only
5
5 Materi PHP Mechanism Basic Syntax Datatypes Variables
7
What is PHP? PHP is an HTML-embedded scripting language. Much of its syntax is borrowed from C, Java and Perl with a couple of unique PHP-specific features thrown in. The goal of the language is to allow web developers to write dynamically generated web pages quickly
8
How does PHP work ?
10
10 Default Syntax Default delimiter (pembatas) syntax dibuka dengan Contoh: Welcome! <?php echo " Some dynamic output here "; ?> Some static output here
11
Default Syntax Jika kode di atas disimpan, lalu dieksekusi pada sebuah web server yang mendukung PHP maka hasilnya sebagai berikut
12
Short-Tags Bagi user yang kurang termotivasi untuk mengetik sintaks, delimiter yang lebih pendek juga tersedia. Hal ini dikenal dengan istilah short-tags. Untuk menggunakan fitur ini, PHP’s short_open_tag terlebih dahulu harus diaktifkan. Pada php.ini >> short_open_tag = On Contoh: <? print "This is another PHP example."; ?>
13
Short-Tags Jika short open tag sudah aktif, dapat digunakan “escape” ke dan dari PHP untuk menghasilkan teks dinamis dengan cara menghilangkan statements “print”. Variasi ini dikenal dengan istilah short-circuit syntax sama dengan
14
Short-Tags Preview on web browser
15
Script PHP juga memiliki variasi delimiter yang lain yaitu Penggunaannya, contoh: print "This is another PHP example.";
16
Embedding Multiple Code Blocks PHP memberikan kemudahan untuk “escape” ke dan dari sebanyak yang diperlukan dalam suatu halaman web tertentu. Contoh: Next slide >>
17
Embedding Multiple Code Blocks <?php $date = date("F d, Y"); ?> Tanggal hari ini adalah Codes
18
Embedding Multiple Code Blocks Preview on web browser
19
19 Comments Comments diperlukan bukan saja untuk diri anda sendiri, namun untuk programmer lain. PHP menawarkan variasi sintaks untuk comments Single-Line C++ Syntax Shell Syntax Multiple-Line C Syntax
20
20 Single-Line C++ Syntax PHP mendukung C + + single-line sintaks komentar, yang diawali dengan (//), slash ganda. Contoh: <?php // Title: PHP script pertama // Author: Guson echo "Ini adalah program PHP"; ?>
21
21 Shell Syntax PHP juga mendukung alternatif single-line C + + syntax, yang dikenal sebagai shell sintaks, yang diawali dengan tanda pagar (#) Contoh: <?php # Title: PHP script pertama # Author: Guson echo "Ini adalah program PHP"; ?>
22
22 Multiple-Line C++ Syntax PHP juga menawarkan multi-line varian yang dapat membuka (/*) dan menutup (*/) komentar pada baris yang berbeda Contoh: <?php /* Title: PHP script pertama Author: Guson Date: June 18, 2010 */ ?>
23
23 Outputting Data to the Browser print() Statement Statement print(), memberikan output ke browser. Contoh: <?php $season = "summertime"; print " I love the $season. "; ?>
24
24 Outputting Data to the Browser echo() Statement Atau, bisa menggunakan echo () statement untuk tujuan sama seperti print () Contoh: <?php $a = "Opera"; $b = "Mozilla"; echo $a, " and ", $b, " are web browsers."; ?>
25
Outputting Data to the Browser PHP Echo Vs Print: Which Is Faster? “In an exclusive LearnPHPOnline.com test, we found that through an extended amount of iterations through a loop, Echo did indeed turn out to be the winner in speed! Whereas it took around 550ms for Print, the complete iteration took around 450ms for Echo. (For those without a calculator or a quick noggin, that’s almost 20% difference.) ” Source: http://www.learnphponline.com/php-basics/php-echo-vs-printhttp://www.learnphponline.com/php-basics/php-echo-vs-print
26
Outputting Data to the Browser Result:
27
27 Outputting Data to the Browser printf() Statement Sangat cocok untuk memadukan antara teks statik dan informasi dinamis yang disimpan dalam satu atau beberapa variable. Alasan? 1. Rapi dalam memisahkan data statis dan dinamis, sehingga memudahkan dalam maintenance. 2. Memberikan keleluasaan untuk memegang kendali atas bagaimana informasi yang dinamis yang diberikan kepada layar dalam hal jenis, presisi, keselarasan, dan posisi. Format: printf(string format [, mixed args])
28
Outputting Data to the Browser – printf() Misalnya, Anda ingin menyisipkan nilai integer dinamis tunggal (single dynamic integer value) menjadi dinyatakan dalam statis string: <?php printf("Inventori spidol: %d spidol dengan merk Artline 5109.", 200); ?>
29
Outputting Data to the Browser – printf() Result:
30
Outputting Data to the Browser – printf() Commonly used Type Specifiers
31
Outputting Data to the Browser – printf() Modifikasi: <?php printf("Harga %d spidol dengan merk Artline 5109 adalah Rp.%X.", 200, 0102000000); ?>
32
Outputting Data to the Browser – printf() Result:
34
34 Datatypes PHP mendukung datatypes. Datatypes adalah istilah generik yang diberikan kepada serangkaian karakteristik umum, meliputi Boolean, integer, float, string dan array. Ragam datatypes: 1. Scalar datatypes, 2. Compound datatypes,
35
1. Scalar Datatypes Scalar datatypes mengandung informasi tunggal. Kategori yang termasuk scalar datatypes adalah Boolean, integer, float, string.
36
Boolean Boolean hanya mendukung dua nilai yaitu TRUE dan FALSE. Cara lain, “0” mewakili FALSE, dan nilai selain 0 mewakili TRUE.
37
Boolean Converting other types to boolean:
38
38 Integer PHP mendukung nilai integer yang diwakili oleh decimal (10-based) hexadecimal (16-based) precede the number with 0x octal (8-based) notation precede the number with 0 optionally preceded by a sign (- or +). $a = 1234; // decimal number $a = -123; // a negative number $a = 0123; // octal number (equivalent to 83 decimal) $a = 0x1A; // hexadecimal number (equivalent to 26 decimal)
39
39 Integer PHP 5.0 ke bawah mendukung nilai maksimum integer -/+ 2 31 Fitur terbaru PHP 6.0 mendukung 64-bit nilai integer. Nilai tersebut sepadan dengan maksimum integer -/+ 2 63
40
Float Floating-point numbers dikenal pula sebagai float, double, atau real numbers. Float digunakan untuk mewakili nilai moneter, berat, jarak, dan berbagai representasi lain yang tidak dapat diwakili oleh nilai integer. Contoh: 3.2 billion as 3.2E9 4.5678 4.0
41
Strings Strings merupakan urutan dari karakter yang diperlakukan sebagai kelompok berdekatan. Pemakaian string dibatasi dengan single (‘’) atau double quotes (“ ”) Contoh: "PHP is a great language" "whoop-de-do" '*9subway\n' "123$%^789“ $color = "maroon";
42
2. Compound Datatypes Compound datatypes memungkinkan untuk beberapa item dari jenis yang sama dikumpulkan dibawah satu badan (entiti) perwakilan yang bersifat tunggal. Yang termasuk compound datatypes adalah Array, Object.
43
Array Mengatur rangkaian item yang sama dengan cara tertentu, dikenal dengan struktur data array. Setiap anggota indeks array (juga dikenal sebagai) referensi kunci nilai yang sesuai dan bisa menjadi acuan numerik sederhana untuk posisi nilai dalam seri, atau bisa memiliki korelasi langsung dengan nilai.
44
Array Misalnya, jika anda ingin membuat sebuah daftar provinsi di Indonesia menggunakan array diindeks secara numerik, seperti: $ provinsi [0] = “Sumatera Utara"; $ provinsi [1] = “Kalimantan Timur"; $ provinsi [2] = “Jawa Timur";
45
Array Bagaimana jika dinginkan adanya korelasi antara provinsi dengan ibukota provinsi menggunakan array ?
46
Array Solusi $ provinsi [“Sumatera Utara”] = “Medan"; $ provinsi [“Kalimantan Timur”] = “Samarinda"; $ provinsi [“Jawa Timur”] = “Surabaya";
47
Object Object merupakan center of the object- oriented programming paradigm. Tidak seperti datatypes sebelumnya, object harus terlebih dahulu di “declare”. Deklarasi karakteristik object dipresentasikan dengan class.
48
Object Contoh: class Appliance { private $_power; function setPower($status) { $this->_power = $status; }... $blender = new Appliance;
49
Object Deklarasi Nama class/ Struktur data > class Appliance Attribute > private $_power; Method > function setPower($status ) Object of class Appliance > $blender Atribut blender yaitu power dapat diset dengan menggunakan metode setPower(), contoh: $blender->setPower(“on”); atau $blender->setpower(“off”);
50
Konversi antar Datatypes Konversi antar datatypes dimungkinkan dengan menggunakan type casting. Tabel casting operator
51
Konversi antar Datatypes Contoh: Konversi double > integer <?php $skor = (int)14.65; echo "$skor"; ?>
52
Konversi antar Datatypes Hasil:
53
Konversi antar Datatypes Contoh: Konversi string > integer <?php $sentence = "This is a sentence"; echo (int) $sentence; ?>
54
Konversi antar Datatypes Hasil:
55
Konversi antar Datatypes Datatype dapat di “cast” menjadi object dengan attribute scalar. Contoh: <?php $model="Toyota"; $obj=(object) $model; print $obj->scalar; ?>
56
Konversi antar Datatypes Hasil
58
58 Identifiers Identifiers adalah terminologi umum untuk variables, functions, dan various other user- defined. Syarat yang harus dipenuhi: Dapat mengandung satu atau lebih karakter dan harus diawali oleh karakter atau underscore. Mengandung letters, numbers, underscore characters, dan kode ASCII dari 127 s/d 255. Identifiers case sensitive, sehingga (contoh: $resep berbeda dengan $Resep). Indentifiers tidak terbatas panjangnya (length). Identifier tidak boleh sama dengan (identik) PHP predefine keywords.
59
Identifiers Contoh:
60
60 Variables Variabel adalah sebuah simbol yang dapat merekam (memori) data dan dapat dimanipulasi untuk kepentingan pelaksanaan program. Deklarasi variable diawali dengan dollar sign ($), lalu diikuti dengan nama variable. Contoh: $color $operating_system $_some_variables $model $color ≠ $Color ≠ $coloR ≠ $COLOR
61
61 Variables Bagaimana menetapkan value sebuah variabel? Setelah $nama_variable dikuti dengan = “value” atau tanpa “”. Contoh: $color = “Red”; $operating_system = “Windows”; <?php $pengalaman_kerja= 12; $usia = 30; $usia_mulai_kerja = $usia - $pengalaman_kerja; echo "$usia_mulai_kerja"; ?>
62
62 Variables Hasil:
63
Variable Scope Kita dapat mendeklarasikan variable dimana saja pada skrip PHP. Kemudahan akses inilah dikenal dengan istilah variable scope. Ada 4 tipe scope yaitu Local variables Function parameters Global variables Static variables
64
64 Local Scope Local scope: Suatu variabel dinyatakan dalam sebuah fungsi dianggap lokal. Artinya, hanya dapat dirujuk pada fungsi yang bersangkutan saja, diluar itu dianggap sebagi variable baru. Contoh: next slide
65
Local scope Code <?php $x = 4; function assignx () { $x = 0; printf("\$x inside function is %d ", $x); } assignx(); printf("\$x outside of function is %d ", $x); ?>
66
Local scope Hasil
67
Function Parameters Function parameters dideklarasikan setelah nama fungsi dan di dalam tanda kurung (parentheses). Contoh: perkalian nilai dengan 10 dan return to caller. // multiply a value by 10 and return it to the caller function x10 ($value) { $value = $value * 10; // stop eksekusi $value return $value; }
68
68 Global Scope Berbeda dengan local scope, global scope dapat diakses di bagian manapun di dalam program. Untuk menggunakan variable ini, pergunakan keyword global di depan variable. Contoh: next slide >>
69
Global Scope Contoh: <?php $x = 4; function assignx () { global $x; $x = 3; printf("\$x inside function is %d ", $x); } assignx(); printf("\$x outside of function is %d ", $x); ?>
70
Global Scope Hasil:
71
71 Static Variable Berbeda dengan variable yang dideklarasikan dengan menggunakan function parameters, dimana variable akan “destroy” pada saat function exit. Static variable tidak kehilangan nilai meskipun function exits dan masih tetap menjaga nilai jika function kembali dipanggil. Bagaimana mendeklarasikan static variable? Dengan menambahkan keyword static di depan nama variable. Contoh: next slide
72
Static Variable (A) <?php function keep_track() { $count = 0; $count++; echo $count; echo " "; } keep_track(); ?> (B) <?php function keep_track() { STATIC $count = 0; $count++; echo $count; echo " "; } keep_track(); ?>
73
73 Static variable Bagimanakah hasil (A) dan (B) ?????
74
Static Variable (A) (B)
75
75 Predefined Variables $GLOBALS Contains a reference to every variable which is currently available within the global scope of the script. The keys of this array are the names of the global variables. $GLOBALS has existed since PHP 3. $_SERVER Variables set by the web server or otherwise directly related to the execution environment of the current script. Analogous to the old $HTTP_SERVER_VARS array (which is still available, but deprecated). $_GET Variables provided to the script via URL query string. Analogous to the old $HTTP_GET_VARS array (which is still available, but deprecated). $_POST Variables provided to the script via HTTP POST. Analogous to the old $HTTP_POST_VARS array (which is still available, but deprecated). $_SESSION Variables which are currently registered to a script's session.
76
76 Predefined Variables $_COOKIE Variables provided to the script via HTTP cookies. Analogous to the old $HTTP_COOKIE_VARS array (which is still available, but deprecated). $_FILES Variables provided to the script via HTTP post file uploads. Analogous to the old $HTTP_POST_FILES array (which is still available, but deprecated). See POST method uploads for more information. $_ENV Variables provided to the script via the environment. Analogous to the old $HTTP_ENV_VARS array (which is still available, but deprecated). $_REQUEST Variables provided to the script via the GET, POST, and COOKIE input mechanisms, and which therefore cannot be trusted.
77
Predefined Variables Contoh: <?php printf("Your IP address is: %s ", $_SERVER['REMOTE_ADDR']); printf("Your browser is: %s ",$_SERVER['HTTP_USER_AGENT']); printf("The path component of the URL: %s ",$_SERVER['REQUEST_URI']); ?>
78
Predefined Variables Hasil:
79
79 Variable from outside PHP Dengan menggunakan superglobal variable, variable dapat dipanggil dari luar PHP. Contoh penggunaan $_POST Contoh 7 Nama: Biaya makan siang: jumlah hari makan siang:
80
Variable from outside PHP HTML Form
81
Variable from outside PHP <?php /* print message about lunch cost */ print($_POST['nama']. ", akan menghabiskan uang"); print($_POST['biaya'] * $_POST['hari']); print(" Rupiah untuk makan siang. \n"); ?>
82
Variable from outside PHP Hasil
83
Variable from outside PHP Hasil, jika menggunakan $_GET
85
Rangkuman PHP Mechanism Basic Syntax Default tag short-tag Script embedding multiple code block Comments output data to browser: print(), echo(), and printf()
86
Rangkuman Datatypes Scalar datatypes: boolean,integer, float, dan string Compound datatypes: array, dan object, Konversi antar datatype > type casting Variables Local variables Function parameters Global variables Static variables
Presentasi serupa
© 2024 SlidePlayer.info Inc.
All rights reserved.