Professional Documents
Culture Documents
1chapter One
1chapter One
ONE
Server Side Scripting Basics
Introduction to server-side scripting
2
Contents To be Covered
! Server-side scripting languages
! Write Comments
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.) 5 April 2019
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.) 5 April 2019
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.) 5 April 2019
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)
What is PHP ?
! PHP stands for PHP: Hypertext Preprocessor
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.) 5 April 2019
How to run php file on XAMPP 11
server?
XAMPP start Apache
installation and MYSQL from Install Notepad++
XAMPP
Basic Syntax
Tag style Starting tag Ending tag
Standard <?php ?>
Short hand <? ?>
ASP <% %>
Script <script language=“php”> </script>
Errors
! Notice
! Warning
! Error
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.) 5 April 2019
16
! 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 /* */
Advanced IP Compiled By: Yonas H. (MSc.) 5 April 2019
24
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.
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;
?>
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;
?>
! Output: error
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";
?>
! 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.) 5 April 2019
43
! Syntax
lcfirst ( string $str )
Output: hello
! Example Student
<?php
$str=“Hello Student";
$str=lcfirst ($str);
echo $str; ?>
Advanced IP Compiled By: Yonas H. (MSc.) 5 April 2019
44
! Syntax
! string ucwords ( string $str )
! Example
Output: Hello
<?php Student
$str=”hello student";
$str=ucwords($str);
echo $str;
?>
Advanced IP Compiled By: Yonas H. (MSc.) 5 April 2019
45
! Syntax
strrev ( string $string )
! Example
Output: tneduts
<?php
olleh
$str=”hello student";
$str=strrev($str);
echo $str;
?>
Advanced IP Compiled By: Yonas H. (MSc.) 5 April 2019
46
! Syntax :
strlen( string $string )
! Example :
<?php Output: 13
$str=”hello student";
$str=strlen($str);
echo $str;
?>
Example
<?php
echo str_replace("world", ”Dave", "Hello
world!");
?>
// outputs 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
Example:
<?php
$marks = array( "mohammad" => array ( "physics" => 35,"maths" => 30, "chemistry" => 39 ),
"abebe" => array ("physics" => 30,"maths" => 32, "chemistry" => 29 ),
"zara" => array ( "physics" => 31, "maths" => 22, "chemistry" => 39 ) );
echo "Marks for mohammad in physics : " ; echo $marks['mohammad']['physics'] . "<br />";
echo "Marks for zara in chemistry : " ;echo $marks['zara']['chemistry'] . "<br />";
?>
// Define array
ksort($age);
print_r($age);
?>
Operators
PHP Operators can be categorized in following
forms:
! Arithmetic Operators
! Comparison Operators
! Bitwise Operators
! Logical Operators
! String Operators
! Assignment Operators
! Incrementing/Decrementing Operators
! Array 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.
The if Statement
! Use the if statement to execute some code
only if a specified condition is true.
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.) 5 April 2019
83
! 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)
! 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
Example: foreach
<?php
$data=array("abebe"=>2,"belen"=>1,”feven"=>5);
foreach($data as $name=>$id)
{
echo $data[$id];
}
?>
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.
Example
A simple function that writes name when it is
called:
! <?php
function writeName()
{
echo “mrx";
}
echo "My name is ";
writeName();
?>
! Output:
My name is mrx
Function parameters
! Information can be passed to functions
through arguments. An argument is just like a
variable.
Passing parameters by
value
Passing parameters by
reference
<?php <?php
$x = $x + 1; $x = $x + 1;
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 />';