Professional Documents
Culture Documents
CHAPTER
ONE
Server
Side
Scripting
Basics
Introduction
to
server-‐side
scripting
2
Web
Application
¡ Architecture
:
client-‐server
architecture
¡ Client
:
web
browser
¡ primary
language
for
web
browsers
is
HTML
(how
the
web
page
is
presented
)
¡ And
including
more
plug-‐ins
like
RealPlayer,
Flash,
and
Shockwave.
¡ Work
with
XML
¡ Support
Java
script
Advanced
IP
Compiled
By:
Yonas
H.
(MSc.)
13
March
2020
4
Web
Application
¡ Server
:
¡ almost
all
of
the
work
of
Web
applications
takes
place
on
the
server.
¡ specific
application,
called
a
Web
server,
will
be
responsible
for
communicating
with
the
browser.(apache
or
Internet
information
server)
¡ A
relational
database
server
stores
whatever
information
the
application
requires.
Advanced
IP
Compiled
By:
Yonas
H.
(MSc.)
13
March
2020
5
Web
Application
¡ Finally,
you
need
a
language
to
broker
requests
between
the
Web
server
and
the
database
server;
¡ it
will
also
be
used
to
perform
programmatic
tasks
on
the
information
that
comes
to
and
from
the
Web
server.
¡ The
Web
server,
programming
language,
and
database
server
you
use
must
work
well
with
your
operating
system.
Advanced
IP
Compiled
By:
Yonas
H.
(MSc.)
13
March
2020
6
Web
Application
¡ Operating
System
¡ Manage
computer
resources
¡ Server
OS
or
OS
¡ Different
OS
(windows
,mac
,Linux
etc)
Middleware
Web
(PHP,ASP,JSP)
Data
Base
Server
(MySql
,Oracle)
(Apache,
IIS)
Web
Browser
(internet
explorer
,Netscape,Mozila
etc)
PHP Features
¡ Performance:
¡ Script
written
in
PHP
executes
much
faster
then
those
scripts
written
in
other
languages
such
as
JSP
&
ASP.
PHP
Features
¡ Platform
Independent:
¡
PHP
are
available
for
WINDOWS,
MAC,
LINUX
&
UNIX
operating
system.
A
PHP
application
developed
in
one
OS
can
be
easily
executed
in
other
OS
also.
¡ Compatibility:
¡
PHP
is
compatible
with
almost
all
local
servers
used
today
like
Apache,
IIS
etc.
¡ Embedded:
¡ PHP
code
can
be
easily
embedded
within
HTML
tags
and
script.
Advanced
IP
Compiled
By:
Yonas
H.
(MSc.)
13
March
2020
How
to
run
php
file
on
XAMPP
11
server?
XAMPP
start
Apache
installation
and
MYSQL
from
Install
Notepad++
XAMPP
the
file
in
XAMP
installation
directory
or
web
write
php
program
or
root
directory.
Default
XAMP
installation
directory
for
windows
is
C/xampp
code
on
text
editor.
Basic
Syntax
Tag
style
Starting
tag
Ending
tag
Standard
<?php
?>
Short
hand
<?
?>
Script
<script
language=“php”>
</script>
Basic
Syntax
¡ PHP
code
is
executed
on
the
server,
and
the
plain
HTML
result
is
sent
to
the
browser
¡ create
a
file
and
write
HTML
tags
+
PHP
code
and
save
this
file
with
.php
extension.
¡ Each
code
line
in
PHP
must
end
with
a
semicolon
(;)
¡ <?php
//your
code
here
?>
or
<!DOCTYPE>
<html>
<body>
<?php
echo
"<h2>Hello
World!</h2>”;
?>
</body>
</html>
Advanced
IP
Compiled
By:
Yonas
H.
(MSc.)
13
March
2020
15
¡ print();
PHP echo
PHP
Comments
¡ Purpose
:
¡ To
provide
information
/readable
code/
¡ To
hide
codes
There
are
two
ways:
¡ single
line
comments
¡ //
(C++
style
single
line
comment)
¡ #
(Unix
Shell
style
single
line
comment)
¡ Multiple
line
comments
¡ enclose
all
lines
within
/*
*/
Errors
¡ Notice:
¡ Message
for
What
you
should
do
and
what
you
should
not
do
Possible
causes
:
¡ accessing
a
variable
that
has
not
yet
been
defined.
<?php
$a="Defined
error";
echo
"Notice
error";
echo
$b;
?>
Errors
¡ Warning:
¡ Occurs
at
run
time
¡ Do
not
interrupt
code
execution
possible
Causes
:
¡ Calling
on
an
external
file
that
does
not
exist
in
the
directory
¡ Wrong
parameters
in
a
function
<?php
echo
"Warning
error"';
include
("external_file.php");
?>
Errors
¡ Fatal
Error:
¡ Occurs
at
run
time
¡ Program
execution
is
not
continued
it
terminates
¡ They
crash
your
program
(critical
errors)
Possible
causes
:
¡ An
undefined
function
or
class
in
the
script
is
the
main
reason
for
this
type
of
error.
Errors
¡ For
instance,
the
following
script
would
result
in
a
fatal
error:
<?php
function
sub()
{
$sub=6-‐1;
echo
"The
sub=
".$sub;
}
div();
?>
Advanced
IP
Compiled
By:
Yonas
H.
(MSc.)
13
March
2020
24
Errors
Parse
Error
(Syntax)
¡ Parse
errors
are
caused
by
misused
or
missing
symbols
in
a
syntax.
Errors
¡ For
example,
the
following
script
would
stop
execution
and
signal
a
parse
error:
<?php
echo
"Red";
echo
"Blue";
echo
"Green"
?>
Advanced
IP
Compiled
By:
Yonas
H.
(MSc.)
13
March
2020
29
Example
:
<?php
?>
PHP
Boolean
¡ A
Boolean
represents
two
possible
states:
TRUE
or
FALSE.
$x
=
true;
$y
=
false;
¡ Booleans
are
often
used
in
conditional
testing.
PHP
Array
¡ An
array
stores
multiple
values
in
one
single
variable.
¡ In
the
following
example
$cars
is
an
array.
¡ The
position
of
array
elements
starts
from
0
not
1
Example
<?php
$cars
=
array("Volvo","BMW","Toyota");
var_dump($cars);
echo
$cars[0];
?>
<?php
$x
=
"Hello
world!";
$x
=
null;
var_dump($x);
?>
PHP
Object
¡ An
object
is
a
data
type
which
stores
data
and
information
on
how
to
process
that
data.
Cont’d
Cont’d
<?php
class
Myclass
{
public
$font_size
=10;
}
$f
=
new
MyClass;
echo
$f-‐>font_size;
?>
Manipulate
Strings
<?php
$str1='Hello
text
multiple
line
text
within
single
quoted
string';
$str2='Using
double
"quote"
directly
inside
single
quoted
string';
$str3='Using
escape
sequences
\n
in
single
quoted
string';
echo
"$str1
<br/>
$str2
<br/>
$str3";
?>
¡
Output:
Hello
text
multiple
line
text
within
single
quoted
string
Using
double
"quote"
directly
inside
single
quoted
string
Using
escape
sequences
\n
in
single
quoted
string
$num1=10;
$str2='trying backslash n and backslash t inside single quoted string \n \t';
?>
Output:
Cont’d
¡
You
can't
use
double
quote
directly
inside
double
quoted
string.
<?php
$str1="Using
double
"quote"
directly
inside
double
quoted
string";
echo
$str1;
?>
Cont’d
¡
We
can
store
multiple
line
text,
special
characters
and
escape
sequences
in
a
double
quoted
PHP
string.
<?php
$str1="Hello
text
multiple
line
text
within
double
quoted
string”;
$str2="Using
double
\"quote
\"
with
backslash
inside
double
quoted
string";
$str3="Using
escape
sequences
\n
in
double
quoted
string";
echo
"$str1
<br/>
$str2
<br/>
$str3";
?>
¡ Output:
Hello
text
multiple
line
text
within
double
quoted
string
Using
double
"quote"
with
backslash
inside
double
quoted
string
Using
escape
sequences
in
double
quoted
string
Advanced
IP
Compiled
By:
Yonas
H.
(MSc.)
13
March
2020
45
In
double
quoted
strings,
variable
will
be
interpreted
<?php
$num1=10;
echo
"Number
is:
$num1";
?>
¡
Output:
Number
is:
10
¡ Example
<?php
$str=”Hello
STUDENT";
$str=strtolower($str);
echo
$str;
?>
¡ Syntax
strtoupper
(
string
$string
)
¡ Example
<?php
$str="Hello
STUDENT";
$str=strtoupper($str);
echo
$str;
?>
¡ Syntax
ucfirst
(
string
$str
)
Output
:
Hello
STUDENT
¡ Example
<?php
$str=“hello
STUDENT";
$str=ucfirst($str);
echo
$str;
?>
Advanced
IP
Compiled
By:
Yonas
H.
(MSc.)
13
March
2020
50
¡ Syntax
lcfirst
(
string
$str
)
Output:
hello
Student
¡ Example
<?php
$str=“Hello
Student";
$str=lcfirst
($str);
echo
$str;
?>
Advanced
IP
Compiled
By:
Yonas
H.
(MSc.)
13
March
2020
51
¡ Syntax
¡ string
ucwords
(
string
$str
)
¡
Example
Output:
Hello
Student
<?php
$str=”hello
student";
$str=ucwords($str);
echo
$str;
?>
Advanced
IP
Compiled
By:
Yonas
H.
(MSc.)
13
March
2020
52
¡ Syntax
strrev
(
string
$string
)
¡ Example
<?php
Output:
tneduts
olleh
$str=”hello
student";
$str=strrev($str);
echo
$str;
?>
Advanced
IP
Compiled
By:
Yonas
H.
(MSc.)
13
March
2020
53
¡ Syntax
:
strlen(
string
$string
)
¡ Example
:
<?php
Output:
13
$str=”hello
student";
$str=strlen($str);
echo
$str;
?>
¡ output : 6
Example
<?php
echo
str_replace("world”,”Dave”,"Hello
world!");
?>
Output:
Hello
Dave!
Constants
¡ PHP
constants
are
name
or
identifier
that
can't
be
changed
during
the
execution
of
the
script.
Syntax
define(name,
value,
case-‐insensitive)
Parameters:
¡ Name:
Specifies
the
name
of
the
constant
¡ Value:
Specifies
the
value
of
the
constant
¡ Case-‐insensitive:
Specifies
whether
the
constant
name
should
be
case-‐
insensitive.
Default
is
false
Example:
¡ The
example
below
creates
a
constant
with
a
case-‐sensitive
name:
<?php
define("GREETING",
"Welcome
to
W3Schools.com!");
echo
GREETING;
?>
Example
<?php
const
MESSAGE="Hello
const
by
PHP";
echo
MESSAGE;
?>
Array
Indexed
Arrays
¡ An
indexed
or
numeric
array
stores
each
array
element
with
a
numeric
index.
¡ Note:
¡ In
an
indexed
or
numeric
array,
the
indexes
are
automatically
assigned
and
start
with
0,
and
the
values
can
be
any
data
type.
Indexed
array
<?php
$colors
=
array("Red”,"Green”,"Blue");
$colors[0]
=
"Red";
$colors[1]
=
"Green";
$colors[2]
=
"Blue";
?>
Associative
Arrays
¡ In
an
associative
array,
the
keys
assigned
to
values
can
be
arbitrary
and
user
defined
strings.
<?php
Multidimensional
Arrays
<?php
$marks
=
array(
”mrx"
=>
array
(
"php"
=>
35,"maths"
=>
30,
”java"
=>
39
),
"abebe" => array ("php" => 30,"maths" => 32, ”java" => 29 ),
”mry" => array ( "php" => 31,"maths" => 22, ”java" => 39 ) );
?>
Advanced
IP
Compiled
By:
Yonas
H.
(MSc.)
13
March
2020
70
Viewing
Array
Structure
and
Values
<?php
//
Define
array
$cities
=
array(“Addis",
“DB",
“Markos");
//
Display
the
cities
array
var_dump($cities);
?>
<?php
//
Define
array
$cities
=
array(“Addis",
“DB",
“Markos");
//
Display
the
cities
array
print_r($cities);
?>
Advanced
IP
Compiled
By:
Yonas
H.
(MSc.)
13
March
2020
71
// Sorting array by key and print in Ascending Order By Key
ksort($age);
print_r($age);
?>
Operators
PHP
Operators
can
be
categorized
in
following
forms:
¡ Arithmetic
Operators
Control
statements
¡ Conditional
statements
¡ If
¡ If..else
¡ If..elseif..else
¡ Switch
¡ Loops
¡ While
¡ Do..while
¡ For
Conditional
Statements
¡ Conditional
statements
are
used
to
perform
different
actions
based
on
different
conditions.
¡ switch
statement
-‐
use
this
statement
to
select
one
of
many
blocks
of
code
to
be
executed
Syntax
¡ <?php
$d=date("D");
if
($d=="Fri")
echo
"Have
a
nice
weekend!";
?>
if...else
:Syntax
¡ The
if...else
Statement
¡ <?php
$d=date("D");
if
($d=="Fri")
{
echo
"Hello!<br
/>";
echo
"Have
a
nice
weekend!";
echo
"See
you
on
Monday!";
}
?>
¡ Syntax
if
(condition)
code
to
be
executed
if
condition
is
true;
elseif
(condition)
code
to
be
executed
if
condition
is
true;
else
code
to
be
executed
if
condition
is
false;
Example:
if..elseif..else
¡ The
following
example
will
output
"Have
a
nice
weekend!"
if
the
current
day
is
Friday,
and
"Have
a
nice
Sunday!"
if
the
current
day
is
Sunday.
Otherwise
it
will
output
"Have
a
nice
day!":
¡ <?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!";
?>
¡ Syntax
switch
(n)
{
case
label1:
code
to
be
executed
if
n=label1;
break;
case
label2:
code
to
be
executed
if
n=label2;
……………….
break;
default:
code
to
be
executed
if
n
is
different
from
both
label1
and
label2;
}
Advanced
IP
Compiled
By:
Yonas
H.
(MSc.)
13
March
2020
91
¡ foreach
-‐
loops
through
a
block
of
code
for
each
element
in
an
array
¡ Syntax
do
{
code
to
be
executed;
}
while
(condition);
¡ Example
¡ The
example
below
defines
a
loop
that
starts
with
i=1.
It
will
then
increment
i
with
1,
and
write
some
output.
Then
the
condition
is
checked,
and
the
loop
will
continue
to
run
as
long
as
i
is
less
than,
or
equal
to
5:
Example:
do…while
¡ <?php
$i=1;
do
{
$i++;
echo
"The
number
is
"
.
$i
.
"<br
/>";
}
while
($i<=5);
?>
Output:
¡ The
number
is
2
The
number
is
3
The
number
is
4
The
number
is
5
The
number
is
6
¡ Parameters:
¡ init:
Mostly
used
to
set
a
counter
(but
can
be
any
code
to
be
executed
once
at
the
beginning
of
the
loop)
¡ condition:
Evaluated
for
each
loop
iteration.If
it
evaluates
to
TRUE,
the
loop
continues.
If
it
evaluates
to
FALSE,
the
loop
ends.
¡ increment:
Mostly
used
to
increment
a
counter
(but
can
be
any
code
to
be
executed
at
the
end
of
the
loop)
¡ Note:
Each
of
the
parameters
above
can
be
empty,
or
have
multiple
expressions
(separated
by
commas).
¡ Output:
The
number
is
1
The
number
is
2
The
number
is
3
The
number
is
4
The
number
is
5
Syntax
foreach
($array
as
$value)
{
code
to
be
executed;
}
¡ For
every
loop
iteration,
the
value
of
the
current
array
element
is
assigned
to
$value
(and
the
array
pointer
is
moved
by
one)
-‐
so
on
the
next
loop
iteration,
you'll
be
looking
at
the
next
array
value.
Example
¡ The
following
example
demonstrates
a
loop
that
will
print
the
values
of
the
given
array:
Functions
¡ A
function
is
a
named
block
of
code
that
is
designed
to
perform
a
specific
task.
¡ Syntax
function
function_Name()
{
code
to
be
executed;
}
function_name(parameter1, parameter2,...);
PHP
Functions
¡ The
real
power
of
PHP
comes
from
its
functions.
¡ when
the
page
loads,
you
can
put
it
into
a
function.
¡ Output:
My
name
is
mrx
echo 'inside function x = ' . $x .'<br />'; echo 'inside function x = ' . $x .'<br />';
} }
$x = 10; $x = 10;
increase($x); increase($x);
echo 'outside function x = ' . $x .'<br />'; echo 'outside function x = ' . $x .'<br />';