Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
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
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
Laptop / PC
Web Server
Web Browser
Text Editor
Installation
Installation
Installation
PHP
PHP
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).
• $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
<?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’]