Beruflich Dokumente
Kultur Dokumente
ASP
PHP scripts are executed on the server
PHP supports many databases (MySQL,
Informix, Oracle, Sybase, Solid, PostgreSQL,
Generic ODBC, etc.)
PHP is an open source software
PHP is free to download and use
Where to Start?
To get access to a web server with PHP support,
you can:
Install Apache (or IIS*) on your own server,
install PHP, and MySQL
Or find a web hosting plan with PHP and
MySQL support
*IIS = Internet Information Services
Client/Server
Client: the device that access to server using any
software (Web browser, etc)
Server: the computer that provide any services such
as:
Web server (HTTP),
Simple Mail Transfer Protocol (SMTP),
Domain Name Serving (DNS),
File Transfer Protocol (FTP),
Firewall (filters data to & from Internet),
Network News Transfer Protocol (NNTP), etc
Client/Server Side
Client Side
Server Side
Client Side
Execute Code and
Show to the web
browser
request
server
answer
client
Send HTML +
Code
server
<html>
<head>
<title>My Fist Web</title>
</head>
<body>
Hello how are u?<br />
<script>
document.writeln(great);
</script>
</body>
</html>
client
Server Side
request
server
answer
Send HTML
client
server
<html>
<head>
<title>My Fist Web</title>
</head>
<body>
Hello how are u?<br />
great
</body>
</html>
client
Comments in PHP
In PHP, we use // to make a
single-line comment or /* and
*/ to make a large comment
block.
<html>
<body>
<?php
//This is a comment
/*
This is
a comment
block
*/
?>
</body>
</html>
Variables in PHP
Variables are used for storing values, like text
strings, numbers or arrays.
All variables in PHP start with a $ sign symbol.
The correct way of declaring a variable in PHP:
Syntax
$var_name = value;
<?php
$txt="Hello World!";
$x=16;
?>
Welcome.php:
Welcome <?php echo $_GET["fname"]; ?>.<br />
You are <?php echo $_GET["age"]; ?> years old!
PHP $_POSTVariable
Example:
Form:
<form action="welcome.php" method="post">
Name: <input type="text" name="fname" />
Age: <input type="text" name="age" />
<input type="submit" />
</form>
Address Bar:
welcome.php
Welcome.php:
Welcome <?php echo $_POST["fname"]; ?>.<br />
You are <?php echo $_POST["age"]; ?> years old!
Form.html
<form method="POST"
name="myform"
action="form.php">
Nama = <input
type="text" name="txtnama"
size="20"><br>
<input type="button"
value="Simpan" name="simpan"
onClick="huruf(this.form)"><inp
ut type="reset" value="Reset"
name="B2">
</form>
<script>
function check(form) {
if(form.txtnama.value=="") {
alert("Nama belum diisi!");
form.txtnama.focus();
return false;
} else
return true;
}
function huruf(form) {
if (check(form)) {
if(!isNaN(form.txtnama.value)) {
alert("Nama harus
huruf!");
form.txtnama.focus();
return false;
} else
form.submit();
return true;
}
}
document.myform.txtnama.focus();
</script>
form.php
<body>
<?php
echo "Nama Anda ".$_POST['txtnama'];
?>
</body>
Result
Legal access
Direct access
form.php (modification 1)
<body>
<?php
if(isset($_POST['txtnama'])) {
echo "Nama Anda : ".$_POST['txtnama'];
}
?>
</body>
Result (modification 1)
Legal access
Direct access
form.php (modification 2)
<body>
<?php
if(isset($_POST['txtnama'])) {
echo "Nama Anda : ".$_POST['txtnama'];
} else {
echo "Illegal Access is not allowed!";
}
?>
</body>
Result (modification 2)
Legal access
Direct access
form.php (modification 3)
<body>
<?php
if(isset($_POST['txtnama'])) {
echo "Nama Anda : ".$_POST['txtnama'];
} elseif(isset($_GET['txtnama'])) {
echo $_GET['txtnama'].", this method is not
allowed!";
} else {
echo "Illegal Access is not allowed!";}
?>
</body>
Result (modification 3)
PHP Operators
Arithmetic Operators
Assignment Operators
Comparison Operators
Logical Operators
Arithmetic Operators
Operator
Description
Addition
Subtraction
Multiplication
Division
++
Increment
--
Decrement
Example
x=2
x+2
x=2
5-x
x=4
x*5
15/5
5/2
5%2
10%8
10%2
x=5
x++
x=5
x--
Result
4
3
20
3
2.5
1
2
0
x=6
x=4
Assignment Operators
Operator
Example
Is The Same As
x=y
x=y
+=
x+=y
x=x+y
-=
x-=y
x=x-y
*=
x*=y
x=x*y
/=
x/=y
x=x/y
.=
x.=y
x=x.y
%=
x%=y
x=x%y
Comparison Operators
Operator
Description
Example
==
is equal to
!=
is not equal
<>
is not equal
>
is greater than
<
is less than
>=
<=
Logical Operators
Operator
Description
Example
&&
and
x=6
y=3(x < 10 && y > 1) returns true
||
or
x=6
y=3(x==5 || y==5) returns false
not
x=6
y=3!(x==y) returns true
Conditional Statements
In PHP we have the following conditional statements:
if statement - use this statement to execute some code
only if a specified condition is true
if...else statement - use this statement to execute
some code if a condition is true and another code if the
condition is false
if...elseif....else statement - use this statement to
select one of several blocks of code to be executed
switch statement - use this statement to select one of
many blocks of code to be executed.
The if Statement
Use the if statement to execute some code only if a
specified condition is true.
<html>
<body>
<?php
$d=date("D");
if ($d=="Fri") echo "Have a nice
weekend!";
?>
</body>
</html>
PHP Functions
A function will be executed by a call to the
function.
You may call a function from anywhere within a
page.
Function : Example
function tulis($kalimat) {
echo $kalimat.<br>\n;
}
tulis(Hello.. How are you?);
tulis(Fine lah);
1 + 16 = 17
<?php
function add($x,$y)
{
$total=$x+$y;
return $total;
}
echo "1 + 16 = " . add(1,16);
?>
Assignment
Create form to input the code of progdi
After it was submitted, it will show the name
of progdi (use switch case)
Must be use php fuction and give validation
Note:
A11 : Teknik Informatika S1
A12 : Sistem Informasi S1
A14 : DKV S1
A21 : Management Informatika D3
A22 : Tenik Informatika D3
Assignment(2)
Create form to input the student ID (Ex:
A11.2012.0001)
After it was submitted, it will show the name
EXAMPLE PHP EXPLODE
of progdi
<?php
$kalimat = "Internet
Use PHP Explode
Programming";
$kalimat."<br/><br/>";
Use different function echo
$pecah=explode(" ",$kalimat);
$jml=count($pecah);
Give validation
for($i=0;$i<$jml;$i++) {
echo $pecah[$i]."<br>\n";
}
?>
THANKS
Email: moses.dinus@gmail.com