Web Development
Web Development
Internet
The global system of interconnected computer networks with billions of
computers and other electronic devices.
Internet use the protocol suite TCP/IP (Transmission Control Protocol/Internet
Protocol) to communicate with these devices.
Internet can also be defined as a network of networks that consists of private,
public, academic, business, and government networks of local to global scope.
These networks are connected by a broad array of electronic, wireless, and optical
networking technologies.
Internet provides number of services
World Wide Web (WWW)
Email
Video conferencing
File sharing
VoIP
Online banking
The Internet has no centralized governance, each connected network sets its own
policies.
Commercial websites
Designed to provide an online space for buying and selling of products or
services directly through the website.
Ex. [Link], [Link]
Educational websites
Designed to provide an online space for collection of materials and
resources for learners and educators.
Ex. [Link], [Link]
Community websites
Designed to provide an online platform for people to build social networks
or social relations.
Ex. [Link], [Link]
Web portal
Designed to bring information from various sources, like emails, online
forums and search engines, together in a uniform way.
Ex. [Link], [Link]
Structure of a Website
A website is a collection of related web pages.
Basic pages of a website
Home Page
The first page on a website that is being accessed by a visitor.
It is the initial or the main web page of a website.
Used to provide a summary of the content or the information the website
offers.
Used to facilitate navigation to other pages on the site by providing links.
About
Used to provide a description or an overview of website owners.
Contact page
Used to provide contact information for visitors to contact website owners.
Products/Services
Used to provide information about all of the products and services website
offers for its visitors.
Closing tag
Opening tag
(Starts with a forward slash)
Tags can have Attributes.
Automatically refresh the current web page after a given time interval in
seconds.
Ex. <head>
<meta http-equiv="refresh" content="60">
</head>
Setting the Viewport for different devices
Ex. <head>
<meta name="viewport" content="width=device-width, initial-
scale=1.0">
</head>
Text formatting
Bold text
<b> is used to display text in boldface.
Ex.
<body>
<b>This text is bold</b>
</body>
Italic text
<i> is used to display text in italics.
Ex.
<body>
<i>This text is italic</i>
</body>
Underlined text
<u> is used to underline text in a document.
Ex.
<body>
<u>This text is underlined</u>
</body>
Fonts
The <font> tag specifies the font face, font size, and color of text.
Ex.
<body>
<font face="Arial" color="green" size = “5”>Hello World!</font>
</body>
Hyperlinks
The <a> tag defines a hyperlink, a link from one page to another.
The “href” attribute of <a> indicates the URL of the destination.
Ex.
<body>
<a href=”[Link] Devi Balika Vidyalaya website
</a>
</body>
The “target” specifies where to open the linked document.
Ex.
<a href=”[Link] target = "_blank">Open Devi Balika
Vidyalaya website in a new tab</a>
Ex.
<body>
<img src=" D:\Pictures\funny_cat.jpg " alt="A Funny Cat"
</body>
“width and height” attributes specify the width and height values in pixels to
display the image.
Ex.
<body>
<img src = "D:\Pictures\funny_cat.jpg" alt="A Funny Cat" width="256"
height="192" />
</body>
“align” attribute can be used to right, left or wrap text around the image.
The <img> tag is an empty tag.
Tables
<table> tag can be used to insert a table in to a HTML document.
A table can be divided into rows with <tr> tag.
Each row can be divided into data cells with <td> tag.
Headings in a table can be defined with <th> tag.
The text in <th> elements are bold and centered by default.
Attribute “border” specifies the size of border thickness.
Ex. A table with one column.
<table border="1">
<tr>
<td>A</td>
</tr>
</table>
<table border="1">
<tr>
<th>Student No</th>
<th>Student Name</th>
<th>Class</th>
</tr>
<tr>
<td>001</td>
<td>Peter Pan</td>
<td>10C</td>
</tr>
<tr>
<td>002</td>
<td> Jack Sparrow </td>
<td>10C</td>
</tr>
</table>
Cell merging
Attributes “rowspan” and “colspan” can be used to merge cells in a HTML
table.
The colspan attribute defines the number of columns a cell should span (or
merge) horizontally.
The rowspan attribute specifies the number of rows a cell should span
vertically.
After Colspan
Merged
After Rowspan
First Cell
1. Unordered list
An unordered list is used when the order of the list items does not matter.
It starts with the <ul> tag and ends with </ul>.
Each item is listed between <li> and </li> tags.
The “type” attribute of <ul> tag can be used to specify the preferred bullet
type (disc, circle, square).
Ex. <ul type = “circle”>
2. Ordered list
Used to group a set of related items, in a specific order.
Created using the <ol> tag, with each list item starts with the <li> tag.
The “type” attribute can be used to specify the preferred numbering format
o Numbering (1)
o Uppercase letters (A)
o Lowercase letters (a)
o Uppercase roman (I)
o Lowercase roman (i)
Ex. <ol type = “A”>
The “start” attribute of <ol> tag can be used to specify the starting point of
numbering.
Ex. To continue an ordered list from 3 with in a new paragraph
<ol start = “3”>
3. Definition list
A definition list is used to create a list of items with a description of each
item.
Tags used in definition list
<dl> − Defines as a definition list
<dt> − List term/ item
<dd>− Term definition
Ex. <dl>
<dt>Internet</dt>
<dd>Network of networks </dd>
<dt>WWW</dt>
<dd> The part of the Internet that contains web pages </dd>
<dt>HTML</dt>
<dd>A markup language for creating web pages</dd>
</dl>
Internet
Network of networks
WWW
The part of the Internet that contains web pages
HTML
A markup language for creating web pages
Nested Lists
A nested list is a list within a list.
Different types of list can be used to create a nested list.
Ex.
<h3>Most popular beverages:</h3>
<ol>
<li>Tea
<ul>
<li>Black Tea</li>
<li>Green Tea</li>
</ul> </li>
<li>Coffee</li>
<li>Milk</li>
</ol>
1. Tea
Black Tea
Green Tea
2. Coffee
3. Milk
Multimedia objects
Using <a> tag
Ex. Insert an audio file
<a href=" Songs\song.mp3"> An Audio Clip </a><br />
Insert a video file
<a href="Videos\video.mp4"> A Video Clip </a><br/>
<embed> tag can also be used to insert multimedia objects like audio, video and
animation to a web page.
The multimedia file will be automatically embedded to the page when it is getting loaded.
Attributes of <embed>
Src - URL of the object to be embedded
Width - Width of the object in pixels
Height - Height of the object in pixels
Name
Defines a name to identify the data collected by the element.
Value
Defines input element’s current/initial value.
Available types for input element
Text - A single-line text field
Ex.
Student name: <input type="text" name="studentName">
Value for name attribute should be the same for each radio button.
Checked attribute is used to specify a pre-selected value to be loaded.
Checkbox - A check box, allows more than one option to be selected.
Ex.
Sports you like:
<input type="checkbox" name="sports" value="cricket"> Cricket
<input type="checkbox" name="sports" value="rugby"> Rugby
<input type="checkbox" name="sports" value="basketball"> Basketball
Reset - A button that resets the contents of the form to default values.
Ex.
<input type="reset">
Select element
Used to define a drop-down list in an HTML form.
The <option> element is used to define a pre-defined option that can be selected.
Ex.
Country:
<select name="country">
<option value="SriLanka"> Sri Lanka </option>
<option value="India" > India </option>
<option value="Japan"> Japan </option>
</select>
Row and cols attributes indicate number of rows and columns of text area
box.
Fieldset element
Used to group related elements in a form.
<legend> element can be used to define a caption for the grouped elements.
Ex.
<fieldset>
<legend>Log in:</legend>
User Name: <input type="text">
Password: <input type="password">
</fieldset>
CSS - Cascading Style Sheets
CSS is a design language used to simplify the process of web page presentation.
CSS defines how to display HTML elements.
Advantages of using CSS:
Separation of style from the content
CSS removes the style formatting from the HTML document.
Easy maintenance
One place of change for the entire site.
More formatting options than HTML
Wider range of style attributes
Multiple presentation
Allows the same markup page to be presented in different viewing
styles or on different devices (printing, mobile devices)
Faster rendering
Pages load faster as CSS optimizes the source code.
Ex.
Selector – Paragraph tag
Property – Color
Value - Blue
p { color : blue; }
Id selector is used to identify one specific element and should be unique within an HTML
document.
Class Selector
Selects a specific element by using the “class” attribute of an HTML element.
All the elements having the matching “class” will be formatted.
A class selector is preceded by a full stop (“.”)
Ex.
.text {
color: blue;
text-align: center;
}
<h1 class = "text"> this h1 heading will be blue and center-aligned.</h1>
<p class = "text"> this paragraph will be blue and center-aligned.</p>
1. In-line method
Uses attribute “style” of an HTML element to insert styles.
Styles will be applied to a specific element only.
The style attribute can contain any CSS property.
Ex.
<p style = " font-family: Arial; color: red;"> This in-line CSS will make
this specific paragraph in Arial and red.</p>
2. Internal method
Uses <style> element to insert styles to an HTML document.
Internal styles are defined within the Head section.
Styles will be applied to all the elements in the document.
Can be used when a whole page has a unique style.
Ex.
<head>
<style>
body {
background-color: #808000;
}
h1 {
font-family: Times New Roman;
color: maroon;
}
</style>
</head>
in this case, defined styles will be applied to all the <h1> headings in the
HTML document.
3. External method
Links an external style sheet to an HTML document.
An external style sheet is a separate file with .css extension.
Uses HTML <link> element to insert the external style sheet.
The link is placed within the Head section of the document.
Ex.
<head>
<link rel= "stylesheet" type = "text/css"
href = "[Link]" >
</head>
[Link]
body {
background-color: #808000;
h1 {
color: maroon;
}
Attributes of <link>
rel - Specifies the relationship between the document and
the linked resource.
type - Specifies the MIME (Multipurpose Internet Mail
Extensions) type
href - Specifies the URL of the resource.
media – An optional attribute to Specify the device the
document will be displayed on. (Screen, Print)
</head>
<body>
<h1 style= "color: green;">This heading will be GREEN in color. </h1>
</body>
</html>
Appearance formatting
Background
Colors
Colors can be applied to an HTML document by using CSS properties.
color : set a text color
Ex. p { color : blue;}
Images
The background-image property can be used to insert an image to the page
background.
Ex. body { background-image: url ("[Link]");}
Text formatting
Text Alignment
The text-align property is used to set the alignment of a text to left, right,
centered, or justified.
Ex. h1 { text-align: center;}
Text Decoration
The text-decoration property is used to set or remove decorations from
text.
It can be used to put underline, over line, line-through a text or to remove
text decorations.
Ex.
h1 { text-decoration: underline;}
a { text-decoration: none; }
Fonts
CSS font properties are used to define the font family, boldness, size, and the style of a
text.
font-family
Sets the text of an element with a preferred font face.
Ex. h1 { font-family: Times New Roman;}
h1 { font-family: Arial;}
font-style
Makes a font either italic or oblique.
Ex. h1 { font- style: italic;}
font-weight
Used to increase or decrease how bold or light a font appears.
Possible values are normal, bold, lighter or a numeric weight like 100, 200
or 300.
Ex. h1 { font- weight: bold;}
h1 { font- weight: 900;}
font-size
Used to increase or decrease the size of a font.
Can set the size in pixels or in %.
Ex. h1 { font- size: 20px;}
h1 { font- size: 200%;}
Font
Can be used to set all the font properties at once.
Syntax:
Selector {font: font-style font-weight font-size font-family}
State of a hyperlink
1. a:link - Indicates unvisited hyperlinks.
2. a :visited - Indicates visited hyperlinks
3. a :hover - Indicates a hyperlink with user's mouse pointer over it.
4. a :active - Indicates a hyperlink on which the user is currently clicking.
Ex.
a:link {color: blue;}
a:visited {color: red;}
a:hover {color: green;}
a:active {color: yellow;}
Text-decoration
Ex.
a:link { text-decoration: none;}
Underlines of links has been removed.
a:hover {text-decoration: underline;}
Underlines will appear when user hovers over it.
Lists
CSS properties can be used to control the appearance of HTML lists.
list-style-type
Used to control the appearance of the marker of the list items.
Possible values:
For unordered lists – none, disc, circle, square
For ordered lists – decimal, lower-alpha, upper-alpha, lower-roman,
upper- roman
Ex.
ul { list-style-type: square;}
ol{ list-style-type: upper-alpha;}
list-style-image
Use to specify an image for the marker
Ex.
ul { list-style-image: url(‘[Link]’);}
ol { list-style-image: url(‘[Link]’);}
.
Tables
CSS can be used to improve the HTML table appearance.
Border
Use to specify table border styles.
Ex.
table, th, td {
border-width: 5px;
border-style: solid;
border-color: black;
}
OR
table, th, td { border: 1px solid black;}
Possible vales for border-style: none, solid, dotted, dashed
Border-collapse property is used to collapse borders (table border, table-
heading border and table-data border) into a single border.
Ex.
table {border-collapse: collapse;}
Width and height of table cells can be set using CSS properties width and
height.
Ex.
th { height: 20px;}
Sets height of the table heading row to 20px.
table { width: 100px;}
Sets width of the table to 100px.
Comments
Comments are used to improve the readability of the code.
Comments are ignored by browsers.
A CSS comment can be placed between /* */ notation.
Ex.
/* This is a comment */
/* Comments
can be
multi lines */
PHP Comments
PHP comments are used to improve the readability of the code.
It can either be single line or multi line.
Ex.
<?php
# A single line comment
// Another way to write single line comment
/* A PHP
Multi line
Comment */
?>
PHP Variables
Variables are used to store values such as numeric values and character strings in a PHP
script.
An identifier is used to a represent a variable.
Identifier naming Conventions:
All identifiers in PHP start with a $ sign.
Must start with a letter or the underscore character (_).
Cannot start with a number.
Can only contain alpha-numeric characters and underscores (A-z, 0-9, and _).
Ex. Characters such as ` ~ ! @ # $ % ^ & * - ( ) + = [ ] { } : ; ‘ “ < > ? . , / | \ are
not allowed.
Cannot contain spaces.
Case sensitive.
Ex. $var and $Var are two different variables.
Examples of valid identifiers
$firstname, $FirstName, $FIRSTNAME, $First_Name, $_ FirstName
Ex. <?php
function testScope()
{
// Local Variable of function testScope()
$var = 1;
// Accessing $var inside the function
echo "var = $var";
echo “<br>”;
}
// Calling Function testScope()
testScope();
var =
2. Doubles
Floating-point numbers.
Ex. $float_num = 1.23;
3. Booleans
Represents two possible values, either true or false.
Ex. $flag = True;
4. NULL
Represents a variable with no value.
Has only one value: NULL.
Ex. $var = NULL;
A variable is considered to be null if:
It has been assigned the constant NULL.
It has not been set to any value yet.
It has been unset().
5. Strings
Sequences of characters.
Can be any text holding letters, numbers or special characters.
Ex. $String_var1 = “Hello world!” // Double quotes
$String_var1 = ‘Hello world!' // Single quotes
6. Arrays
Used to hold more than one value at a time in a single variable.
Formally defined as an indexed collection of data values.
An array can be created using the built-in array() function.
Types of arrays:
Indexed array
An array with a numeric key.
Ex.
<?php
// Define an indexed array
// Method 1:
$colors = array("Red", "Green", "Blue");
// Method 2:
$colors[0] = "Red";
Output: $colors[1] = "Green";
Color number 3 is Blue $colors[2] = "Blue";
Associative array
An array where each key has its own specific value.
Ex.
<?php
// Define an Associative array
// Method 1:
$color_codes = array ( "Red" => "#ff0000",
"Green" => "#00ff00",
"Blue" => "#0000ff" );
// Method 2:
$color_codes [‘Red’] = "#ff0000";
$color_codes [‘Green’] = "#00ff00";
$color_codes [‘Blue’] = "#0000ff ";
Output:
// Accessing Associative array values
Color code of Red is #ff0000 // Accessing individual values
#ff0000 echo "Color code of Red is ". $color_codes[‘Red’];
#00ff00 echo "<br>";
#0000ff //Accessing all values
foreach($color_codes as $value )
{
echo "$value <br />";
}
?>
PHP Operators
An operator is a character that represents a certain action.
Arithmetic operators
Used to perform arithmetic operations on numeric data.
Ex.
Operator Description Example
+ Addition $num1 + $num2
- Subtraction $num1 - $num2
* Multiplication $num1 * $num2
/ Division $num1 / $num2
% Modulus $num1 % $num2
Assignment operators
Used to assign values to variables.
Assigns values from right side operands to the left side operand.
Ex.
Operator Description Example
= Assignment $num1 = 10
+= Add AND $num1 += $num2
assignment (i.e .$num1 = $num1 +$num2)
-= Subtract AND $num1 -= $num2
assignment (i.e .$num1 = $num1 - $num2)
*= Multiply AND $num1 *= $num2
assignment (i.e .$num1 = $num1 * $num2
/= Divide AND $num1 /= $num2
assignment (i.e .$num1 = $num1 / $num2
%= Modulus AND $num1 %= $num2
assignment (i.e .$num1 = $num1 % $num2
Comparison Operators
Used to compare two numeric or string values.
The final result of comparison is a Boolean value, True or False.
Ex.
Operator Description
== Equal Returns true if value 1 is equal
to value 2
=== Identical Returns true if value 1 is equal
to value 2, AND they are of the
same data type
!= or < > Not equal Returns true if value 1 is not
equal to value 2
!= = Not identical Returns true if value 1 is not
equal to value 2, OR they are
not the same data type
< Less than Returns true if value 1 is less
than value 2
> Greater than Returns true if value 1 is greater
than value 2
>= Greater than or equal Returns true if value 1 is greater
than or equal to value 2
<= Less than or equal Returns true if value 1 is less
than or equal to value 2
Increment/Decrement operators
Used to increment or decrement a variable's value.
Ex.
Ex. <?php
echo "Pre-increment<br>";
$x = 10;
echo ++$x."<br>"; Output:
echo $x."<br>"; Pre-increment
11
echo "Post-increment<br>";
11
$x = 10; Post-increment
10
echo $x++."<br>"; 11
echo $x."<br>";
?>
if statement
Select an execution path if the given condition is true.
Ex. <?php
If($marks >= 40)
{
echo "You have passed the test!";
}
?>
if...else statement
Select one execution path if the given condition is true or another if
the condition is false.
Ex. <?php
If($marks >= 40)
{
echo "You have passed the test!";
}
Else
{
echo "You have failed the test!";
}
?>
if...elseif....else statement
Select an execution path when there are more than two conditions
are available.
Ex. <?php
if ($marks >= 75)
{
echo "A";
}
elseif ($marks >= 65)
{
echo "B";
}
elseif ($marks >= 55)
{
echo "C";
}
elseif ($marks >= 40)
{
echo "S";
}
Else
{
echo "W";
}
?>
Switch statement
Tests an expression against a series of values (cases) until it finds a
match, and executes the corresponding path of that match.
Ex. <?php
switch ($subject)
{
case "Physics":
echo "My favorite subject is Physics";
break;
case "Maths":
echo "My favorite subject is Maths";
break;
case "IT":
echo "My favorite subject is IT";
break;
default:
echo "I like all the subjects!";
}
?>
“Break” statement is used to prevent the code from running into
the next case automatically.
If none of the cases matches then the “default” statement will be
executed.
2. Control Loops
Allows execution of a code block until the specified condition is true.
Used to automate the repetitive tasks within a program.
While loop
Executes a code block as long as the given expression is true.
Ex. <?php
$i = 0;
while( $i < 3) Output:
{
$i++; 1
echo ("$i <br>"); 2
}
echo ("Loop stopped at i = $i" ); 3
?> Loop stopped at i = 3
Do...while loop
Executes the code block once and evaluates the condition
at the end.
It executes the code block at least once, even if the
condition is false the first time.
Ex. <?php
$i = 0; Output:
do
1
{
$i++; 2
echo ("$i <br>");
3
}
while( $i < 3 ); Loop stopped at i = 3
echo ("Loop stopped at i = $i" );
?>
For loop
A for loop is similar to the while loop but uses a counter to
execute a code block a certain number of times.
Ex.
. <?php
$x = 4;
for( $i = 0; $i<=3; $i++ )
{
$x -= 1;
echo ("$i <br>");
}
echo ("At the end of the loop x = $x and i = $i" );
?> Output:
1. Built-in Functions
Standard PHP functions that can directly include i.e call within a PHP
scripts to perform a specific task.
Ex.
is_numeric() - Returns true if numeric and false if it’s not
count() - Return the length (the number of elements) of an array
str_word_count() - Counts the number of words in a string
2. User-Defined Functions
Functions created by the programmer to perform a specific task.
Syntax:
function functionName()
{
code to be executed;
}
Ex.
<?php
/* Defining a PHP Function */
function writeMessage()
{
echo "Welcome to PHP!";
}
MySQLi Procedural
else
{
// Display a message with the error description
echo "ERROR: Could not be able to execute $sql”.mysqli_error($conn);
}
// Close connection
mysqli_close($conn);
?>
Saving form data into a MySQL database
Ex.
<?php
$conn = mysqli_connect("localhost", "root", "", "php");
if(isset($_POST['submit']))
{
$uname=$_POST['User_Name'];
$password=$_POST['Password'];
If (mysqli_query($conn, $sql))
{
echo "Records inserted successfully.";
}
else
{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($conn);
}
}
// Close connection
mysqli_close($conn);
?>
<html>
<head>
<title> Sign in Form</title>
</head>
<body>
<form method = "POST" action = "#">
User Name:<br>
<input type="text" name="User_Name"/> <br>
Password:<br>
<input type="password" name="Password"/>
<input type="submit" name="submit" value="Sign in"/>
</form>
</body>
</html>
MySQLi Object-oriented
else
{
echo "Error: " . $sql . "<br>" . $conn->error;
}
//Close Connection by calling method close().
$conn->close();
?>