• Tidak ada hasil yang ditemukan

Pengenalan PHP. 1. PHP adalah sebuah bahasa pemograman yang tidak melakukan sebuah kompilasi dalam penggunaannya.

N/A
N/A
Protected

Academic year: 2022

Membagikan "Pengenalan PHP. 1. PHP adalah sebuah bahasa pemograman yang tidak melakukan sebuah kompilasi dalam penggunaannya."

Copied!
21
0
0

Teks penuh

(1)

Pengenalan PHP

PHP (Personal Home Page): Hypertext Preprocessor adalah bahasa skrip yang dapat ditanamkan atau disisipkan dalam HTML, banyak digunakan pada situs web dinamis. PHP adalah salah satu dari pemograman yang berbasis Objek.

Kelebihan dari PHP (wikipedia):

1. PHP adalah sebuah bahasa pemograman yang tidak melakukan sebuah kompilasi dalam penggunaannya.

2. Web server yang mendukung PHP sangat banyak sehingga dapat dikonfigurasi dengan mudah con : apache, xitami, xampp dll.

3. Banyak milis-milis dan developer yang siap membantu dalam pengembanngannya sehingga lebih mudah dikembangkan.

4. Memiliki referensi yang banyak sehingga mudah dipahami.

5. PHP adalah bahasa open source yang dapat dipergunakan di berbagai mesin (Linux, Windows, Macintosh, Unix) dan dapat dijalankan secara runtime melalui console dan juga dapat mekukan perintah-perintah system.

Apa yang bisa dilakukan oleh PHP :

1. Php dapat disisipkan dalam web dimanis

2. Php dapat membuat, membuaka, membaca, menulis, menghapus dan menutup file dalam server

3. Php dapat dipergunakan untuk mengumpulkan data 4. Php dapat menerima dan mengirimkan cookies

5. Php dapat menambahan, menghapus dan memodifikasi data dalam database

(2)

 Tempat menuliskan kode berada di tengah-tengah <body> dan </body>

 Tempat penyimpanan di :

 Untuk melihat hasil di IE/Mozilla/chrome/browser lain :

Contoh script PHP :

(3)

PENGENALAN PHP

Pengertian PHP (PHP Hypertext Preprocessor)

PHP atau PHP Hypertext Prepocessor adalah

sebuah bahasa pemrograman web berbasis

server (server-side) yang mampu mem-parsing

kode php dari kode web dengan ekstensi .php,

sehingga menghasilkan tampilan website yang

dinamis di sisi client (browser).

(4)

What is a PHP File?

• PHP files can contain text, HTML, CSS, JavaScript, and PHP code

• PHP code are executed on the server, and the result is returned to the browser as plain HTML

• PHP files have extension ".php"

(5)

What Can PHP Do?

• PHP can generate dynamic page content

• PHP can create, open, read, write, delete, and close files on the server

• PHP can collect form data

• PHP can send and receive cookies

• PHP can add, delete, modify data in your database

• PHP can restrict users to access some pages on your website

• PHP can encrypt data (proses membuat sandi atau mengacak data untuk menyelubungi pesan )

• With PHP you are not limited to output HTML.

You can output images, PDF files, and even

Flash movies. You can also output any text,

such as XHTML and XML.

(6)

Why PHP?

• PHP runs on various platforms (Windows, Linux, Unix, Mac OS X, etc.)

• PHP is compatible with almost all servers used today (Apache, IIS, etc.)

• PHP supports a wide range of databases (MySQL, PosgreSQL, Oracle, SQL Server, access)

• PHP is free. Download it from the official PHP resource:

www.php.net

• PHP is easy to learn and runs efficiently on the server side

• The PHP script is executed on the server, and the plain HTML result is sent back to the browser.

(7)

Memasukkan Kode PHP

Hasil akhir pengolahan kode PHP akan dikembalikan lagi dalam bentuk kode HTML untuk ditampilkan di browser. Ada 4 jenis tag yang bisa digunakan untuk memasukkan kode PHP.

Jenis Tag Tag Pembuka Tag Penutup Tag Standar <? php ?>

Tag Pendek <? ?>

Tag ASP <% %>

Tag Script <script language =

“php”>

</script>

Contoh Script PHP

(8)

PHP Case Sensitivity

• In PHP, all user-defined functions, classes, and keywords (e.g. if, else, while, echo, etc.) are NOT case-sensitive.

• In the example below, all three echo statements below are legal (and equal):

• <?php

ECHO "Hello World!<br>";

echo "Hello World!<br>";

EcHo "Hello World!<br>";

?>

• However; in PHP, all variables are case- sensitive.

• In the example below, only the first statement will display the value of the $color variable (this is because $color, $COLOR, and $coLOR are treated as three different variables):

• <?php

$color="red";

echo "My car is " . $color . "<br>";

echo "My house is " . $COLOR . "<br>";

echo "My boat is " . $coLOR . "<br>";

?>

(9)

PHP echo and print Statements

• There are some differences between echo and print:

1. echo - can output one or more strings 2. print - can only output one string, and

returns always 1

Tip: echo is marginally faster compared to print as echo does not return any value.

The PHP echo Statement

• echo is a language construct, and can be used with or without parentheses: echo or echo().

• <?php

echo "<h2>PHP is fun!</h2>";

echo "Hello world!<br>";

echo "I'm about to learn PHP!<br>";

echo "This", " string", " was", " made", " with multiple parameters.";

?>

(10)

The PHP print Statement

• print is also a language construct, and can be used with or without parentheses: print or print().

• <?php

print "<h2>PHP is fun!</h2>";

print "Hello world!<br>";

print "I'm about to learn PHP!";

?>

Variabel

• Variabel ditandai dengan adanya tanda dolar ($) yang kemudian bisa diikuti dengan angka, huruf, dan underscore. Namun variable tidak bisa mengandung spasi. Berikut ini contoh pendefinisian variable. Untuk mendefinisikan variable, hanya perlu menuliskannya maka otomatis variable dikenali oleh PHP.

• Contoh : $nama, $no_telp, $_pekerjaan

(11)

Rules for PHP variables:

• A variable starts with the $ sign, followed by the name of the variable

• A variable name must start with a letter or the underscore character

• A variable name cannot start with a number

• A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )

• Variable names are case sensitive ($y and $Y are two different variables)

• Remember that PHP variables are case- sensitive.

• PHP has no command for declaring a variable.

• A variable is created the moment you first

assign a value to it:

(12)

Contoh05.php:

• Hasil :

(13)

• The scope of a variable is the part of the script where the variable can be referenced/used.

• PHP has three different variable scopes:

• local

• global

• static

PHP Variables Scope

Local and Global Scope

A variable declared outside a function has a GLOBAL SCOPE and can only be accessed outside a function.

A variable declared within a function has a LOCAL SCOPE and can only be accessed within that function.

• The following example tests variables with

local and global scope:

(14)

• <?php

$x=5; // global scope function myTest() {

$y=10; // local scope

echo "<p>Test variables inside the function:<p>";

echo "Variable x is: $x";

echo "<br>";

echo "Variable y is: $y";

}

myTest();

echo "<p>Test variables outside the function:<p>";

echo "Variable x is: $x";

echo "<br>";

echo "Variable y is: $y";

?>

• You can have local variables with the same

name in different functions, because local

variables are only recognized by the function

in which they are declared.

(15)

• <?php

$x=5;

$y=10;

function myTest() {

global $x,$y;

$y=$x+$y;

}

myTest();

echo $y; // outputs 15

?>

<?php

$x=5;

$y=10;

function myTest() {

$GLOBALS['y']=$GLOBALS['x']+$GLOBALS['y'];

} myTest();

echo $y; // outputs 15

?>

static

<?php

function myTest() {

static $x=0;

echo $x;

$x++;

} myTest();

echo "<br>";

myTest();

echo "<br>";

myTest();

?>

Normally, when a function is completed/executed, all of its variables are deleted. However, sometimes we want a local variable NOT to be deleted. We need it for a further job.

To do this, use the static keyword when you first declare the variable:

Then, each time the function is called, that variable will still have the information it contained from the last time the function was called.

Note: The variable is still local to the function.

(16)

Tipe Data

• Berbeda dengan bahasa pemrograman lain, variable di PHP lebih fleksibel. Kita tidak perlu mendefinisikan jenisnya ketika mendefinisikan pertama kali. Ada 6 variabel dasar yang dapat diakomodasi di PHP, seperti terlihat di tabel.

Tipe Contoh Penjelasan

Integer 134 Semua angka bukan pecahan

Double 5.1234 Nilai pecahan

String “asep” Kumpulan karakter

Boolean False Salah satu nilai True atau False

Object Sebuah instance dari class

Array Larik

Contoh06.php:

(17)

Konstanta

• Selain variable, sebuah program umumnya juga memungkinkan adanya konstanta.

Konstanta fungsinya sama seperti variable namun nilainya statis/konstan dan tidak bisa berubah. Cara untuk mendefinisikan konstanta adalah :

• Define (“NAMA_KONSTANTA”, nilai_konstanta);

• <?php

define("GREETING", "Welcome to BSI!");

echo GREETING;

?>

• <?php

define("GREETING", "Welcome to BSI!", true);

echo greeting;

?>

(18)

Komentar

• Program merupakan kegiatan menuliskan bahasa yang dipahami oleh mesin. Walaupun bahasa yang digunakan adalah bahasa tingkat tinggi, namun tent masih tidak semudah dipahami oleh bahasa biasa. Untuk itu kita bisa menggunakan komentar. Berikut ini contoh pembuatan komentar di php.

Comments are useful for:

• To let others understand what you are doing - Comments let other programmers understand what you were doing in each step (if you work in a group)

• To remind yourself what you did - Most programmers have experienced coming back to their own work a year or two later and having to re-figure out what they did. Comments can remind you of what you were thinking when you wrote the code

• PHP supports three ways of commenting:

(19)

//komentar satu baris

#ini juga komentar satu baris /*komentar

Banyak baris Kode di sini tidak

Dieksekus oleh parser */

• <!DOCTYPE html>

<html>

<body>

<?php

// This is a single line comment

# This is also a single line comment /*

This is a multiple lines comment block that spans over more than

one line

*/

?>

</body>

</html>

(20)

Contoh script konstanta & komentar

Contoh07.php

Deklarasi komentar Deklarasi konstanta

Hasil :

(21)

Latihan Variabel :

Referensi

Dokumen terkait

Adapun permasalahan yang dibatasi dalam perancangan proyek akhir ini, yaitu : 1. Hosting yang digunakan pada web adalah jasa hosting komersial. Web berbasis bahasa pemrograman