Вы находитесь на странице: 1из 73

COURSE CODE COURSE TITLE L T P C

INTERNET PROGRAMMING
1151CS202 2 0 2 3
Course Category: Program Core

A. Preamble:
This course Internet Programming provides an introduction and Basic Concepts of Server-Side
Programming and Designing of Static and Dynamic WebPages.

B. Pre-requisites:
Sl. No Course Code Course Name
1 1151CS103 Programming in Java

C. Related Courses:
Sl. No Course Code Course Name
1 1156CS601 Minor Project

2 1156CS701 Major Project

D. Course Outcomes:
Upon the successful completion of the course, learners will be able to
Level of learning
CO domain (Based on
Course Outcomes
Nos. revised Bloom’s
taxonomy)
Demonstrate web applications using Hypertext Markup Language,
CO1 K2
Cascading style sheets and Java Database Connectivity.
Explain about the java networking technologies and working of struts K2
CO2
framework.
Develop applications using hibernate framework and Hypertext Markup
CO3 Language Protocols. K3

Design web applications in PHP (Hypertext Preprocessing) and to present K3


CO4 data in XMLformat.

Illustrate Rich Internet Application using AJAX and web services. K2


CO5

E. Correlation of COs with Program outcomes and Programme Specific Outcomes:


PSO PSO PSO
Cos PO1 PO2 PO3 PO4 PO5 PO6 PO7 PO8 PO9 PO10 PO11 PO12 1 2 3
L H L
CO1 M M M L
L M L
CO2 M L L L L
L H L
CO3 M H H M L L H L M M
L M L
CO4 M H H L L M M
L H L
CO5 M M M L L L L L H M M
H- Strong; M-Medium; L-Low

F. Course Content:
UNIT I WEBSITES BASICS, HTML 5, CSS 3 9
Basics-RIA Rich Internet Applications Internet technologies Overview –Understanding the
difference between internet and intranet; HTML and CSS: HTML 5.0, XHTML, CSS 3- TCP
and Datagram Sockets – Remote Method Invocation - Servlets, Java Server Pages. – JDBC Java
Beans – Enterprise Java Beans.

UNIT II NAMING SERVICES AND STRUTS 9


Java Security – Native Methods – Java Virtual Machine - Naming Services – Java Mail – Java
Messaging Services – Transactions- Introduction to struts Frame work.

UNIT III HIBERNATE, HTML AND PROTOCOLS 9


Introduction to hibernate – Hibernate, HQL – J2EE (struts) and hibernate –Hibernate and Aspect
– Birth Reporting Tool. Introduction – Network concepts - Web concepts – Internet addresses –
URL – Retrieving Data with URL - CGI – Introduction to HTML – HTML protocols – HTTP,
SMTP, POP3, MIME, IMAP – Forms – Events.

UNIT IV PHP and XML 8 9


An introduction to PHP: PHP- Using PHP- Variables- Program control- Built-in Functions-Connecting to
Database – Using Cookies-Regular Expressions; XML: Basic XML- Document Type DefinitionXML Schema
DOM and Presenting XML, XML Parsers and Validation, XSL and XSLT Transformation, News Feed (RSS
and ATOM).

UNIT V INTRODUCTION TO AJAX and WEB SERVICES 9


Ajax Client Server Architecture-XML Http Request Object-Call Back Methods; Web Services:
Introduction- Java web services Basics – Creating, Publishing, Testing and Describing a Web
services (WSDL)-Consuming a web service, Database Driven web service from an application –
SOAP
TOTAL: 45 periods
G. Learning Resources:
i. Text Book

1. Deitel&Deitel, Goldberg, “Internet and world wide web – How to Program”, Pearson Education
Asia, 2011.

ii. Reference Books

1. Chris Bates, Web Programming – Building Intranet Applications, 3 rd. Edition, Wiley Publications,
2009.
2. Jeffrey C and Jackson, “Web Technologies A Computer Science Perspective”, Pearson Education,
2011.
3. UttamK. Roy, “Web Technologies”, Oxford University Press, 2011.

iii. Online Resources

1. Eric Ladd, Jim O’ Donnel, “Using HTML 4, XML and JAVA”, Prentice Hall of India –
QUE, 1999.
2. PHP &MYSQL TimConverse, Joyce Park, Alark Morgan.
3. PHP for Beginners VikramVaswani, Tata McGraw-Hill
4. Ajax in Action, Dreamtech press
5. WWW. W3schools.com

UNIT IV

PHP and XML 8


An introduction to PHP: PHP

PHP started out as a small open source project that evolved as more and more people found out
how useful it was. Rasmus Lerdorf unleashed the first version of PHP way back in 1994.

 PHP is a recursive acronym for "PHP: Hypertext Preprocessor".


 PHP is a server side scripting language that is embedded in HTML. It is used to manage
dynamic content, databases, session tracking, even build entire e-commerce sites.
 It is integrated with a number of popular databases, including MySQL, PostgreSQL,
Oracle, Sybase, Informix, and Microsoft SQL Server.
 PHP is pleasingly zippy in its execution, especially when compiled as an Apache module
on the Unix side. The MySQL server, once started, executes even very complex queries
with huge result sets in record-setting time.
 PHP supports a large number of major protocols such as POP3, IMAP, and LDAP. PHP4
added support for Java and distributed object architectures (COM and CORBA), making
n-tier development a possibility for the first time.
 PHP is forgiving: PHP language tries to be as forgiving as possible.
 PHP Syntax is C-Like.

Common uses of PHP


 PHP performs system functions, i.e. from files on a system it can create, open, read,
write, and close them.
 PHP can handle forms, i.e. gather data from files, save data to a file, through email you
can send data, return data to the user.
 You add, delete, modify elements within your database through PHP.
 Access cookies variables and set cookies.
 Using PHP, you can restrict users to access some pages of your website.
 It can encrypt data.

Characteristics of PHP
Five important characteristics make PHP's practical nature possible −

 Simplicity
 Efficiency
 Security
 Flexibility
 Familiarity

"Hello World" Script in PHP


To get a feel for PHP, first start with simple PHP scripts. Since "Hello, World!" is an essential
example, first we will create a friendly little "Hello, World!" script.

As mentioned earlier, PHP is embedded in HTML. That means that in amongst your normal
HTML (or XHTML if you're cutting-edge) you'll have PHP statements like this −

<html>

<head>
<title>Hello World</title>
</head>

<body>
<?php echo "Hello, World!";?>
</body>

</html>

It will produce following result −

Hello, World!
If you examine the HTML output of the above example, you'll notice that the PHP code is not
present in the file sent from the server to your Web browser. All of the PHP present in the Web
page is processed and stripped from the page; the only thing returned to the client from the Web
server is pure HTML output.

All PHP code must be included inside one of the three special markup tags ATE are recognised
by the PHP Parser.

<?php PHP code goes here ?>

<? PHP code goes here ?>

<script language = "php"> PHP code goes here </script>

A most common tag is the <?php...?> and we will also use the same tag in our tutorial.

From the next chapter we will start with PHP Environment Setup on your machine and then we
will dig out almost all concepts related to PHP to make you comfortable with the PHP language.

This chapter will give you an idea of very basic syntax of PHP and very important to make your
PHP foundation strong.

Escaping to PHP
The PHP parsing engine needs a way to differentiate PHP code from other elements in the page.
The mechanism for doing so is known as 'escaping to PHP'. There are four ways to do this −

Canonical PHP tags

The most universally effective PHP tag style is −

<?php...?>

If you use this style, you can be positive that your tags will always be correctly interpreted.

Short-open (SGML-style) tags

Short or short-open tags look like this −

<?...?>

Short tags are, as one might expect, the shortest option You must do one of two things to enable
PHP to recognize the tags −

 Choose the --enable-short-tags configuration option when you're building PHP.


 Set the short_open_tag setting in your php.ini file to on. This option must be disabled to
parse XML with PHP because the same syntax is used for XML tags.
ASP-style tags

ASP-style tags mimic the tags used by Active Server Pages to delineate code blocks. ASP-style
tags look like this −

<%...%>

To use ASP-style tags, you will need to set the configuration option in your php.ini file.

HTML script tags

HTML script tags look like this −

<script language = "PHP">...</script>

Commenting PHP Code


A comment is the portion of a program that exists only for the human reader and stripped out
before displaying the programs result. There are two commenting formats in PHP −

Single-line comments − They are generally used for short explanations or notes relevant to the
local code. Here are the examples of single line comments.

<?
# This is a comment, and
# This is the second line of the comment

// This is a comment too. Each style comments only


print "An example with single line comments";
?>

Multi-lines printing − Here are the examples to print multiple lines in a single print statement −

<?
# First Example
print <<<END
This uses the "here document" syntax to output
multiple lines with $variable interpolation. Note
that the here document terminator must appear on a
line with just a semicolon no extra whitespace!
END;

# Second Example
print "This spans
multiple lines. The newlines will be
output as well";
?>
Multi-lines comments − They are generally used to provide pseudocode algorithms and more
detailed explanations when necessary. The multiline style of commenting is the same as in C.
Here are the example of multi lines comments.

<?
/* This is a comment with multiline
Author : Mohammad Mohtashim
Purpose: Multiline Comments Demo
Subject: PHP
*/

print "An example with multi line comments";


?>

PHP is whitespace insensitive


Whitespace is the stuff you type that is typically invisible on the screen, including spaces, tabs,
and carriage returns (end-of-line characters).

PHP whitespace insensitive means that it almost never matters how many whitespace characters
you have in a row.one whitespace character is the same as many such characters.

For example, each of the following PHP statements that assigns the sum of 2 + 2 to the variable
$four is equivalent −

$four = 2 + 2; // single spaces


$four <tab>=<tab2<tab>+<tab>2 ; // spaces and tabs
$four =
2+
2; // multiple lines

PHP is case sensitive


Yeah it is true that PHP is a case sensitive language. Try out following example −

Live Demo

<html>
<body>

<?php
$capital = 67;
print("Variable capital is $capital<br>");
print("Variable CaPiTaL is $CaPiTaL<br>");
?>

</body>
</html>

This will produce the following result −


Variable capital is 67
Variable CaPiTaL is

Statements are expressions terminated by semicolons


A statement in PHP is any expression that is followed by a semicolon (;).Any sequence of valid
PHP statements that is enclosed by the PHP tags is a valid PHP program. Here is a typical
statement in PHP, which in this case assigns a string of characters to a variable called $greeting −

$greeting = "Welcome to PHP!";

Expressions are combinations of tokens


The smallest building blocks of PHP are the indivisible tokens, such as numbers (3.14159),
strings (.two.), variables ($two), constants (TRUE), and the special words that make up the
syntax of PHP itself like if, else, while, for and so forth

Braces make blocks


Although statements cannot be combined like expressions, you can always put a sequence of
statements anywhere a statement can go by enclosing them in a set of curly braces.

Here both statements are equivalent −

if (3 == 2 + 1)
print("Good - I haven't totally lost my mind.<br>");

if (3 == 2 + 1) {
print("Good - I haven't totally");
print("lost my mind.<br>");
}

Running PHP Script from Command Prompt


Yes you can run your PHP script on your command prompt. Assuming you have following
content in test.php file

Live Demo

<?php
echo "Hello PHP!!!!!";
?>

Now run this script as command prompt as follows −

$ php test.php
It will produce the following result −

Hello PHP!!!!!

What is PHP?
 PHP is an acronym for "PHP: Hypertext Preprocessor"
 PHP is a widely-used, open source scripting language
 PHP scripts are executed on the server
 PHP is free to download and use

What is a PHP File?


 PHP files can contain text, HTML, CSS, JavaScript, and PHP code
 PHP code are executed on the server, and the result is returned to the browser as plain HTML
 PHP files have extension ".php"

What Can PHP Do?


 PHP can generate dynamic page content
 PHP can create, open, read, write, delete, and close files on the server
 PHP can collect form data
 PHP can send and receive cookies
 PHP can add, delete, modify data in your database
 PHP can be used to control user-access
 PHP can encrypt data

With PHP you are not limited to output HTML. You can output images, PDF files, and even
Flash movies. You can also output any text, such as XHTML and XML.

Why PHP?
 PHP runs on various platforms (Windows, Linux, Unix, Mac OS X, etc.)
 PHP is compatible with almost all servers used today (Apache, IIS, etc.)
 PHP supports a wide range of databases
 PHP is free. Download it from the official PHP resource: www.php.net
 PHP is easy to learn and runs efficiently on the server side
What's new in PHP 7
 PHP 7 is much faster than the previous popular stable release (PHP 5.6)
 PHP 7 has improved Error Handling
 PHP 7 supports stricter Type Declarations for function arguments
 PHP 7 supports new operators (like the spaceship operator: <=> )

Using PHP

PHP File Extensions


File extension and Tags In order for the server to identify our PHP files and scripts, we must
save the file with the “.php” extension. Older PHP file extensions include

 .phtml
 .php3
 .php4
 .php5
 .phps

PHP was designed to work with HTML, and as such, it can be embedded into the HTML code.

You can create PHP files without any html tags and that is called Pure PHP file .

The server interprets the PHP code and outputs the results as HTML code to the web browsers.

In order for the server to identify the PHP code from the HTML code, we must always enclose
the PHP code in PHP tags.

A PHP tag starts with the less than symbol followed by the question mark and then the words
“php”.

PHP is a case sensitive language, “VAR” is not the same as “var”.

The PHP tags themselves are not case-sensitive, but it is strongly recommended that we use
lower case letter. The code below illustrates the above point.

<?php … ?>

We will be referring to the PHP lines of code as statements. PHP statements end with a semi
colon (;). If you only have one statement, you can omit the semi colon. If you have more than
one statement, then you must end each line with a semi colon. For the sake of consistency, it is
recommended that you always end your statement(s) with a semi colon. PHP scripts are
executed on the server. The output is returned in form of HTML.

PHP Hello world


The program shown below is a basic PHP application that outputs the words “Hello World!”
When viewed in a web browser.

<?php
echo "Hello world";
?>

Output:

Hello world

Variables

PHP Variables
A variable can have a short name (like x and y) or a more descriptive name (age, carname,
total_volume).

Rules for PHP variables:

 A variable starts with the $ sign, followed by the name of the variable
 A variable name must start with a letter or the underscore character
 A variable name cannot start with a number
 A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9,
and _ )
 Variable names are case-sensitive ($age and $AGE are two different variables)

Remember that PHP variable names are case-sensitive!

<!DOCTYPE html>
<html>
<body>
<?php
$txt = "Hello world!";
$x = 5;
$y = 10.5;
echo $txt;
echo "<br>";
echo $x;
echo "<br>";
echo $y;
?>
</body>
</html>
Output:-
Hello world!
5
10.5

Output Variables
The PHP echo statement is often used to output data to the screen.

The following example will show how to output text and a variable:

<!DOCTYPE html>
<html>
<body>
<?php
$txt = "W3Schools.com";
echo "I love $txt!";
?>
</body>
</html>
Output:-
I love W3Schools.com!

<!DOCTYPE html>
<html>
<body>
<?php
$x = 5;
$y = 4;
echo $x + $y;
?>
</body>
</html>
Output:-
9

PHP is a Loosely Typed Language


In the example above, notice that we did not have to tell PHP which data type the variable is.
PHP automatically associates a data type to the variable, depending on its value. Since the data
types are not set in a strict sense, you can do things like adding a string to an integer without
causing an error.

In PHP 7, type declarations were added. This gives an option to specify the data type expected
when declaring a function, and by enabling the strict requirement, it will throw a "Fatal Error" on
a type mismatch.

You will learn more about strict and non-strict requirements, and data type declarations in
the PHP Functions chapter.

PHP Variables Scope


In PHP, variables can be declared anywhere in the script.

The scope of a variable is the part of the script where the variable can be referenced/used.

PHP has three different variable scopes:

 local
 global
 static

Global and Local Scope


A variable declared outside a function has a GLOBAL SCOPE and can only be accessed outside
a function:

<!DOCTYPE html>
<html>
<body>
<?php
$x = 5; // global scope
function myTest() {
// using x inside this function will generate an error
echo "<p>Variable x inside function is: $x</p>";
}
myTest();
echo "<p>Variable x outside function is: $x</p>";
?>
</body>
</html>
Output:-
Variable x inside function is:

Variable x outside function is: 5

A variable declared within a function has a LOCAL SCOPE and can only be accessed within that function:

<!DOCTYPE html>
<html>
<body>
<?php
function myTest() {
$x = 5; // local scope
echo "<p>Variable x inside function is: $x</p>";
}
myTest();
// using x outside the function will generate an error
echo "<p>Variable x outside function is: $x</p>";
?>
</body>
</html>
Output:-
Variable x inside function is: 5

Variable x outside function is:

PHP The global Keyword


The global keyword is used to access a global variable from within a function.

To do this, use the global keyword before the variables (inside the function):

<!DOCTYPE html>

<html>

<body>

<?php

$x = 5;

$y = 10;

function myTest() {

global $x, $y;

$y = $x + $y;

myTest(); // run function


echo $y; // output the new value for variable $y

?>

</body>

</html>

Output:-
15

PHP The static Keyword


Normally, when a function is completed/executed, all of its variables are deleted. However,
sometimes we want a local variable NOT to be deleted. We need it for a further job.

To do this, use the static keyword when you first declare the variable:

<!DOCTYPE html>
<html>
<body>

<?php
function myTest() {
static $x = 0;
echo $x;
$x++;
}
myTest();
echo "<br>";
myTest();
echo "<br>";
myTest();
?>
</body>
</html>
Output:-
0
1
2

Program control

PHP Control Structures: If else, Switch Case

What is a control structure?


Code execution can be grouped into categories as shown below

 Sequential – this one involves executing all the codes in the order in which they have been
written.
 Decision – this one involves making a choice given a number of options. The code executed
depends on the value of the condition.

A control structure is a block of code that decides the execution path of a program depending on
the value of the set condition.

Let’s now look at some of the control structures that PHP supports.

PHP IF Else
If… then... else is the simplest control structure. It evaluates the conditions using Boolean logic
When to use if… then… else
 You have a block of code that should be executed only if a certain condition is true
 You have two options, and you have to select one.
 If… then… else if… is used when you have to select more than two options and you have to
select one or more

Syntax The syntax for if… then… else is;

<?php
if (condition is true) {

block one

else

block two

}
?>

HERE,

 “if (condition is true)” is the control structure


 “block one” is the code to be executed if the condition is true
 {…else…} is the fallback if the condition is false
 “block two” is the block of code executed if the condition is false

How it works The flow chart shown below illustrates how the if then… else control structure
works
Let’s see this in action The code below uses “if… then… else” to determine the larger value
between two numbers.

<?php

$first_number = 7;

$second_number = 21;

if ($first_number > $second_number){

echo "$first_number is greater than $second_number";

}else{

echo "$second_number is greater than $first_number";

?>

Output:

21 is greater than 7

PHP Switch Case


Switch… case is similar to the if then… else control structure.
It only executes a single block of code depending on the value of the condition.

If no condition has been met then the default block of code is executed.

It has the following basic syntax.

<?php
switch(condition){

case value:

//block of code to be executed

break;

case value2:

//block of code to be executed

break;

default:

//default block code

break;

}
?>

HERE,

 “switch(…){…}” is the control structure block code


 “case value: case…” are the blocks of code to be executed depending on the value of the
condition
 “default:” is the block of code to be executed when no value matches with the condition

How it works

The flow chart shown below illustrates how the switch control structure works
Practical example

The code below uses the switch control structure to display a message depending on the day of
the week.

<?php

$today = "wednesday";

switch($today){

case "sunday":

echo "pray for us sinners.";

break;

case "wednesday":

echo "ladies night, take her out for dinner";


break;

case "saturday":

echo "take care as you go out tonight.";

break;

default:

echo "have a nice day at work";

break;

?>

Output:

ladies night, take her out for dinner

PHP Loop: For, ForEach, While, Do While [Example]

A Loop is an Iterative Control Structure that involves executing the same number of code a
number of times until a certain condition is met.

PHP For Loop

The above code outputs “21 is greater than 7” For loops For... loops execute the block of code a
specifiednumber of times. There are basically two types of for loops;

 for
 for… each.

Let’s now look at them separately. For loop It has the following basic syntax

<?php
for (initialize; condition; increment){

//code to be executed

}
?>

HERE,

 “for…{…}” is the loop block


 “initialize” usually an integer; it is used to set the counter’s initial value.
 “condition” the condition that is evaluated for each php execution. If it evaluates to true then
execution of the for... loop is terminated. If it evaluates to false, the execution of the for... loop
continues.
 “increment” is used to increment the initial value of counter integer.

How it works

The flowchart shown below illustrates how for loop in php works

How to code

The code below uses the “for… loop” to print values of multiplying 10 by 0 through to 10

<?php

for ($i = 0; $i < 10; $i++){

$product = 10 * $i;

echo "The product of 10 * $i is $product <br/>";


}

?>

Output:

The product of 10 x 0 is 0
The product of 10 x 1 is 10
The product of 10 x 2 is 20
The product of 10 x 3 is 30
The product of 10 x 4 is 40
The product of 10 x 5 is 50
The product of 10 x 6 is 60
The product of 10 x 7 is 70
The product of 10 x 8 is 80
The product of 10 x 9 is 90

PHP For Each loop

The php foreach loop is used to iterate through array values. It has the following basic syntax

<?php
foreach($array_variable as $array_values){

block of code to be executed

}
?>

HERE,

 “foreach(…){…}” is the foreach php loop block code


 “$array_data” is the array variable to be looped through
 “$array_value “ is the temporary variable that holds the current array item values.
 “block of code…” is the piece of code that operates on the array values

How it works The flowchart shown below illustrates how the for… each… loop works
Practical examples

The code below uses for… each loop to read and print the elements of an array.

<?php

$animals_list = array("Lion","Wolf","Dog","Leopard","Tiger");

foreach($animals_list as $array_values){

echo $array_values . "<br>";

?>

Output:

Lion
Wolf
Dog
Leopard
Tiger

Let’s look at another example that loops through an associative array.

An associative array uses alphanumeric words for access keys.

<?php

$persons = array("Mary" => "Female", "John" => "Male", "Mirriam" =>


"Female");

foreach($persons as $key => $value){

echo "$key is $value"."<br>";

?>

The names have been used as array keys and gender as the values.

Output:

Mary is Female
John is Male
Mirriam is Female

While Loop
PHP While loop

They are used to execute a block of code a repeatedly until the set condition gets satisfied

When to use while loops

 While loops are used to execute a block of code until a certain condition becomes true.
 You can use a while loop to read records returned from a database query.

Types of while loops

 Do… while - executes the block of code at least once before evaluating the condition
 While… - checks the condition first. If it evaluates to true, the block of code is executed as long
as the condition is true. If it evaluates to false, the execution of the while loop is terminated.

While loop
It has the following syntax

<?php
while (condition){

block of code to be executed;

}
?>

HERE,

 “while(…){…}” is the while loop block code


 “condition” is the condition to be evaluated by the while loop
 “block of code…” is the code to be executed if the condition gets satisfied

How it works

The flow chart shown below illustrates how the while… loop works

Practical example

The code below uses the while… loop to print numbers 1 to 5.

<?php

$i = 0;

while ($i < 5){

echo $i + 1 . "<br>";
$i++;

?>

Output:

1
2
3
4
5

PHP Do While

The difference between While… loop and Do… while loop is do… while is executed at-least
once before the condition is evaluated.

Let’s now look at the basic syntax of a do… while loop

<?php
do{

block of code to be executed

}
?>

while(condition);

HERE,

 “do{…} while(…)” is the do… while loop block code


 “condition” is the condition to be evaluated by the while loop
 “block of code…” is the code that is executed at least once by the do… while loop

How it works

The flow chart shown below illustrates how the while… loop works
Practical example

We are now going to modify the while… loop example and implement it using the do… while
loop and set the counter initial value to 9.

The code below implements the above modified example

<?php

$i = 9;

do{

echo "$i is"." <br>";

while($i < 9);

?>

The above code outputs:

Note the above example outputs 9 only.


This is because the do… while loop is executed at least once even if the set condition evaluates to
false.

Summary
 The for… loop is used to execute a block of a specified number of times
 The foreach… loop is used to loop through arrays
 While… loop is used to execute a block of code as long as the set condition is made to be false
 The do… while loop is used to execute the block of code at least once then the rest of the
execution is dependent on the evaluation of the set condition

Built-in Functions

What is a Function?
A function is a reusable piece or block of code that performs a specific action.

Functions can either return values when called or can simply perform an operation without
returning any value.

PHP has over 700 functions built in that perform different tasks.

In this tutorial, you will learn-

 Why use Functions?


 Built in Functions
 String Functions
 Numeric Functions
 Date Function
 Why use User Defined Functions?

Why use Functions?


 Better code organization – functions allow us to group blocks of related code that perform a
specific task together.
 Reusability – once defined, a function can be called by a number of scripts in our PHP files. This
saves us time of reinventing the wheel when we want to perform some routine tasks such as
connecting to the database
 Easy maintenance- updates to the system only need to be made in one place.

Built in Functions
Built in functions are functions that exist in PHP installation package.
These built in functions are what make PHP a very efficient and productive scripting language.

The built in functions can be classified into many categories. Below is the list of the categories.

String Functions
These are functions that manipulate string data, refer to the article on strings for implementation
examples of string functions

Numeric Functions
Numeric functions are function that return numeric results.

Numeric php function can be used to format numbers, return constants, perform mathematical
computations etc.

What is a Function?
A function is a reusable piece or block of code that performs a specific action.

Functions can either return values when called or can simply perform an operation without
returning any value.

PHP has over 700 functions built in that perform different tasks.

In this tutorial, you will learn-

 Why use Functions?


 Built in Functions
 String Functions
 Numeric Functions
 Date Function
 Why use User Defined Functions?

Why use Functions?


 Better code organization – functions allow us to group blocks of related code that perform a
specific task together.
 Reusability – once defined, a function can be called by a number of scripts in our PHP files. This
saves us time of reinventing the wheel when we want to perform some routine tasks such as
connecting to the database
 Easy maintenance- updates to the system only need to be made in one place.

Built in Functions
Built in functions are functions that exist in PHP installation package.

These built in functions are what make PHP a very efficient and productive scripting language.

The built in functions can be classified into many categories. Below is the list of the categories.

String Functions
These are functions that manipulate string data, refer to the article on strings for implementation
examples of string functions

Numeric Functions
Numeric functions are function that return numeric results.

Numeric php function can be used to format numbers, return constants, perform mathematical
computations etc.

The table below shows the common PHP numeric functions

Function Description Example Output

is_number Accepts an argument and returns <?php false


if(is_numeric("guru"))
true if its numeric and false if it’s {
not echo "true";
}
else
{
echo "false";
}
?>

<?php true
if(is_numeric (123))
{
echo "true";
}
else
{
echo "false";
}
?>
number_format Used to formats a numeric value <?php 2,509,663
echo
using digit separators and decimal number_format(2509663);
points ?>
Function Description Example Output

rand Used to generate a random <?php Random number


echo rand();
number. ?>

round Round off a number with decimal <?php 3


echo round(3.49);
points to the nearest whole ?>
number.

sqrt Returns the square root of a <?php 10


echo sqrt(100);
number ?>

cos Returns the cosine <?php 0.52532198881773


echo cos(45);
?>
sin Returns the sine <?php 0.85090352453412
echo sin(45);
?>
tan Returns the tangent <?php 1.6197751905439
echo tan(45);
?>
pi Constant that returns the value of <?php 3.1415926535898
echo pi();
PI ?>

Date Function
The date function is used to format Unix date and time to human readable format.

Check the article on PHP date functions for more details.

Other functions

These include;

 Arrays – see the article on arrays for examples


 Files – see the article on files for examples
 Database functions – see the article on MySQL PHP and other database access methods v2

Why use User Defined Functions?


User defined functions come in handy when;

 you have routine tasks in your application such as adding data to the database
 performing validation checks on the data
 Authenticating users in the system etc.

These activities will be spread across a number of pages.

Creating a function that all these pages can be calling is one of the features that make PHP a
powerful scripting language.

Before we create our first user defined function, let’s look at the rules that we must follow when
creating our own functions.

 Function names must start with a letter or an underscore but not a number
 The function name must be unique
 The function name must not contain spaces
 It is considered a good practice to use descriptive function names.
 Functions can optionally accept parameters and return values too.

Let’s now create our first function. We will create a very basic function that illustrates the major
components of a function in PHP.

<?php

//define a function that displays hello function

function add_numbers(){
echo 1 + 2;
}
add_numbers ();
?>

Output:

HERE,

 “function…(){…}” is the function block that tells PHP that you are defining a custom function
 “add_numbers” is the function name that will be called when using the function.
 “()” can be used to pass parameters to the function.
 “echo 'Hello function!';” is the function block of code that is executed. It could be any code
other than the one used in the above example.

Let’s now look at a fairly complex example that accepts a parameter and display a message just
like the above function.
Suppose we want to write a function that prints the user name on the screen, we can write a
custom function that accepts the user name and displays it on the screen.

The code below shows the implementation.

<?php
function display_name($name)
{
echo "Hello " . $name;
}
display_name("Martin Luther King");
?>

Output:

Hello Martin Luther King

HERE,

 “…($name){…” is the function parameter called name and is initialized to nameless. If no


parameter is passed to the function, nameless will be displayed as the name. This comes in
handy if not supplying any parameter to the function can result in unexpected errors.

Let’s now look at a function that accepts a parameter and then returns a value. We will create a
function that converts kilometers to miles. The kilometers will be passed as a parameter. The
function will return the miles equivalent to the passed kilometers. The code below shows the
implementation.

<?php
function kilometers_to_miles($kilometers = 0)
{
$miles_scale = 0.62;
return $kilometers * $miles_scale;
}
echo kilometers_to_miles(100);
?>

Output:

62

Connecting to Database
PHP 5 and later can work with a MySQL database using:

 MySQLi extension (the "i" stands for improved)


 PDO (PHP Data Objects)
Earlier versions of PHP used the MySQL extension. However, this extension was deprecated in
2012.

Should I Use MySQLi or PDO?


If you need a short answer, it would be "Whatever you like".

Both MySQLi and PDO have their advantages:

PDO will work on 12 different database systems, whereas MySQLi will only work with MySQL
databases.

So, if you have to switch your project to use another database, PDO makes the process easy. You
only have to change the connection string and a few queries. With MySQLi, you will need to
rewrite the entire code - queries included.

Both are object-oriented, but MySQLi also offers a procedural API.

Both support Prepared Statements. Prepared Statements protect from SQL injection, and are very
important for web application security.

MySQL Examples in Both MySQLi and PDO Syntax


In this, and in the following chapters we demonstrate three ways of working with PHP and
MySQL:

 MySQLi (object-oriented)
 MySQLi (procedural)
 PDO

MySQLi Installation
For Linux and Windows: The MySQLi extension is automatically installed in most cases, when
php5 mysql package is installed.

For installation details, go to: http://php.net/manual/en/mysqli.installation.php


PDO Installation
For installation details, go to: http://php.net/manual/en/pdo.installation.php

Open a Connection to MySQL


Before we can access data in the MySQL database, we need to be able to connect to the server:

Example (MySQLi Object-Oriented)


<?php
$servername = "localhost";
$username = "username";
$password = "password";

// Create connection
$conn = new mysqli($servername, $username, $password);

// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>

Note on the object-oriented example above:

$connect_error was broken until PHP 5.2.9 and 5.3.0. If you need to ensure compatibility with
PHP versions prior to 5.2.9 and 5.3.0, use the following code instead:

// Check connection
if (mysqli_connect_error()) {
die("Database connection failed: " . mysqli_connect_error());
}

Example (MySQLi Procedural)


<?php
$servername = "localhost";
$username = "username";
$password = "password";

// Create connection
$conn = mysqli_connect($servername, $username, $password);

// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";
?>

Example (PDO)
<?php
$servername = "localhost";
$username = "username";
$password = "password";

try {
$conn = new PDO("mysql:host=$servername;dbname=myDB", $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Connected successfully";
}
catch(PDOException $e)
{
echo "Connection failed: " . $e->getMessage();
}
?>

Note: In the PDO example above we have also specified a database (myDB). PDO require a
valid database to connect to. If no database is specified, an exception is thrown.

Tip: A great benefit of PDO is that it has an exception class to handle any problems that may
occur in our database queries. If an exception is thrown within the try{ } block, the script stops
executing and flows directly to the first catch(){ } block.
Close the Connection
The connection will be closed automatically when the script ends. To close the connection
before, use the following:

MySQLi Object-Oriented:
$conn->close();

MySQLi Procedural:
mysqli_close($conn);

PDO:
$conn = null;

Using Cookies

What is a Cookie?
A cookie is often used to identify a user. A cookie is a small file that the server embeds on the
user's computer. Each time the same computer requests a page with a browser, it will send the
cookie too. With PHP, you can both create and retrieve cookie values.

Create Cookies With PHP


A cookie is created with the setcookie() function.

Syntax
setcookie(name, value, expire, path, domain, secure, httponly);

Only the name parameter is required. All other parameters are optional.

PHP Create/Retrieve a Cookie


The following example creates a cookie named "user" with the value "John Doe". The cookie
will expire after 30 days (86400 * 30). The "/" means that the cookie is available in entire
website (otherwise, select the directory you prefer).
We then retrieve the value of the cookie "user" (using the global variable $_COOKIE). We also
use the isset() function to find out if the cookie is set:

Example
<?php
$cookie_name = "user";
$cookie_value = "John Doe";
setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/"); // 86400 = 1 day
?>
<html>
<body>

<?php
if(!isset($_COOKIE[$cookie_name])) {
echo "Cookie named '" . $cookie_name . "' is not set!";
} else {
echo "Cookie '" . $cookie_name . "' is set!<br>";
echo "Value is: " . $_COOKIE[$cookie_name];
}
?>

</body>
</html>

Note: The setcookie() function must appear BEFORE the <html> tag.

Note: The value of the cookie is automatically URLencoded when sending the cookie, and
automatically decoded when received (to prevent URLencoding, use setrawcookie() instead).

Modify a Cookie Value


To modify a cookie, just set (again) the cookie using the setcookie() function:

Example
<?php
$cookie_name = "user";
$cookie_value = "Alex Porter";
setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/");
?>
<html>
<body>

<?php
if(!isset($_COOKIE[$cookie_name])) {
echo "Cookie named '" . $cookie_name . "' is not set!";
} else {
echo "Cookie '" . $cookie_name . "' is set!<br>";
echo "Value is: " . $_COOKIE[$cookie_name];
}
?>

</body>
</html>

Delete a Cookie
To delete a cookie, use the setcookie() function with an expiration date in the past:

Example
<?php
// set the expiration date to one hour ago
setcookie("user", "", time() - 3600);
?>
<html>
<body>

<?php
echo "Cookie 'user' is deleted.";
?>

</body>
</html>

Check if Cookies are Enabled


The following example creates a small script that checks whether cookies are enabled. First, try
to create a test cookie with the setcookie() function, then count the $_COOKIE array variable:
Example
<?php
setcookie("test_cookie", "test", time() + 3600, '/');
?>
<html>
<body>

<?php
if(count($_COOKIE) > 0) {
echo "Cookies are enabled.";
} else {
echo "Cookies are disabled.";
}
?>

</body>
</html>

Regular Expressions

What is a Regular Expressions?


Regular expressions are powerful pattern matching algorithm that can be performed in a single
expression.

Regular expressions use arithmetic operators such as (+,-,^) to create complex expressions.

Regular expressions help you accomplish tasks such as validating email addresses, IP address
etc.

Why to use regular expressions

 Regular expressions simplify identifying patterns in string data by calling a single function. This
saves us coding time.
 When validating user input such as email address, domain names, telephone numbers, IP
addresses,
 Highlighting keywords in search results
 When creating a custom HTML template. Regular expressions can be used to identify the
template tags and replace them with actual data.

In this tutorial, you will learn-

 Regular expressions in PHP


 Preg_match
 Preg_split
 Preg_replace
 Meta characters
 Explaining the pattern

Regular expressions in PHP


PHP has built in functions that allow us to work with regular functions. Let’s now look at the
commonly used regular expression functions in PHP.

 preg_match – this function is used to perform a pattern match on a string. It returns true if a
match is found and false if a match is not found.
 preg_split – this function is used to perform a pattern match on a string and then split the
results into a numeric array
 preg_replace – this function is used to perform a pattern match on a string and then replace the
match with the specified text.

Below is the syntax for a regular expression function such as preg_match,preg_split or


preg_replace.

<?php
function_name('/pattern/',subject);
?>

HERE,

 "function_name(...)" is either preg_match, preg_split or preg_replace.


 "/.../" The forward slashes denote the beginning and end of our regular expression
 "'/pattern/'" is the pattern that we need to matched
 "subject" is the text string to be matched against

Let’s now look at practical examples that implement the above regular expression functions in
PHP.

PHP Preg_match
The first example uses the preg_match function to perform a simple pattern match for the word
guru in a given URL.

The code below shows the implementation for the above example.

<?php
$my_url = "www.guru99.com";
if (preg_match("/guru/", $my_url))
{
echo "the url $my_url contains guru";
}
else
{
echo "the url $my_url does not contain guru";
}
?>

Browse to the URL http://localhost/phptuts/preg_match_simple.php

Let’s examine the part of the code responsible for our output "preg_match('/guru/',
$my_url)" HERE,

 "preg_match(...)" is the PHP regular expression function


 "'/guru/'" is the regular expression pattern to be matched
 "$my_url" is the variable containing the text to be matched against.

The diagram below summarizes the above points

PHP Preg_split
Let’s now look at another example that uses the preg_split function.

We will take a string phrase and explode it into an array; the pattern to be matched is a single
space.

The text string to be used in this example is "I Love Regular Expressions".

The code below illustrates the implementation of the above example.

<?php

$my_text="I Love Regular Expressions";

$my_array = preg_split("/ /", $my_text);


print_r($my_array );

?>

Browse to the URL http://localhost/phptuts/preg_split.php

PHP Preg_replace
Let’s now look at the preg_replace function that performs a pattern match and then replaces the
pattern with something else.

The code below searches for the word guru in a string.

It replaces the word guru with the word guru surrounded by css code that highlights the
background colour.

<?php

$text = "We at Guru99 strive to make quality education affordable to the


masses. Guru99.com";

$text = preg_replace("/Guru/", '<span style="background:yellow">Guru</span>',


$text);

echo $text;

?>

Assuming you have saved the file preg_replace.php, browser to the URL
http://localhost/phptuts/preg_replace.php
Meta characters
The above examples used very basic patterns; metacharacters simply allow us to perform more
complex pattern matches such as test the validity of an email address. Let’s now look at the
commonly used metacharacters.

Metacharacter Description Example

Matches any single character except a new /./ matches anything that has a single
.
line character

Matches the beginning of or string / excludes /^PH/ matches any string that starts
^
characters with PH

/com$/ matches guru99.com,yahoo.com


$ Matches pattern at the end of the string
Etc.

/com*/ matches computer,


* Matches any zero (0) or more characters
communication etc.

Requires preceding character(s) appear at


+ /yah+oo/ matches yahoo
least once

/yahoo+\.com/ treats the dot as a literal


\ Used to escape meta characters
value

[...] Character class /[abc]/ matches abc


Metacharacter Description Example

a-z Matches lower case letters /a-z/ matches cool, happy etc.

A-Z Matches upper case letters /A-Z/ matches WHAT, HOW, WHY etc.

0-9 Matches any number between 0 and 9 /0-4/ matches 0,1,2,3,4

The above list only gives the most commonly used metacharacters in regular expressions.

Let’s now look at a fairly complex example that checks the validity of an email address.

<?php
$my_email = "name@company.com
";
if (preg_match("/^[a-zA-Z0-9._-]+@[a-zA-Z0-9-]+\.[a-zA-Z.]{2,5}$/",
$my_email)) {
echo "$my_email is a valid email address";
}
else
{
echo "$my_email is NOT a valid email address";
}
?>

Explaining the pattern "[/^[a-zA-Z0-9._-]+@[a-zA-Z0-9-


]+\.[a-zA-Z.]{2,5}$/]"
HERE,

 "'/.../'" starts and ends the regular expression


 "^[a-zA-Z0-9._-]" matches any lower or upper case letters, numbers between 0 and 9 and dots,
underscores or dashes.
 "+@[a-zA-Z0-9-]" matches the @ symbol followed by lower or upper case letters, numbers
between 0 and 9 or dashes.
 "+\.[a-zA-Z.]{2,5}$/" escapes the dot using the backslash then matches any lower or upper case
letters with a character length between 2 and 5 at the end of the string.

Browse to the URL http://localhost/phptuts/preg_match.php


As you can see from the above example breakdown, metacharacters are very powerful when it
comes to matching patterns.

 A regular expression is a pattern match algorithm


 Regular expressions are very useful when performing validation checks, creating HTML
template systems that recognize tags etc.
 PHP has built in functions namely preg_match,preg_split and preg_replace that support
regular expressions.
 Metacharacters allow us to create complex patterns

XML: Basic XML


XML tags are not predefined like HTML tags. HTML is a markup language whereas XML
provides a framework for defining markup languages.
...
XML | Basics

1. XML stands for eXtensible Markup Language.


2. XML is a markup language like HTML.
3. XML is designed to store and transport data.
4. XML is designed to be self-descriptive.

Differences between XML and HTML

XML and HTML were designed with different goals:

 XML is designed to carry data emphasizing on what type of data it is.


 HTML is designed to display data emphasizing on how data looks
 XML tags are not predefined like HTML tags.
 HTML is a markup language whereas XML provides a framework for defining markup languages.
 HTML is about displaying data,hence it is static whereas XML is about carrying
information,which makes it dynamic.
EXAMPLE :
XML code for a note is given below

HTML code for the note is given below

filter_none

brightness_4

<!DOCTYPE html>

<html>

<h1>Note</h1>

<body>

<p>To:RAJ

<br>

From:RAVI

</p>

<h1>Reminder</h1>

<p>Meeting at 8am</p>

</body>

</html>
OUTPUT :

Note : The output in both the cases is same but while using HTML we have used predefined tags
like p tag and h1 tag whereas while using XML we have used self defined tags like “To” tag and
“from” tag.

Another Example :
The XML above is quite self-descriptive:

 It has sender information.


 It has receiver information.
 It has a heading.
 It has a message body.
 The tags in the example below are not defined in any XML standard. These tags are “invented”
by the author of the XML document.HTML works with predefined tags like p tag, h1 tag,
etc.While in XML, the author must define both the tags and the document structure.

Input :
Output :

Basically XML above does not do anything. XML is just information wrapped in tags. User must
require a piece of software to send, receive, store, or display it.

XML makes web development User Friendly : Many computer systems contain data in
incompatible formats. Exchanging data between incompatible systems or upgraded systems is a
time-consuming task for web developers. Large amounts of data must be converted, and
incompatible data is often lost. XML stores data in plain text format. This provides a software-
and hardware-independent way of storing, transporting, and sharing data.

XML is Extensible : XML applications will work as expected even if data is edited i.e. added or
removed. Example :The above note is edited into a newer version by adding date tag and hour
tag , and by removing the heading tag.
Previous version
New Version

Document Type Definition XML Schema

What is an XML Schema?


An XML Schema describes the structure of an XML document.

The XML Schema language is also referred to as XML Schema Definition (XSD).

XSD Example
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="note">
<xs:complexType>
<xs:sequence>
<xs:element name="to" type="xs:string"/>
<xs:element name="from" type="xs:string"/>
<xs:element name="heading" type="xs:string"/>
<xs:element name="body" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>

</xs:schema>

The purpose of an XML Schema is to define the legal building blocks of an XML document:

 the elements and attributes that can appear in a document


 the number of (and order of) child elements
 data types for elements and attributes
 default and fixed values for elements and attributes

Why Learn XML Schema?


In the XML world, hundreds of standardized XML formats are in daily use.

Many of these XML standards are defined by XML Schemas.

XML Schema is an XML-based (and more powerful) alternative to DTD.

XML Schemas Support Data Types


One of the greatest strength of XML Schemas is the support for data types.

 It is easier to describe allowable document content


 It is easier to validate the correctness of data
 It is easier to define data facets (restrictions on data)
 It is easier to define data patterns (data formats)
 It is easier to convert data between different data types

XML Schemas use XML Syntax


Another great strength about XML Schemas is that they are written in XML.

 You don't have to learn a new language


 You can use your XML editor to edit your Schema files
 You can use your XML parser to parse your Schema files
 You can manipulate your Schema with the XML DOM
 You can transform your Schema with XSLT

XML Schemas are extensible, because they are written in XML.

With an extensible Schema definition you can:

 Reuse your Schema in other Schemas


 Create your own data types derived from the standard types
 Reference multiple schemas in the same document

XML Schemas Secure Data Communication


When sending data from a sender to a receiver, it is essential that both parts have the same
"expectations" about the content.

With XML Schemas, the sender can describe the data in a way that the receiver will understand.

A date like: "03-11-2004" will, in some countries, be interpreted as 3.November and in other
countries as 11.March.

However, an XML element with a data type like this:

<date type="date">2004-03-11</date>

ensures a mutual understanding of the content, because the XML data type "date" requires the
format "YYYY-MM-DD".

Well-Formed is Not Enough


A well-formed XML document is a document that conforms to the XML syntax rules, like:

 it must begin with the XML declaration


 it must have one unique root element
 start-tags must have matching end-tags
 elements are case sensitive
 all elements must be closed
 all elements must be properly nested
 all attribute values must be quoted
 entities must be used for special characters

Even if documents are well-formed they can still contain errors, and those errors can have
serious consequences.

Think of the following situation: you order 5 gross of laser printers, instead of 5 laser printers.
With XML Schemas, most of these errors can be caught by your validating software.

What is a DTD?
A DTD is a Document Type Definition.

A DTD defines the structure and the legal elements and attributes of an XML document.

Why Use a DTD?


With a DTD, independent groups of people can agree on a standard DTD for interchanging data.

An application can use a DTD to verify that XML data is valid.

An Internal DTD Declaration


If the DTD is declared inside the XML file, it must be wrapped inside the <!DOCTYPE>
definition:

XML document with an internal DTD


<?xml version="1.0"?>
<!DOCTYPE note [
<!ELEMENT note (to,from,heading,body)>
<!ELEMENT to (#PCDATA)>
<!ELEMENT from (#PCDATA)>
<!ELEMENT heading (#PCDATA)>
<!ELEMENT body (#PCDATA)>
]>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend</body>
</note>

Output:-
Tove
Jani
Reminder
Don't forget me this weekend!
The DTD above is interpreted like this:

 !DOCTYPE note defines that the root element of this document is note
 !ELEMENT note defines that the note element must contain four elements:
"to,from,heading,body"
 !ELEMENT to defines the to element to be of type "#PCDATA"
 !ELEMENT from defines the from element to be of type "#PCDATA"
 !ELEMENT heading defines the heading element to be of type "#PCDATA"
 !ELEMENT body defines the body element to be of type "#PCDATA"

An External DTD Declaration


If the DTD is declared in an external file, the <!DOCTYPE> definition must contain a reference
to the DTD file:

XML document with a reference to an external DTD


<?xml version="1.0"?>
<!DOCTYPE note SYSTEM "note.dtd">
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>

And here is the file "note.dtd", which contains the DTD:

<!ELEMENT note (to,from,heading,body)>


<!ELEMENT to (#PCDATA)>
<!ELEMENT from (#PCDATA)>
<!ELEMENT heading (#PCDATA)>
<!ELEMENT body (#PCDATA)>

Output:-
Tove
Jani
Reminder
Don't forget me this weekend!

DOM and Presenting XML

What is the DOM?


The DOM defines a standard for accessing and manipulating documents:

"The W3C Document Object Model (DOM) is a platform and language-neutral interface that
allows programs and scripts to dynamically access and update the content, structure, and style
of a document."

The HTML DOM defines a standard way for accessing and manipulating HTML documents. It
presents an HTML document as a tree-structure.

The XML DOM defines a standard way for accessing and manipulating XML documents. It
presents an XML document as a tree-structure.

Understanding the DOM is a must for anyone working with HTML or XML.

The HTML DOM


All HTML elements can be accessed through the HTML DOM.

This example changes the value of an HTML element with id="demo":

Example
<!DOCTYPE html>

<html>

<body>

<h1 id="demo">This is a Heading</h1>

<p>This is a paragraph.</p>

<script>

document.getElementById("demo").innerHTML = "Hello World!";

</script>

</body>

</html>

Output:-

Hello World!

This is a paragraph.

The XML DOM


All XML elements can be accessed through the XML DOM.

The XML DOM is:

 A standard object model for XML


 A standard programming interface for XML
 Platform- and language-independent
 A W3C standard

In other words: The XML DOM is a standard for how to get, change, add, or delete XML
elements.
XML Parsers and Validation

XML Validation is a process by which an XML document is validated. An XML document is


said to be valid if its contents match with the elements, attributes and associated document type
declaration(DTD), and if the document complies with the constraints expressed in it. Validation
is dealt in two ways by the XML parser. They are −

 Well-formed XML document


 Valid XML document

Well-formed XML Document


An XML document is said to be well-formed if it adheres to the following rules −

 Non DTD XML files must use the predefined character entities for amp(&), apos(single
quote), gt(>), lt(<), quot(double quote).
 It must follow the ordering of the tag. i.e., the inner tag must be closed before closing the
outer tag.
 Each of its opening tags must have a closing tag or it must be a self ending
tag.(<title>....</title> or <title/>).
 It must have only one attribute in a start tag, which needs to be quoted.
 amp(&), apos(single quote), gt(>), lt(<), quot(double quote) entities other than these
must be declared.

Example

Following is an example of a well-formed XML document −

<?xml version = "1.0" encoding = "UTF-8" standalone = "yes" ?>


<!DOCTYPE address
[
<!ELEMENT address (name,company,phone)>
<!ELEMENT name (#PCDATA)>
<!ELEMENT company (#PCDATA)>
<!ELEMENT phone (#PCDATA)>
]>

<address>
<name>Tanmay Patil</name>
<company>TutorialsPoint</company>
<phone>(011) 123-4567</phone>
</address>

The above example is said to be well-formed as −

 It defines the type of document. Here, the document type is element type.
 It includes a root element named as address.
 Each of the child elements among name, company and phone is enclosed in its self
explanatory tag.
 Order of the tags is maintained.

Valid XML Document


If an XML document is well-formed and has an associated Document Type Declaration (DTD),
then it is said to be a valid XML document.

XML parser is a software library or a package that provides interface for client
applications to work with XML documents. It checks for proper format of the XML document
and may also validate the XML documents. Modern day browsers have built-in XML parsers.

Following diagram shows how XML parser interacts with XML document −

The goal of a parser is to transform XML into a readable code.

To ease the process of parsing, some commercial products are available that facilitate the
breakdown of XML document and yield more reliable results.

Some commonly used parsers are listed below −

 MSXML (Microsoft Core XML Services) − This is a standard set of XML tools from
Microsoft that includes a parser.
 System.Xml.XmlDocument − This class is part of .NET library, which contains a
number of different classes related to working with XML.
 Java built-in parser − The Java library has its own parser. The library is designed such
that you can replace the built-in parser with an external implementation such as Xerces
from Apache or Saxon.
 Saxon − Saxon offers tools for parsing, transforming, and querying XML.
 Xerces − Xerces is implemented in Java and is developed by the famous open source
Apache Software Foundation.

XSL and XSLT Transformation


XSL
Before learning XSLT, we should first understand XSL which stands for EXtensible Stylesheet
Language. It is similar to XML as CSS is to HTML.

Need for XSL

In case of HTML document, tags are predefined such as table, div, and span; and the browser
knows how to add style to them and display those using CSS styles. But in case of XML
documents, tags are not predefined. In order to understand and style an XML document, World
Wide Web Consortium (W3C) developed XSL which can act as XML based Stylesheet
Language. An XSL document specifies how a browser should render an XML document.

Following are the main parts of XSL −

 XSLT − used to transform XML document into various other types of document.
 XPath − used to navigate XML document.
 XSL-FO − used to format XML document.

What is XSLT
XSLT, Extensible Stylesheet Language Transformations, provides the ability to transform XML
data from one format to another automatically.

How XSLT Works

An XSLT stylesheet is used to define the transformation rules to be applied on the target XML
document. XSLT stylesheet is written in XML format. XSLT Processor takes the XSLT
stylesheet and applies the transformation rules on the target XML document and then it generates
a formatted document in the form of XML, HTML, or text format. This formatted document is
then utilized by XSLT formatter to generate the actual output which is to be displayed to the end-
user.
Advantages

Here are the advantages of using XSLT −

 Independent of programming. Transformations are written in a separate xsl file which is


again an XML document.
 Output can be altered by simply modifying the transformations in xsl file. No need to
change any code. So Web designers can edit the stylesheet and can see the change in the
output quickly.

News Feed (RSS and ATOM)


RSS is an open method for delivering regularly changing web content. Many news-related sites,
weblogs, and other online publishers syndicate their content as an RSS Feed to whoever wants it.

Any time you want to retrieve the latest headlines from your favorite sites, you can access the
available RSS Feeds via a desktop RSS reader. You can also make an RSS Feed for your own
site if your content changes frequently.

In brief:

 RSS is a protocol that provides an open method of syndicating and aggregating web
content.
 RSS is a standard for publishing regular updates to web-based content.
 RSS is a Syndication Standard based on a type of XML file that resides on an Internet
server.
 RSS is an XML application, which conforms to the W3C's RDF specification and is
extensible via XML.
 You can also download RSS Feeds from other sites to display the updated news items on
your site, or use a desktop or online reader to access your favorite RSS Feeds.

What does RSS stand for? It depends on what version of RSS you are using.

 RSS Version 0.9 - Rich Site Summary


 RSS Version 1.0 - RDF Site Summary
 RSS Versions 2.0, 2.0.1, and 0.9x - Really Simple Syndication

What is RSS Feed?


 RSS Feed is a text XML file that resides on an Internet server.
 An RSS Feed file includes the basic information about a site (title, URL, description),
plus one or more item entries that include - at a minimum - a title (headline), a URL, and
a brief description of the linked content.
 There are various flavors of RSS Feed depending on RSS Version. Another XML Feed
format is called ATOM.
 RSS Feeds are registered with an RSS registry to make them more available to viewers
interested in your content area.
 RSS Feeds can have links back to your website, which will result in a high traffic to your
site.
 RSS Feeds are updated hourly (Associated Press and News Groups), some RSS Feeds are
updated daily, and others are updated weekly or irregularly.

How Does RSS Work?


This is how RSS works:

 A website willing to publish its content using RSS creates one RSS Feed and keeps it on
a web server. RSS Feeds can be created manually or with software.
 A website visitor will subscribe to read your RSS Feed. An RSS Feed will be read by an
RSS Feed reader.
 The RSS Feed Reader reads the RSS Feed file and displays it. The RSS Reader displays
only new items from the RSS Feed.
 The RSS Feed reader can be customized to show you content related to one or more RSS
Feeds and based on your own interest.

News Aggregators and Feed Readers


RSS Feed readers and news aggregators are essentially the same thing; they are a piece of
software. Both are used for viewing RSS Feeds. News aggregators are designed specifically to
view news-related Feeds but technically, they can read any Feeds.

Who can Use RSS?


RSS started out with the intent of distributing news-related headlines. The potential for RSS is
significantly larger and can be used anywhere in the world.

Consider using RSS for the following:

 New Homes - Realtors can provide updated Feeds of new home listings on the market.
 Job Openings - Placement firms and newspapers can provide a classified Feed of job
vacancies.
 Auction Items - Auction vendors can provide Feeds containing items that have been
recently added to eBay or other auction sites.
 Press Distribution - Listing of new releases.
 Schools - Schools can relay homework assignments and quickly announce school
cancellations.
 News & Announcements - Headlines, notices, and any list of announcements.
 Entertainment - Listings of the latest TV programs or movies at local theatres.

RSS is growing in popularity. The reason is fairly simple. RSS is a free and easy way to promote
a site and its content without the need to advertise or create complicated content sharing
partnerships.

RSS - Advantages

RSS is taking off so quickly because people are liking it. RSS is easy to use and it has
advantages for a publisher as well as for a subscriber. Here we have listed out a few advantages
of RSS for subscribers as well as for publishers.

Advantages for Subscribers


RSS subscribers are the people who subscribe to read a published Feed. Here are some of the
advantages of RSS Feeds for subscribers:

 All news at one place: You can subscribe to multiple news groups and then you can
customize your reader to have all the news on a single page. It will save you a lot of time.
 News when you want it: Rather than waiting for an e-mail, you go to your RSS reader
when you want to read a news. Furthermore, RSS Feeds display more quickly than
information on web-sites, and you can read them offline if you prefer.
 Get the news you want: RSS Feed comes in the form of headlines and a brief
description so that you can easily scan the headlines and click only those stories that
interest you.
 Freedom from e-mail overload: You are not going to get any email for any news or
blog update. You just go to your reader and you will find updated news or blog
automatically whenever there is a change on the RSS server.
 Easy republishing: You may be both a subscriber and a publisher. For example, you
may have a web-site that collects news from various other sites and then republishes it.
RSS allows you to easily capture that news and display it on your site.

Advantages for Publishers


RSS publishers are the people who publish their content through RSS feed. We would suggest
you to use RSS:

 if you want to get your message out and easily,


 if you want people to see what you publish, and
 if you want your news to bring people back to your site.

Here are some of the advantages of RSS if you publish on the Web:

 Easier publishing: RSS is really simple publishing. You don't have to maintain a
database of subscribers to send your information to them, instead they will access your
Feed using a reader and will get updated content automatically.
 A simpler writing process: If you have a new content on your web site, you only need to
write an RSS Feed in the form of titles and short descriptions, and link back to your site.
 An improved relationship with your subscribers: Because people subscribe from their
side, they don't feel as if you are pushing your content on them.
 The assurance of reaching your subscribers: RSS is not subject to spam filters, your
subscribers get the Feeds, which they subscribe to and nothing more.
 Links back to your site: RSS Feeds always include links back to a website. It directs a
lot of traffic towards your website.
 Relevance and timeliness: Your subscribers always have the latest information from
your site.

RSS - Version History

RSS was first invented by Netscape. They wanted to use an XML format to distribute news,
stories, and information. Netscape refined the version of RSS and then dropped it.

Later Userland Software started controlling RSS specifications and releasing newer RSS
versions. They continued development of their own version of RSS and eventually UserLand
released RSS v2.

RSS has been released in many different versions.


 12/27/97 - Dave Winer at Userland developed scriptingNews. RSS was born.
 3/15/99 - Netscape developed RSS 0.90 (which supported scriptingNews). This was
simply XML with an RDF Header and it was used for my.netscape.com.
 6/15/99 - Dave Winer at UserLand develops scriptingNews 2.0b1, which included
Netscape's RSS 0.90 features also.
 7/10/99 - Netscape developed RSS 0.91. In this version they removed the RDF header,
but included most features from scriptingNews 2.0b1.
 7/28/99 - UserLand deprecated scriptingNews formats and adopted only RSS 0.91.
 Netscape stops their RSS development.
 6/4/00 - UserLand releases the official RSS 0.91 specification.
 8/14/00 - A group led by Rael Dornfest at O'Reilly, developed RSS 1.0. This format uses
RDF and namespaces. This version is often confused as being a new version of 0.91, but
this is a completely new format with no ties to RSS 0.91
 12/25/00 - Dave Winer at UserLand develops RSS 0.92, which is 0.91 with optional
elements.
 04/20/01 - RSS0.93 was discussed but never deployed.
 03/14/02 - MetaWeblog API merged RSS 0.92 with XML-RPC to provide a powerful
blogging API.
 09/18/02 - Dave Winer developed RSS 2.0 after leaving Userland. This is 0.92 with
optional elements. MetaWeblog API updated for RSS 2.0. While in development, this
format was called 0.94.
 07/15/03 - Official Spec RSS 2.0 was released through Harvard under a Creative
Commons license.

Which RSS Version Should be Used?


There is no consensus on using RSS Feed version. It's up to you, which version you want to use.
We would suggest to use the latest one, which is RSS2.0. This is simple enough to use and easy
to learn.

 About 50 % of all RSS Feeds use RSS 0.91.


 About 25 % use RSS 1.0.
 The last 25 % is split between RSS 0.9x versions and RSS 2.0.

We will see Feed formats for these versions in the next chapter.

RSS - Feed Formats

RSS has been released in many different versions in the last 10 years. Here we will give you
detail about three most commonly used RSS version.

RSS v0.91 Feed Format


 RSS v0.91 was originally released by Netscape in 1999.
 RSS v0.91 does not have RDF header.
 RSS v0.91 is called Rich Site Summary (RSS).
 RSS v0.91 has features from Dave Winer's RSS version scriptingNews 2.0b1.
 RSS v0.91 has support for international languages and encodings.
 RSS v0.91 has support for image height and width definitions.
 RSS v0.91 has support for description text for headlines.
 Check complete set of - RSS v0.91 tags and syntax

RSS v1.0 Feed Format


 RSS 1.0 is the only version that was developed using the W3C RDF (Resource
Description Framework) standard. This version of RSS is called RDF Site Summary.
 RSS 0.91 and RSS 2.0 are easier to understand than RSS 1.0.
 Check complete set of - RSS v1.0 tags and syntax

RSS v2.0/2.01 Feed Format:


 RSS 2.0/2.01 is very similar to RSS 0.9x. RSS 2.0/2.01 adds namespace modules and six
optional elements to RSS 0.9x.
 RSS 2.0/2.01 specification was written by Dave Winer of Radio UserLand. The copyright
was later transferred to Harvard University.
 Check complete set of - RSS v2.0 tags and syntax

RSS - Reading Feeds

Many sites offer RSS Feeds, which you can identify by a small yellow button that says either
or . However, if you click one of these links, you will most likely get a page full of
code in your browser.

To properly read the Feed, you need an RSS reader. Here are the steps to get and use RSS Feed
readers.

Step 1 - Get an RSS Feed Reader


There are a lot of different RSS readers available. Some work as web services, and some are
limited to windows (or Mac, PDA or UNIX). Here are a few, which you can try:

 NewsGator Online - A free online RSS reader. Includes synchronization with Outlook,
viewing TV content with Media Center Edition, and publication of blogs and headlines.
 RssReader - A free Windows-based RSS reader. Supports RSS versions 0.9x, 1.0, and
2.0, and Atom 0.1, 0.2, and 0.3.
 FeedDemon - A Windows-based RSS reader. Very easy to use and has a very orderly
interface. However, this is not freeware!
 blogbot - An RSS reader plug-in for Outlook or Internet Explorer. The light-version for
Internet Explorer is free.

Step 2 - RSS Reader Installation


All the readers come along with installation instructions. So, use the provided script to install
your RSS Reader on your computer.

When you first launch a standalone reader, most often, you will see a toolbar and three window
panes arranged much like the preview mode in Microsoft Outlook. The pane on the left side
typically displays the RSS Feeds, or channels, to which you are subscribed. These can be
organized into categories or folders.

The upper-right panel typically shows a list of articles within whichever channel is selected, and
the article content is then displayed in the lower-right panel. To change channel groups, just click
the drop-down box at the upper left beneath the menus. Sometimes a brief description will
appear in the lower right; if so, click the link in the article to load the complete text.

Some standalone apps can be configured to send you e-mail every time there's a new article on a
topic you're interested in.

Step 3 - Add Channels and Channel Groups


To add a channel i.e., RSS Feed, go to the RSS page of any site using the yellow button that says
either or . Right-click or use CTRL+C to copy the URL from the address bar of your
browser, which should show a page full of XML code.

Now go back to your newsreader, choose the category where you want the new subscription to
live (Business, Entertainment, the New York Times), and select New or New Channel from the
File menu. In most cases, the URL you copied should automatically be pasted into the URL field
in the New Channel wizard. If not, you can cut and paste the URL yourself.

Step 4 - Customize RSS Reader


When you accumulate lots of articles from your various Feeds, it can become difficult to find
specific information. Fortunately, newsreaders include useful tools for finding articles.

A Filter tool will show only articles that contain a keyword you specify. This may also be labeled
Search. To use it, type a keyword directly into the Filter/Search bar.

Some readers include the ability to set a watch, an automatic search through all your incoming
Feeds for a specific keyword. For example, you could enter ICQ as a watch. If any article in any
Feed you subscribe to mentions ICQ, the article will be included in the Watch list.
You need to check the help section of your reader to find out more options to customize it
according to your needs.

Step 5 - Cleaning Unwanted Feeds


Eventually, you'll probably end up with more Feeds than you want or can read regularly. In most
readers, to delete a Feed you're no longer interested in, you simply delete its title. Then your RSS
reader won't seek out that information anymore, and you won't get any content from the
publisher unless you go to its site or resubscribe to the Feed.

RSS - Feed Publishing

Now you are aware how to write an RSS Feed for your site. If you don't know how to prepare
RSS Feed file, then please go through the RSS Feed Formats chapter.

Uploading an RSS Feed


Here are the simple steps to put your RSS Feed on the web.

 First decide which version of RSS Feed you are going to use for your site. We would
recommend you to use the latest version available.
 Create your RSS Feed in a text file with extension either .xml or .rdf. Upload this file on
your web server.
 You should validate your RSS Feed before making it live. Check the next chapter on RSS
Feed Validation.
 Create a link on your Web Pages for the RSS Feed file. You will use a small yellow
button for the link that says either or .

Now, your RSS Feed is online and people can start using it. But there are ways to promote your
RSS Feed so that more number of people can use your RSS Feed.

Promote Your RSS Feed


 Submit your RSS Feed to the RSS Feed Directories. There are many directories available
on the web, where you can register your Feed. Some of them are given here:
o Syndic8: Over 300,000 Feeds listed.
o Daypop: Over 50,000 feeds listed.
o Newsisfree: Over 18,000 Feeds.
 Register your Feed with the major search engines. Similar to your web pages, you can
add your Feed as well with the following major search engines.
o Yahoo - http://publisher.yahoo.com/promote.php
o Google - http://www.google.com/webmasters/add.html
o MSN - http://rss.msn.com/publisher.armx
Keeping Up-To-Date Feed
As we have explained earlier, RSS Feed makes sense for the site which are changing their
content very frequently, for example, any news or blogging sites.

So now, you have got RSS Feed buttons from Google, Yahoo, and MSN. You must make sure to
update your content frequently and that your RSS Feed is constantly available.

RSS - Feed Validation & Validators

If you have created one RSS Feed for your news group or web blog or for any other purpose,
then it is your responsibility to ensure that your RSS Feed file can be parsed by the XML parser
of any subscribing site.

Many of the RSS Feed creation softwares validate XML at the time of Feed creation but some
don't. Make a note that small errors can make your Feed unreadable by the standard Feed
readers.

So we would suggest you to make sure you have done all the required validations before
publishing your RSS Feed. You may wish to load your RSS Feed file to your internet server and
then enter the URL in one of the following validators to check the syntax.

 Feed Validator - This validator validates multiple syndication formats: RSS 0.90, 0.91,
0.92, 0.93, 0.94, 1.0, 1.1, and 2.0. It includes validation for common namespaces.
 RSS Validator - If you are using RSS 0.91 or RSS0.92, then you can use this validator to
validate your RSS Feed.
 Experimental Online RSS 1.0, Validator - If you are using RSS 1.0, then you can use this
validator.
 Redland RSS 1.0 Validator and Viewer - This is not just a validator, but also it acts as an
RSS to HTML converter.

What is Atom 1.0

Atom is the name of an XML-based Web content and metadata syndication format, and an
application-level protocol for publishing and editing Web resources belonging to periodically
updated websites.

Atom is a relatively recent spec and is much more robust and feature-rich than RSS. For
instance, where RSS requires descriptive fields such as title and link only in item breakdowns,
Atom requires these things for both items and the full Feed.

All Atom Feeds must be well-formed XML documents, and are identified with the
application/atom+xml media type.
Structure of an Atom 1.0 Feed
A Feed consists of some metadata, followed by any number of entries. Here is a basic structure
of an Atom 1.0 Feed.

<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<title>...</title>
<link>...</link>
<updated>...</updated>

<author>
<name>...</name>
</author>

<id>...</id>

<entry>
<title>...</title>
<link>...</link>
<id>...</id>

<updated>...</updated>
<summary>...</summary>
</entry>

</feed>

Atom 1.0 Feed Tags


An Atom 1.0 Feed Document will be constructed of the following two elements:

 <feed> Elements
 <entry> Elements

There are some common construct, which are required for the above two elements and they are
explained in: Common Construct.

RSS - Further Extensions

RSS originated in 1999, and has strived to be a simple, easy to understand format, with relatively
modest goals. After it became a popular format, developers wanted to extend it using modules
defined in namespaces, as specified by the W3C.

RSS 2.0 adds that capability, following a simple rule. An RSS Feed may contain elements not
described on this page, only if those elements are defined in a namespace.
The elements defined in this tutorial are not themselves members of a namespace, so that RSS
2.0 can remain compatible with the previous versions in the following sense - a version 0.91 or
0.92 file is also a valid 2.0 file. If the elements of RSS 2.0 were in a namespace, this constraint
would break, a version 0.9x file would not be a valid 2.0 file.

RSS is by no means a perfect format, but it is very popular and widely supported. Having a
settled spec is something RSS has needed for a long time.

However, the RSS spec is, for all practical purposes, frozen at version 2.0.1. But you can
anticipate the possible 2.0.2 or 2.0.3 versions, etc. only for the purpose of clarifying the
specification, not for adding new features to the format.

Subsequent work should happen in modules, using namespaces, and in completely new
syndication formats, with new names.

Вам также может понравиться