Web Application Development Note 2
Web Application Development Note 2
to web
application
1.0 Introduction
Web application or services are applicable in variety
of ways as it serves business logic components which can be connected across the
exchange data to do meaningful work. In this, the components can be internal or
external to an organization, which are used to communicate using Internet-based
protocols which can be Hyper Text Transfer Protocol or else. These services run
on servers and carried out large variety of data which users want to access quickly
and easily.
As seen, the famous programming model where individual web services are
joined or mixed in order to frame functional results in service-oriented
architecture. It is an architectural model, where service consumer put on the
requests to service provider using standard connection. In this, request and
successive response are explained in a way which is understandable to consumer
and provider.
Many web services make use of Extensible Markup Language (XML) in
order to define format of request and response messages. This feature is tagged
structure which shows required flexibility for changing of information that exists
among disparate components.
Simple Object Access Protocol shows standard for persistence and sending
of web service messages which is part of XML messaging specification that
shows message format with certain rules applied in exchanging of data in required
sequence that appears among structured data types and arrays.
2
Fig 1.1 describes the basic architecture of web components and services which are commonly
applied to host websites and build web applications.
3
Here the browser will read the response from server and provide the page. If
Introduction
the page has fixed content which can be images, then the client will request for it. to web
In HTTP/1.0, requests will be on different connections where as HTTP/1.1 and application
later carries same connection.
From the figure we see that initially the user will access the website using
the browser where user types the URL of website in browser and click on go
option.
Then the page request on browser will reach to Web Server.
After this, the web server will collect requested page from its document
root.
When a static element such as HTML, CSS, image or Script file appears
then web server will send it directly to browser and in such case the browser will
leave it to user on screen.
4
Introduction When the PHP file appears then the web browser will send content of file to
to PHP
PHP Interpreter which further will interprets such code and start execute.
After executing the code, the PHP interpreter will generate an output and
sends it to web server. Further, the web server will send the required content to
browser which provides it to users' screen.
We see all static components which can be HTML, CSS files, Image Files,
Scripts etc will not require interpreter. In such case the web browser will provide
them and shows on screen.
Only if requested page is in PHP, then the web server will send it to PHP
interpreter in order to do translation and execution. For such reason, those listed
static components will remain inside the server and are consider as part of User
Interface which provides at user's browser which is a Client side.
Due to this, the PHP files are referred to as Server side components since
they carry out dependencies on other with this, we see that:
1. PHP files are kept on Server - Server Side
2. PHP Interpreter will interprets PHP language and perform calculation of
instructions as per required code without any compilation.
a. Client Side
b. Server side
c. Both of these
d. None of these
b. .ph
c. .xml
d. .php
5
1.2 Static and dynamic web application Introduction
to web
Web server process out every piece of information to users so as to compose application
the web pages. It is seen that web page structure can of many pieces, which result
in sum that affects series of performance metrics which can be in terms of
bandwidth consumption; user perceived latency without relevance to workload
required so as to arrange all information of particular web pages together. It
describes whether a web page needs large data processing or queries to permanent
store so as to put together.
A web application's content can be of two types:
Dynamic
Static
Dynamic content is what needs some type of processing that can be applied
where as static content is that that never changes. When a request is made for
either type of content, in such case the web server will do execution which is
required to send out dynamic content present in the file. This process is shown in
figure 1.4.
6
Introduction
to PHP
It is noticed that the art of dispatching dynamic or static content will take
place when a particular load on web server using dynamic content places heavier
load as compared to static content. It describes inefficient allocation of resources
on web server that is required to send dynamic and static content. So it is analysed
that decoupling of static content from application is considered as steps in order to
upgrade the performance on web servers. The problem with static content
separation is not clear of having full static web page.
When a user's browser receives the original application page of the elements
they are declared in the content where browser start requesting for referenced
content. Figure 1.5 shows such multiple request process.
7
Introduction
to web
application
8
Introduction
to PHP
Fig 1.6 Different web servers for dynamic and static content.
a. Content that cannot change without a developer editing its source code,
b. Different content from the same source code.
c. Both of these
d. None of these
9
1.3 Installing WAMP Introduction
to web
WAMP allows you to move between different versions of PHP if it is application
installed properly. There are certain steps to be followed while installing Wamp
Server.
To start installing, you have to open the folder where you have saved your
file and double click on installer file. After double clicking, a security warning
window will open which will ask you whether to run this file or not. If you select
on Run you will find that the installation process started and you will see
Welcome To WampServer Setup Wizard screen on which you have choose next to
forward with the installation.
After this, the License Agreement screen will appear which will ask you to
accept all agreements by selecting the radio button. Selecting the radio button and
clicking on next will allow moving to next screen.
10
Introduction
to PHP
This screen will ask for the location where you have to select Destination
Location. Unless you would like to install WampServer on another drive, you
should not need to change anything. Click Next to continue.
11
After this, the next screen shows Select Additional Tasks screen where you
Introduction
need to select whether you would like Quick Launch icon to be added to taskbar to web
or Desktop icon after the installation. After making the selections, click Next to application
continue.
Clicking on Next will take you to Ready To Install screen where you can
review your setup choices and change any by clicking Back to required screen.
Once you have reviewed your choices, click Install to continue.
12
Introduction
to PHP
After this, the WampServer will start extracting files to required selected location.
13
After extraction of files, you will be asked to select default browser. Wamp Introduction
Server defaults to Internet Explorer upon opening the local file browser window. to web
If your default browser is not an Internet Explorer, then find required .exe file: application
Now select default browser’s .exe file and click Open to continue.
On clicking you will find that a Windows Security Alert window will appear
prompting that Windows Firewall has blocked some features of program. Here
you need to check whether you want to allow Apache HTTP Server to
communicate on private or public network and if this and then click Allow
Access. You will see that the setup screen will appear showing status of
installation process.
14
Introduction
to PHP
You will that the progress bar will work and once it is completely green,
then PHP Mail Parameters screen will appear. Here you have to leave SMTP
server local host and change email address to one of your selection. Click Next to
continue.
After this you will find that the Installation complete screen will appear
where you have to select Launch WampServer Now and click on Finish to
complete installation.
15
Introduction
to web
application
Finally you will find WampServer icon in systray on right side of taskbar.
d. None of these
2. Which of the following is needed to be installed on computer as to run
PHPscript?
i) Adobe Dreamweaver
ii) PHP
iii) Apache
iv) IIS
a. All of these
b. Only ii)
c. ii) and iii)
16
Introduction
to PHP
1.4 Let Us Sum Up
In this unit we have learnt that Web application are applicable in variety of
ways as it serves business logic components which can be connected across the
exchange data to do meaningful work. Simple Object Access Protocol shows
standard for persistence and sending of web service messages which is part of
XML messaging specification that shows message format with certain rules
applied in exchanging of data in required sequence that appears among structured
data types and arrays.
Answers: (1 –b)
1.6 Glossary
1. Web application - Services used for business logic components connected
across with exchange data to do work.
2. Simple Object Access Protocol - Standard for persistence and sending of web
service messages.
17
3. Server side applications - Applications performed at server end on static
Introduction
responded by user input. to web
4. Web server - Server running web applications data process information to application
1.7 Assignment
Design a process to install PHP web application.
1.8 Activities
Create an activity on WampServer.
18
2.0 Introduction PHP Basics
Default Syntax
It is a default delimiter syntax which opens with <?php and results with ?> as:
<h3>Hi!</h3>
<?php
echo "<p>K. J. Sharma</p>";
?>
<p>Engineer</p>
Output:
Hi!
K. J.
Sharma
Engineer
Short-Tags
It is a shorter delimiter syntax where syntax gives up php location that is
needed in default syntax. For this you have to allow PHP’s short_open_tag
directive as shown:
19
Introduction <?
to PHP
print "He is an Author";
?>
Script
It is noted that many programmers has problems with PHP escape syntax
variants, so support for another mainstream delimiter variant, <script>, is offered:
<script language="php">
ASP Style
If you are having experience on ASP, the delimiting strategy/escape syntax
PHP supports it as shown:
<%
It is noted that ASP Style and Script delimiting variants are not commonly
used and should be avoided till you have reasons for doing.
<body>
<?php
</body> </html>
It is noted that any variables can be declared a prior code block that are
remembered for later blocks.
d. None of these
2. Choose the correct way of writing ASP tag.
a. <? echo”world” ?>
b. <% echo”world” %>
21
Introduction It is noted that programmers having experience in editing their work and can
to PHP
re-figure will introduce comment just to remind about thinking in order to avoid
decoding work.
You can add comments in PHP code by many ways. Initially you can use //
to comment a line as shown:
<?php echo "Hi"; //this is a comment echo " How are you"; ?>
If you have single line comment, so there is another option of introducing
comment by using a # sign as shown:
<?php echo "Hi"; #this is a comment echo " How are you"; ?>
If you have no multi line comment, then you can comment by using /* */ as
shown in example:
<?php echo "Hi"; /* Using this you can create big block of text that comments
as */ echo " How are you"; ?>
b. /* */
c. //
d. All of these
22
<?php On
executing
print("<p>Hello World</p>"); following
variation
?>
of above
<?php syntax
will
$author = "K. J. Sharma"; generate
output as:
print "<p>I like
$author.</p>";
?>
I like K. J. Sharma.
We see that official syntax calls for use of parentheses will enclose the
argument, and are not applied as print() is not technical function but language
construct. The print() statement’s will return false value as it return 1 immaterial
of outcome.
From the prototype, echo() is able to output many strings. Study the example
shown:
<?php
$he = "Rohit";
$she = "Vandana";
echo $he, " and ", $she, " are great couple.";
?>
This code produces the following:
23
PHP Basics
24
Introduction echo "$he and $she are great couple.";
to PHP
The printf() Statement
The printf() statement is good in order to blend output with static text and
dynamic information which is kept in one or more variables. This statement
separates static and dynamic data in two different sections which allow easy
maintenance and also does large control over dynamic information required to
screen in terms of type, precision, alignment and position as shown:
integer printf(string format [, mixed args])
25
Check your progress 3
1. What is the difference between printf and sprint?
26
2. What is the difference between echo and print? PHP Basics
a. Echo has no return value while print has a return value of 1
b. Echo can take multiple parameters while print can take one argument.
Object
NULL
Resource
PHP String
A string is sequence of characters that can be "K. J. Sharma" which is inside
quotes or double quotes as shown in example:
<?php
$x = "K. J. Sharma";
$y = 'K. J. Sharma';
echo $x;
echo "<br>";
echo $y;
?>
Output
27
Introduction K. J. Sharma
to PHP
K. J. Sharma
PHP Integer
The integer data type is without decimal that ranges from -2,147,483,648
and 2,147,483,647 and carries certain rules:
An integer should have at least 1 digit
An integer should have 1 decimal point
An integer can be positive or negative
An integers specifies formats such as decimal (10-based), hexadecimal (16-
based) or octal (8-based)
The PHP var_dump() function returns the data type and value as shown in
function example:
<?php
$x = 1234;
var_dump($x);
?>
Output:
Int(1234)
PHP Float
Float is a floating point number having decimal point or exponential form.
In an example shown, $x is float where PHP var_dump() function returns data
type as:
<?php
$x = 12.345;
var_dump($x);
?>
Output:
float(12.345)
PHP Boolean
28
A
Bool
ean
has
only
two
value
s
whic
h can
be
TRU
E or
FAL
SE.
29
$x = true;
PHP Basics
$y = false;
Booleans are often used in conditional testing.
PHP Array
Array stores many values in single variable. From the example, $scooters is
an array which returns PHP var_dump() function as:
<?php
$cars = array("Bajaj","Priya","LML");
var_dump($scooters);
?>
Output:
array(3) { [0]=> string(5) "Bajaj" [1]=> string(3) "Priya" [2]=> string(6) "LML" }
PHP Object
Object are data type that keep data and information about data processing. It
is noted that in PHP, object should clearly be declared. Initially we have to declare
a class of object. A class is a structure that has properties and methods such as:
<?php
class Scooter {
function Scooter()
{
$this->model = "Priya 112";
}
}
// create an object
$herbie = new Scooter();
30
Priya
112
31
Introduction PHP NULL Value
to PHP
Null is special data type having single value: NULL. A variable of data type
NULL is variable having no assigned value. If variable is without a value, it is
directly given with NULL value. Example
<?php
$x = "Name!";
$x = null;
var_dump($x);
?>
Output:
null
PHP Resource
It is a different type of resource which is not real data type which stores
reference to functions and resources that is external to PHP.
b. #num
c. $21
d. None of these
2. What is resource in PHP?
d. All of these
32
2.5 Identifiers PHP Basics
Variable names begin with dollar sign ($) and are case-sensitive such as:
$copy
$head_count
$MaxArea
$I_HEART_PHP
$_self
$_int
There exist certain uncommon variable names like:
$not valid
$|
$3wa
Function names
33
Introduction Keywords
to PHP
It is a reserved word by language for its main function that cannot assign
variable, function, class or constant.
d. All of these.
2.6 Variables
A variable in PHP serves as containers which used for keeping information.
It starts with $ sign followed by name of variable as shown in example:
<?php
$txt = "Solving Integers";
$x = 11;
$y = 16.7;
?>
Output:
Solving Integers
11
16.7
On executing above program, variable $txt will keep the value Solving
Integers, where variable $x will keep value 11 while variable $y keep value 16.7.
Variable have short name or can be descriptive.
34
Variable name to begin with letter or underscore
35
Variable name cannot begin with number
PHP Basics
Variable name only has alpha-numeric characters and underscores
Variable names are case-
sensitive Output Variables
The PHP echo statement is mainly applied to output data to screen as shown
in example:
<?php
$txt = "K. J. Sharma Books";
echo "I like!";
?>
Output:
<?php
$x = 8;
$y = 8;
echo $x + $y;
?>
Output
:
16
In above example we see that we have not instructed PHP about data type.
PHP will directly convert variable to correct data type as per its value.
PHP Variables Scope
In PHP, variables are declared in the script. The scope of variable is part of
script where variable can be used. PHP has three different variable scopes:
local
global
static
myTest();
echo "<p>Variable x outside function is: $x</p>";
?>
Output:
<?php
function myTest() {
}
myTest();
?>
Output:
$x = 12;
$y = 18;
function myTest() {
global $x, $y;
$y = $x + $y;
}
myTest();
Output
:
30
$x = 9;
$y = 19;
function myTest() {
$GLOBALS['y'] = $GLOBALS['x'] + $GLOBALS['y'];
}
myTest();
Output:
28
function myTest() {
static $x = 5;
echo $x;
$x++;
}
myTest();
myTest();
myTest();
?>
Output
:
5
Here every time the function is called with variable having information
contained from last when function was called.
39
Check your progress 6 PHP Basics
$num1 = 7;
$num2 = 15;
function test() {
global $num1, $num2;
test();
echo $num2;
?>
a. 8
b. -8
c. 0
d. None of these
$a = 'alen';
?>
a. "My name is $a"
d. None of these
40
Introduction
to PHP
2.7 Constants
Constants are variables which cannot be defined unless changed or
undefined. It is an identifier (name) for simple value which cannot be changed
during script. So valid constant name begins with letter or underscore. To create a
constant, you require to define() function.
Syntax
?>
Output:
<?php
define("GREETING", "Welcome to my Library of Books!", true);
echo greeting;
?>
Output:
Welcome to my Library of Books!
Constants are direct global applied across the script. The example uses constant in
a function, even if it is defined outside the function:
<?php
41
define("GR
EETING",
" Welcome
to my
Library of
Books!");
42
function myTest() {
PHP Basics
echo GREETING;
}
myTest();
?>
Output:
Welcome to my Library of Books!
define("HELLO", 100);
echo HELLO;
?>
a. Hello
b. HELLO
c. 100
d. None of these
2.8 Expressions
Expressions are bits of PHP which calculates to generate a value. It carries
simple expressions having literal values and variables that shows literal value
calculates it as variable calculates value stored in variable.
In PHP, expressions are important building stones which can be applied for
writing an expression. The simplest expression is anything that has value. The
basic feature of expressions results are constants and variables.
It is noted that when you type "$a = 10", then it shows that '10' assigns to $a.
'10', has value 10, or we see that '10' is an expression with value of 10.
43
Introduction After an assignment, $a's value as 10, hence if we wrote $b = $a, then $b =
to PHP
10. In other words, $a is expression with value 10. It is noted that PHP takes
expressions much further in same way as other languages. In an example '$a = 10'
it is noted that there exists two values, value of integer constant '10' and value of
$a that updates to 10 as well. In practice, we see that '$a = 10' is an expression
having value 10. So writing like '$b = $a = 10' is like '$a = 10; $b = 10;'. As
assignments are arranged from right to left, so we can write '$b = $a = 10'.
b. $a+ $b
c. @a+@b
d. None of these
Double Quotes
Strings in double quotes are applied in PHP scripts as they give flexibility
since both variables and escape sequences are parsed as shown in example:
<?php
$book = "Electronics";
44
This example returns the following:
PHP Basics
Sanjay's favorite book is Electronics.
Single Quotes
Keeping a string in single quotes is important while interpreted string
exactly similar as stated that has variables and escape sequences not interpreted
when string is parsed as shown:
print 'This string will $print exactly as it\'s \n declared.';
It shows output as:
d. None of these
45
Introduction
to PHP
2.10 Control Structures
In PHP, control structures are loops, conditionals and if which are familiar
to javascript programmer such as:
if and else
elseif
while
do...while
for
foreach
break and continue
switch
Reference
if and else
It is same as javascript like:
if ($a==$b) {
print "The same";
}
else {
print "They aren't the same";
elseif
elseif also else if gives a second if-statement where first if-condition if met run
other elseif conditions even if not run.
$a="Sanjay";
if($a=="Sanjay"){
print"Hello Sanjay";}
elseif($a=="Sharma"){
print"Hello Sharma";
46
}
PHP Basics
else if ($a=="Sanjay"){
print "Hello Sanjay 2";
}
else {
while
While loop is similar as in javascript:
$i=1;
while ($i<10){
print "Hello";
$i++;
do..while
do..while loop is same as while loop but while loop will not run if condition does
not exists. The do..while loop always run at least once as shown in example:
$i=1;
do{
while($i>100);
//result: The number is 1
for
The for loop in PHP is same as in javascript's:
for($i=1;$i<=2;$i++){
print "This is $i<br>";
}
47
Introduction //This is 5
to PHP
//This is 6
The for loop can be written in various ways. One additional way is as follows:
for($i=1;$i<=3;print "This time it's $i ",$i++);
//result: This time it's 5 This time it's 6 This time it's 7
foreach
This control structure access items in array:
$arr=array("cow","deer","gost");
foreach($arr as $value){
echo"Value: $value<br>\n";
//Value: cow
//Value: deer
//Value: gost
This example shows the keys and values of an associative array:
$a=array("fruit"=>"pineapple","meat"=>"gost","vegetables"=>"beans","sweet"=>
"ice cream");
foreach($a as $k=>$v){
print"\$a[$k]=>$v.\n";
};
//result: $a[fruit]=>pineapple. $a[meat]=>gost. $a[vegetables]=>beans.
$a[sweet]=>ice cream.
switch
The switch statement will continue as shown which print all statements after
finding as true as shown:
$i=1;
48
c
a
s
e
0
:
49
print "zero";
PHP Basics
break;
case 1:
print "one";
break;
//result: one
<?php
$a = 25;
if ($a < "30") {
echo "Hello World!";
}
?>
a. Hello World!
b. 25
c. 30
d. None of these
switch ($x)
{
case 1:
print "hello";
50
Introduction case 2:
to PHP print "to";
default:
print "world";
}
?>
a. helloworld
b. hellotoworld
c. syntax error
d. None of these
It is known that comment in PHP code is a line that is not part of program
but to read by person editing the code. Identifier are name used to name variables,
functions, constants and classes where first character is ASCII letter, underscore
or characters between ASCII 0x7F and ASCII 0xFF.
It is noted that constants are variables that are not defined unless changed or
undefined which serves as an identifier for simple value that cannot be changed
during script. As seen, expressions are bits of PHP that calculates value having
simple expressions with literal values and variables.
51
Check your progress 2 PHP Basics
Answers: (1 –d)
Answers: (1 –b)
Answers: (1 –c)
Answers: (1 –b)
Answers: (1 –b)
2.13 Glossary
1. Variables - They are containers used for keeping information.
2. Constants - Variables that are not defined till it
52
changes or undefined and also serves as identifier.
3. Expression - These are bits in PHP that finds generated value.
53