You are on page 1of 49

What is PHP?

PHP stands for PHP: Hypertext Preprocessor


PHP is a server-side scripting language, like

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

All codes can be seen by


visitors

Visitor can not see the codes

is processing on the visitors


computer
Can not access to the file on
the web server
Can not access to the
database

is processing on the web


server
Able to access the file on the
web server
Able to access the database

Client Side
Execute Code and
Show to the web
browser

request

server
answer

client

Send HTML +
Code

Ex: Client Side


<html>
<head>
<title>My Fist Web</title>
</head>
<body>
Hello how are u?<br />
<script>
document.writeln(great);
</script>
</body>
</html>

server

<html>
<head>
<title>My Fist Web</title>
</head>
<body>
Hello how are u?<br />
<script>
document.writeln(great);
</script>
</body>
</html>

client

Client Side Result

Server Side

Execute Code and


Send to the client

request
server
answer
Send HTML

client

Ex: Server Side


<html>
<head>
<title>My Fist Web</title>
</head>
<body>
Hello how are u?<br />
<?php
echo great;
?>
</body>
</html>

server

<html>
<head>
<title>My Fist Web</title>
</head>
<body>
Hello how are u?<br />
great
</body>
</html>

client

Server Side Result

Basic PHP Syntax


Starts with <?php and ends with ?>
standard
Starts with <? and end with ?> shorthand
For maximum compatibility, we recommend
that you use Standard (<?php) rather than the
shorthand form.

Display Text in PHP


There are two basic statements to output text
with PHP: echo and print. In the example above
we have used the echo statement to output the
text "Hello World".
<html>
<body>
<?php
echo "Hello World";
?>
</body>
</html>

Note: The file must have a .php


extension. If the file has a .html
extension, the PHP code will not be
executed.

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;
?>

Naming Rules for Variables


A variable name must start with a letter or an
underscore "_"
A variable name can only contain alphanumeric characters and underscores (a-z, A-Z,
0-9, and _ )
a.$101_day=seeking love; c. $_101_day= love story;
b.$myname=love;
d. $_name=amor;

Which is incorrect variable in PHP?

PHP $_GET Variable


The predefined $_GET variable is used to collect values in a
form with method="get
Information sent from a form with the GET method is
visible to everyone (it will be displayed in the browser's
address bar) and has limits on the amount of information to
send.
Note:
This method should not be used when sending passwords or
other sensitive information!
The get method is not suitable for very large variable values. It
should not be used with values > 2000 characters.

PHP $_GET Variable


Example:
Form:
<form action="welcome.php" method="get">
Name: <input type="text" name="fname" />
Age: <input type="text" name="age" />
<input type="submit" />
</form>
Address Bar:
welcome.php?fname=Peter&age=37

Welcome.php:
Welcome <?php echo $_GET["fname"]; ?>.<br />
You are <?php echo $_GET["age"]; ?> years old!

PHP $_POST Function


The predefined $_POST variable is used to collect
values from a form sent with method="post".
Information sent from a form with the POST
method is invisible to others and has no limits on
the amount of information to send.

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!

PHP $_REQUEST Variable


The predefined $_REQUEST variable contains the
contents of both $_GET, $_POST, and $_COOKIE.
The $_REQUEST variable can be used to collect
form data sent with both the GET and POST
methods.

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)

Legal access with post mehod

Denied access with get

PHP Operators

Arithmetic Operators
Assignment Operators
Comparison Operators
Logical Operators

Arithmetic Operators
Operator

Description

Addition

Subtraction

Multiplication

Division

Modulus (division remainder)

++

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

5==8 returns false

!=

is not equal

5!=8 returns true

<>

is not equal

5<>8 returns true

>

is greater than

5>8 returns false

<

is less than

5<8 returns true

>=

is greater than or equal to 5>=8 returns false

<=

is less than or equal to

5<=8 returns true

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>

Notice that there is no ..else.. in this syntax. The code is


executed only if the specified condition is true.

The if...else Statement


Use the if....else statement to execute some
code if a condition is true and another code if
a condition is false.
<html>
<body>
<?php
$d=date("D");
if ($d=="Fri")
echo "Have a nice weekend!";
else
echo "Have a nice day!";
?>
</body>
</html>

The if...elseif....else Statement


Use the if....elseif...else statement to select
one of several blocks of code to be executed.
<html>
<body>
<?php
$d=date("D");
if ($d=="Fri")
echo "Have a nice weekend!";
elseif ($d=="Sun")
echo "Have a nice Sunday!";
else
echo "Have a nice day!";
?>
</body>
</html>

PHP Switch Statement


Use the switch statement to select one of many
blocks of code to be executed.
<?php
switch ($x)
{
case 1:
echo "Number 1";
break;
case 2:
echo "Number 2";
break;
case 3:
echo "Number 3";
break;
default:
echo "No number between 1 and 3";
}
?>

PHP Functions
A function will be executed by a call to the
function.
You may call a function from anywhere within a
page.

Create a PHP Function


A function will be executed by a call to the
function.
Syntax
function functionName()
{
code to be executed;
}

PHP function guidelines:


Give the function a name that reflects what the
function does
The function name can start with a letter or
underscore (not a number)

Function : Example
function tulis($kalimat) {
echo $kalimat.<br>\n;
}
tulis(Hello.. How are you?);
tulis(Fine lah);

PHP Functions - Adding parameters


To add more functionality to a function, we can
add parameters. A parameter is just like a
variable.
Parameters are specified after the function name,
inside the parentheses ().

Ex:PHP Functions - Adding parameters


function pangkat($a,$b) {
if($b>0) {
$hasil=$a;
for($i=2;$i<=$b;$i++) {
$hasil=$hasil*$a;
}
return $hasil;
} else return 1;
}
$x=pangkat(2,0);
echo $x;

PHP Functions - Return values


To let a function return a value, use the return
statement.
Output:
Example

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