Sei sulla pagina 1di 31

PHP

UNIVERSITAS MUHAMMADIYAH SURAKARTA


INTRODUCTION

 PHP
 PHP: Hypertext Preprocessor
 Originally called “Personal Home Page Tools”
 Popular server-side scripting technology
 Open-source
 Anyone may view, modify and redistribute source code
 Supported freely by community
 Platform independent
Why is PHP used?

1. Easy to Use
Code is embedded into HTML. The PHP code is enclosed in special
start and end tags that allow you to jump into and out of "PHP mode".

<html>
<head>
<title>Example</title>
</head>
<body>

<?php
echo "Hi, I'm a PHP script!";
?>
</body>
</html>
Why is PHP used?

2. Cross Platform
Runs on almost any Web server on several operating systems.
One of the strongest features is the wide range of supported databases

Web Servers: Apache, Microsoft IIS, Caudium, Netscape


Enterprise Server

Operating Systems: UNIX (HP-UX,OpenBSD,Solaris,Linux),


Mac OSX, Windows NT/98/2000/XP/2003

Supported Databases: 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
Why is PHP used?

3. Cost Benefits
PHP is free. Open source code means that the entire PHP community
will contribute towards bug fixes. There are several add-on
technologies (libraries) for PHP that are also free.

PHP
Software Free

Platform Free (Linux)

Development Tools Free


PHP Coder, jEdit
Preparation

 Laptop / PC
 Web Server
 Web Browser
 Text Editor
Installation
Installation
Installation
PHP

 <?php ............... ?>


 <? .......................?>
7

PHP

How to escape from HTML and enter PHP mode


• PHP parses a file by looking for one of the special tags that
tells it to start interpreting the text as PHP code. The parser then
executes all of the code it finds until it runs into a PHP closing tag
HTML PHP CODE HTML

<?php echo “Hello World”;


?>

<?php ?> Preferred method as it allows the use of


PHP with XHTML
<? ?> Not recommended. Easier to type, but has
to be enabled and may conflict with XML
<script language="php"> ?> Always available, best if used when
FrontPage is the HTML editor

<% %> Not recommended. ASP tags support was


added in 3.0.4
8

PHP

• Basic application
– Variables preceded by $ symbol
• Case-sensitive
– End statements with semicolon
– Comments
• # for single line
• // for single line
• /* */ for multiline
– Filenames end with .php by convention
Type Something

 echo “your_word”;

Example :
<p>
This is from web browser!!
<?php
echo “This is from Server …”;
?>
</p>
11

VARIABLE

• Variables
– Can have different types at different times
– Variable names inside strings replaced by their value
– Type conversions
• settype function
• Type casting
– Concatenation operator
• . (period)
• Combine strings
12

Data type
Data type Description
int, integer Whole numbers (i.e., numbers without a decimal point).
float, double Real numbers (i.e., numbers containing a decimal point).

string Text enclosed in either single ('') or double ("")


quotes.
bool, Boolean True or false.

array Group of elements of the same type.


object Group of associated data and methods.
Resource An external data source.
NULL No value.
Varriable

• $angka = 0;
• $nama = ‘sule’;
• $tgl=date ("d M y");
17

Arithmetic Operator
• Arithmetic operators
– Assignment operators
• Syntactical shortcuts
• Before being assigned values, variables have
value undef
• Constants
– Named values
– define function
• + - * / % . ++ --
• Operator bisa melakukan konversi otomatis tipe
contoh :
$a = "12" + 5; // maka $a = 17
Array
• Arrays
• Group of related data
• Elements
• Name plus braces and index

Indices start at zero
• count function
• array function
• $variable=array(nilai1,nilai2,nilai3);
• $variable[]=nilai;

• Accessing
• $variable[indeks]
• Indeks start from 0
Operator

 $a + $b –($c * $a) / ($b % $c)


 $a && $b, $a and $b
 $a || $b , $a or $b
 $a xor $b
 !$a
 $a <> $b , atau $a != $b
 $a <= $b, atau $a >= $b
 ++$a, atau --$a, $a += $b;
 $a++
 $a = “Selamat”; $a .= “Pagi”;
Array

• Untuk menambahkan menggunakan kurung kotak tanpa isi


index ( [] )
• Tipe element tidak perlu di sebutkan, bisa berisi tipe macam-
macam
Loop for ( Seperti di C )
Statement if / else

Elseif adalah keyword yang sering dipakai, walaupun else if


juga support
Loop while

Keyword break dan continue bisa digunakan dan mirip seperti di


Java dan C
Loop foreach
IF...Else

<?php
If( ($a == $b ) && ($a > $c) ){
echo “SAMA”;
}elseif($a > $b) {
echo “LEBIH BESAR”;
}else {
echo “LEBIH KECIL”;
}
?>
While....Do...

<?php
$i=0;
while($i<10) {
echo ++$i;
}
do {
echo --$i;
}while ($i>0);
?>
Switch

<?php
$i = 0;
switch ($i) {
case 0:
echo "i equals 0“; break;
case 1:
echo "i equals 1“; break;
case 2:
echo "i equals 2“; break;
}
?>
Server Web

FORM MANIPULATION

daftar.php

hasil.php
Basic Concept [POST]

<form
action=“reg_form.php”
method=“post”
name=“register”>
Username : <input type=“text” name=“username”
/>
<input type=“submit” name=“submit”
value=“submit” />
</form>
Basic Concept [GET]

<form
action=“reg_form.php”
method=“get”
name=“register”>
Username : <input type=“text” name=“username”
/>
<input type=“submit” name=“submit”
value=“submit” />
</form>
Catch Variable From Form

 Method POST
 $_POST[‘variable_name’]

 Method GET
 $_GET[‘variable_name’]

Potrebbero piacerti anche