KEMBAR78
Web Technology | PDF | World Wide Web | Internet & Web
0% found this document useful (0 votes)
10 views85 pages

Web Technology

The document provides an overview of web technology, focusing on the World Wide Web, server-side and client-side scripting, and the role of JavaScript in web development. It explains the differences between server-side and client-side scripting, the features of JavaScript, and various data types, operators, and control structures in JavaScript. Additionally, it highlights how JavaScript can be integrated into HTML and its importance in creating dynamic and interactive web applications.

Uploaded by

yttv746164
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views85 pages

Web Technology

The document provides an overview of web technology, focusing on the World Wide Web, server-side and client-side scripting, and the role of JavaScript in web development. It explains the differences between server-side and client-side scripting, the features of JavaScript, and various data types, operators, and control structures in JavaScript. Additionally, it highlights how JavaScript can be integrated into HTML and its importance in creating dynamic and interactive web applications.

Uploaded by

yttv746164
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 85

Unit 3: Web Technology

Introduction
 World Wide Web is the service of Internet that supports special formatted
documents.
 It is huge collection of hypertext document that can be accessed over the
Internet.
 It contains billions of files stored on the computers all over the world.
 The World Wide Web (WWW) is a network of online content that is formatted in
HTML and accessed via HTTP.
 The three main programming language languages that completes up the World
Wide Web are HTML, CSS, and JavaScript.
 In the field of Information Technology, Internet is an essential platform, whether it
is for developing or for consumer use.
 Web technology is a term which refers to different multimedia components which
can be used with markup languages in order to create web sites.
 Each technology in the web is dependent on other in order to fulfil the objectives.
Server side and Client side scripting
Server side scripting
 Server side scripting is a method of designing websites so that the process or user
request is run on the originating server.
 Any scripting or programming that can run on the web server is known as server-
side scripting.
 Server-side scripts provide an interface to the user and limit access to proprietary
data and help keep control of the script source code.
 It works in the back end and is not visible at the client end.
 The operations like customization of a website, dynamic change in the website
content, response generation to the user’s queries, accessing the database, and so
on are performed at the server end.
 When a browser sends a request to the server for a webpage consisting of server-
side scripting, the web server processes the script prior to serving the page to the
browser.
Server side and Client side scripting
Here the processing of a script could include extracting information from a database,
making simple calculations, or choosing the appropriate content that is to be
displayed in the client end.
The script is being processed and the output is sent to the browser.
The web server abstracts the scripts from the end user until serving the content,
which makes the data and source code more secure.
Client side scripting
 A client-side script is a program that is processed within the client browser.
 These kinds of scripts are small programs which are downloaded, compiled and run
by the browser.
 The browser gets the page sent by the server and executes the client-side scripts.
 Client-side scripting can’t be utilized to join with the databases on the web server.
 Client-side scripting cannot get the file system which lies at the web server.
 When the user opens the web page, the web server sends the HTML document and
the script to the browser and the browser executes the script, displaying the final
result.
 In the client side scripting web browser downloads the code temporarily and
processes it without the server which results in the less work for the server.
 JavaScript is the popular and widely used client side scripting language as it is
compatible and has the ability to run on any browsers.
 It is widely used in creating dynamic websites.
Introduction Of Internet Technology
 Internet is defined as the interconnection between several computers of different
types belonging to various network all around the world.
 Internet is simply the medium through which billions of computers around the
world can be interconnected with each other.
 Internet uses the standard Internet protocol suite (TCP/IP) to link several billion of
devices worldwide.
 The group of technologies that allow users to access information and
communication over the World Wide Web is called Internet technology.
 Technology includes web browsers, associated hardware components, Internet
Service Providers and the different protocols that are required to access Internet
and World Wide Web.
 So Internet Technology is a system that combines computers and devices,
telecommunications, different software and protocols (rules/procedures) which
enables data transmission within and between geographically dispersed persons or
organizations through the network.
JavaScript
 JavaScript is a lightweight, interpreted programming language.
 It is designed for creating network-centric applications.
 It is complimentary to and integrated with Java.
 JavaScript is very easy to implement because it is integrated with HTML.
 JavaScript is a lightweight text based programming language that can be used on
both the client-side and server-side.
 HTML and CSS is used to define the style and structure of the web page whereas
JavaScript is used to make web pages interactive and dynamic.
 JavaScript can be used on both, front-end and the back-end and has become one of
the core technologies of web development.
 It can easily be integrated with HTML and almost all the web browsers can execute
JavaScript commands.
 So JavaScript codes can easily be typed into the HTML document and it doesn’t
require any additional programs or compilers.
Features of JavaScript
 Different JavaScript based frameworks like jQuery, Node.JS and libraries which are
already developed can be used directly which makes software development fast.
 JavaScript is platform independent and portable as it can run on any platform or
any web browser without affecting the output of the Script.
 JavaScript is an interpreter based programming language, so the script written
inside JavaScript is processed line by line.
 JavaScript is a lightweight scripting language because it is designed for executing
the scripts in the user’s browser software.
 JavaScript can be used to handle form validation which helps the client to validate
the details entered by the user and sends data to the server with very ease.
Adding JavaScript to HTML page
 JavaScript code can easily be embed in the HTML document by using the HTML tag
<script> ……. </script>.
 The <script>......</script> tag can be placed anywhere in the HTML document. It can be
placed in the <head> section, <body> section, or after the </body> close tag, depending
on where the JavaScript code is to be loaded.
<html>
<head>
<title>Today's Date</title>
</head>
<body>
<script>
let d = new Date();
document.body.innerHTML = "<h1>Today's date is " + d + "</h1>"
</script>
</body>
</html>
JavaScript Data types
 Data type defines the type of data that is stored by the
variables.
 It is not necessary to specify type of the variable because it is
dynamically used by JavaScript engine.
 var is used to specify the data type.
 It can hold any type of values such as numbers, strings etc.
 There are two types of data types in JavaScript.
 Primitive data type
 Non-primitive (reference) data type
Primitive data type
There are five types of primitive data types in JavaScript. They are as
follows:

Data Type Description

String Represents sequence of characters


example "Programming"

Number Represents numeric values example 100

Boolean Represents boolean value, that is either


true or false

Undefined Represents undefined value

Null Represents null value that is no value at


all
For example:
var x=100; //holding number
var y=“Program";//holding string
Non-primitive data type
The non-primitive data types are as follows:

Data Type Description

Object Represents instance through which we can


access members

Array Represents group of similar values

RegExp Represents regular expression


Variables
 Variable is the name given to the memory location which is used to store value.
 The value stored in the variable can be changed during the execution of program.
 JavaScript uses a keyword var to declare a variable.
 Variables in JavaScript are loosely-typed variables which means that it can store
value of any data type.
 Value can be assigned to the variable using equal to (=) operator.
The rules for declaring a variable (identifier) are
 Variable name can have alphanumeric character but must start with a letter (a to z
or A to Z), underscore( _ ), or dollar( $ ) sign.
 Reserved words or keywords cannot be used as variable name.
 Variable name can be of any character long. There is no limit to the length of the
variable name.
Example of variable declaration and
Initialization
var n = 100; // variable stores numeric value
var name = 'Anish'; // variable stores string value
var age; // declared a variable without assigning a value
 Multiple variables can also be declared in a single line separated by comma.
var n = 100, name = 'Anish', age;
Value can also be stored in the variables without var keyword.
n = 100; // variable stores numeric value
name = 'Anish'; // variable stores string value
Operator
 Operators are the sign or symbols which performs specific arithmetic and logical
operations on constants and variables.
 These constant and variable on which the operator operates are called operands.
JavaScript supports a large set of built in operators.
 The different types of operators in JavaScript are:
 Arithmetic Operators
 Relational Operators
 Logical Operators
 Assignment Operators
 String Operator
 Conditional Operators
Arithmetic Operator
The operators which are used to perform different types of mathematical calculation
such as addition, subtraction, multiplication and division are called arithmetic
operators.
The different arithmetic operators used in JavaScript are

Operator Description Example(A=100, B=200)

+ Adds two operands. A + B = 300

− Subtracts second operand from the first. A − B = -100

* Multiplies both operands. A * B = 2000

/ Divides numerator by de-numerator. B/A=2

% Modulus Operator and remainder of after an integer division. B%A=0


Relational Operators
 The operators which are used to compare two variables or two values are called
relational operators.
 The different types of relational operators are
Operator Description Example(A=100,B=200)

== Checks if the values of two operands are (A == B) is not


equal or not. If yes, then the condition true.
becomes true.
!= Checks if the values of two operands are (A != B) is
equal or not. If the values are not equal, true.
then the condition becomes true.
> Checks if the value of left operand is (A > B) is not
greater than the value of right operand. If true.
yes, then the condition becomes true.
< Checks if the value of left operand is less (A < B) is
than the value of right operand. If yes, true.
then the condition becomes true.
>= Checks if the value of left operand is (A >= B) is not
greater than or equal to the value of right true.
operand. If yes, then the condition
becomes true.
<= Checks if the value of left operand is less (A <= B) is
than or equal to the value of right true.
operand. If yes, then the condition
becomes true.
Logical Operators
 Logical operators are used to combine and test two or more
conditions to make decisions.
 These operators are used to perform logical operations on the
given variables and the logical result will be either true or
false.
 The different types of logical operators are
Operator Description Example(A=100,B=200)
&& Called Logical AND operator. If all the conditions are true, (A>200 && B<30) is false.
then the result becomes true.

|| Called Logical OR Operator. If any one condition is true, (A>20 || B<30) is true.
then the result becomes true.

! Called Logical NOT Operator. It is used to reverse the !(A>20) is true.


logical state of its operand. If a condition is true, then
Logical NOT operator will make it false.
Assignment operator
Assignment operator is used to assign or store value to the
variables with less key strokes.
Operator Description Example Meaning

= Assigns right operand value to left operand. a=10 constant value 10 is


assigned to or stored in
variable a

+= Sums up left and right operand values and assign a += 10 a = a + 10


the result to the left operand.

-= Subtract right operand value from left operand a -= 10 a = a – 10


value and assign the result to the left operand.

*= Multiply left and right operand values and assign a *= 10 a = a * 10


the result to the left operand.

/= Divide left operand value by right operand value a /= 10 a = a / 10


and assign the result to the left operand.

%= Get the modulus of left operand divide by right a %= 10 a = a % 10


operand and assign resulted modulus to the left
operand.
Unary operator
 The operators which are used to increases or decreases the value of
variable by 1 is called unary operator.
 ++ and --are the examples of unary operators.
Operator Description Example

++ Increases the value of variable by 1 A++ equivalent to A=A+1

-- Decreases the value of variable by 1 A-- equivalent to A=A-1


String Operator
 The string operator is used to add (concatenate) two or more
strings values.
 The example of string operator is +.
var a = "Java";
var b = "Script";
var c = a+b;
The value stored in variable c is JavaScript.
Control structures

 Control structure actually controls the flow of execution of a program.


Following are the several control structure supported by JavaScript.
 if … else
 switch case
 do while loop
 while loop
 for loop
if
The statements inside if body executes only when the condition defined
by if statement is true. If the condition is false, the statement enclosed
in if’s body is skipped and execution continues with the rest of the
program.
Syntax: if (conditional expression)
{
[block of statement
}
if
Example:
<html>
<body>
<script type="text/javascript">
var num = prompt("Enter Number");
if (num > 0)
{
document.write("Given number is Positive");
}
</script>
</body>
</html>
if ( )…..else
This control statement has two block of statements. The statements
inside if body executes when the condition defined by if statement is
true. If condition is false, the statements within else body will be
executed.
Syntax: if (conditional expression)
{
[block of statement 1]
}
else
{
[block of statement 2]
}
Example:
<html>
<body>
<script type="text/javascript">
var num = prompt("Enter Number");
if (num %2== 0)
{
document.write("Given number is even");
}
else
document.write ("Given number is odd");
</script>
</body>
</html>
if ( )…..else if ( )……….else
This control statement has multiple block of statements and user can decide
among multiple options. If one of the conditions controlling the if is true, the
statement associated with that if is executed, and the rest of the else-if ladder is
bypassed. If none of the conditions are true, then the final else statement will be
executed.
Syntax: if (conditional expression)
{
[block of statement- 1]
}
else if (conditional expression)
{
[block of statement -2]
}
else
{
[block of statement- n]
<html>
<body>
<script type="text/javascript">
var n = prompt("Enter Number");
if (n> 0)
{
document.write("Given number is Positive");
}
else if(n< 0)
{
document.write("Given number is negative");
}
else
{
document.write("Given number is neutral");
}
</script>
</body>
</html>
switch
 Switch case statements are used to overcome the problem for long if
statements that compare a variable to several values.
 Switch statement is a control statement that allows us to choose only
one option among the many given options.
 The expression in switch evaluates a value, which is then compared to
the values present in different cases.
 It executes that block of code which matches the case value. If there
is no match, then default block is executed.
 If we do not use break, all statements after the matching label are
executed.
switch
Syntax:
switch(expression)
{
case condition 1:
Statements;
break;
case condition 2:
Statements;
break;
case condition 3:
Statements;
break;
case condition n:
Statements;
break;
default:
Statement;
Example:
<html>
<head>
<script>
var day = prompt("Enter a number between 1 and 7");
switch (day)
{
case (day="1"):
document.write("The day is Sunday");
break;
case (day="2"):
document.write("The day is Monday");
break;
case (day="3"):
document.write("The day is Tuesday");
break;
case (day="4"):
document.write("The day is Wednesday");
break;
case (day="5"):
document.write("The day is Thursday");
break;
case (day="6"):
document.write("The day is Friday");
break;
case (day="7"):
document.write("The day is Saturday");
break;
default:
document.write("Invalid Weekday");
break;
}
</script>
</head>
</html>
Iteration / Looping control statement
 The process of repeatedly executing the block of statements is called
iteration or looping. Loops are used to repeat the execution of a block
of code.
 Loops are used to execute a set of statements repeatedly until a
particular condition is satisfied and loop terminates when the
condition is false or when certain condition is met.
 Loops may be classified as entry controlled loop (Pre test loop) and
exit controlled loop (Post test loop).
 There are three types of looping statements in C.
 for
 while
 do ……..while
for
 for loop is used to execute a set of statements for given number of times.
 For loop is generally used when the number of execution of statements is
already known.
 In for loop, condition is checked before the execution of loop. So it is
known as entry controlled loop( Pre test loop).
Syntax: for (initialization; conditional expression; increment/decrement)
{
[block of statements]
}
Example:
<html>
<head>
<script>
for(i=1;i<=50;i++)
{
document.write(i);
}
</script>
</head>
</html>
while
 The while loop is an entry controlled loop in which condition is checked
before the execution of loop.
 In while loop, the loop may not execute even at once.
 It is generally used when the number of iterations is not known.
Syntax: variable initialization
while (conditional expression)
{
statements
…………..…
…………….
variable increment/decrement
}
<html>
<head>
<script>
var i=1;
while (i<=20)
{
document.write(i);
i=i+1;
}
</script>
</head>
</html>
do …………while
 Sometimes it is necessary to execute body of the loop before testing the
condition.
 ‘Such situations can be handled by do….. while loop.
 do…….while’ loop is an exit controlled loop (Post test loop). In
‘do…..while’ loop the condition is checked after the execution of loop.
 The statements within the do…..while loop will execute at least once.
Syntax: variable initialization
do
{
statements
Variable increment/decrement
}
while(conditional expression);
Example
<html>
<body>
<script type ="text/javascript">
var i = 1;
do
{
document.write(i+"<br>")
i=i+2;
}
while (i <= 20)
</script>
</body>
</html>
Function
 A function is a block of code which can be called from anywhere in the
program.
 Creating function eliminates the need of writing the same code again and
again as it can be called and executed according to the requirement.
 It helps programmers in writing modular codes.
 Functions allow a programmer to divide a big program into a number of
small and manageable functions.
JavaScript Function Syntax
 A JavaScript function is defined with the function keyword, followed by a
unique function name and the list of parameters which are enclosed inside
the parenthesis ( ).
 Function names can contain letters, digits, underscores, and dollar signs
(same rules as variables).
 The parenthesis may include parameter names separated by commas or
can also be empty.
 The code to be executed, by the function, is placed inside curly brackets {}.
Syntax:
//calling a function
function-name( arguments);
//defining a function
function function-name(Parameters )
{
// Statements
}
Parameters are listed inside the parenthesis ( ) in the function definition and
arguments are the values received by the function when it is invoked.
<html>
<body>
<script type ="text/javascript">
Message(“Ram", “Gurung");
Message("Ramesh", "Shrestha");
Message(100, 200);
function Message(x,y)
{
document.write("Hello " + x + " " + y+"<br>");
}
</script>
</body>
</html>
<html>
<body>
<script type ="text/javascript">
showMessage()
function showMessage()
{
document.write( "Hello everyone!" );
document.write( "Goood morning" );
}
</script>
</body>
</html>
Function Return
 When JavaScript reaches a return statement, it ends the execution of the
function and returns control to the calling function.
 The return statement in function returns a value specified by the
programmer.
 In JavaScript, a function can return nothing, by using empty return
statement.
<html>
<body>
<script type ="text/javascript">
var a=100;
var b=200
var sum = add(a, b)
document.write(sum)
function add(a, b)
{
return a + b;
}
</script>
</body>
</html>
Object based programming with
Java Script and Event handling
 Java Script's interaction with HTML is handled through events that occur
when the user or the browser manipulates a page.
 When the page loads, it is called an event.
 When the user clicks a button, that click too is an event.
 One event that occurs frequently is when the mouse is clicked on
something, such as a hypertext link.
 Many browsers change the status bar when the mouse is moved over an
anchor.
 Other examples include events like pressing any key, closing a window,
resizing a window, etc.
 Software developers can use these events to execute Java Script coded
responses, which cause buttons to close windows, messages to be displayed
to users, data to be validated, and virtually any other type of response
imaginable.
Object based programming with
Java Script and Event handling
 Events are also frequently used on forms to make it easier for the user to
enter correct information, and to alert them if they enters wrong
information.
 For instance, if a text box requires a phone number, you can use events to
notice whenever the user inputs data into the text box, and to ensure that
the inputted data contains only numbers and dashes.
 Finally, you can validate all of the input before the user submits the form.
Events don't only have to be used to process forms.
A Short list of Java Script Events:
onclick Event Type
 This is the most frequently used event type which occurs when a user clicks
the left button of his mouse.
 User can put validation, warning etc., against this event type.
A Short list of Java Script Events:
<html>
<body>
<script>
function sayHello()
{
alert("Hello World")
}
</script>
<p>Click the following button and see result</p>
<form>
<input type = "button" onclick = "sayHello()" value = "Say Hello" />
</form>
</body>
</html>
<html>
<body>
<script>
function warning()
{
alert("Do you want to close the tab ")
}
</script>
<p>Click the following button and see result</p>
<form>
<input type = "button" onclick = "warning()" value = "close" />
</form>
</body>
</html>
onmouseover and onmouseout
 These two event types will help to create nice effects with images or even
with text as well.
 The onmouseover event triggers when user bring the mouse over any
element and the onmouseout triggers when user move the mouse out from
that element.
<html>
<body>
<script>
function over()
{
document.write ("Mouse Over");
}
function out() {
document.write ("Mouse Out");
}
</script>
<p>Bring your mouse inside the division to see the result:</p>
<div onmouseover = "over()" onmouseout = "out()">
<h2> This is inside the division </h2>
</div>
</body>
</html>
PHP
 PHP stands for Hypertext Pre-processor.
 PHP is widely used open source general-purpose server side scripting
language that is used to develop dynamic websites and especially suited for
web development.
 It is an interpreted language and PHP scripts can only be interpreted on a
server that has PHP installed.
 PHP can be embedded into HTML and can also be used in combination with
various web template systems, web content management system and web
frameworks.
 PHP runs on various platforms (Windows, Linux, Unix, Mac OS X, etc.) and is
compatible with almost all servers used today (Apache, IIS, etc.).
 PHP supports a wide range of databases and can be integrated with a
number of popular databases, including MySQL, PostgreSQL, Oracle, Sybase,
Informix, and Microsoft SQL Server.
 PHP is generally used as back end development.
Characteristics of PHP

Simplicity and ease of use


One of the characteristics of PHP is its simplicity, familiarity and easy to learn
the language as the syntax is similar to that of ‘C’ language. PHP is very
advantageous for new users as it is reliable, fluent, organized, clean,
demandable and efficient. PHP also has many pre-defined functions which can
be implemented easily to create dynamic websites with secured data.
Loosely typed language
PHP is loosely typed language because it allows the use of variables without
declaring its data types. Variables are executed depending on the value
assigned to the variable.
Open Source
PHP is open source, so it can be freely downloaded and user does not have to
pay for using PHP. All PHP frameworks are open sources and no payment is
required for the users and it is completely free. User can just download PHP
and start using developing their applications and projects. This also reduces
the total cost of software development.
Platform Independent
PHP code can run on every platform, Linux, Unix, Mac OS X, Windows and is compatible
with almost all servers used today (Apache, IIS, etc.) It also supports various databases
like MySQL, SQLite, Oracle, Sybase, Informix, and PostgreSQL and provides libraries to
access these databases to interact with web servers. Many frameworks like Codeignitor,
Zend Framework, CakePHP make use of PHP.Popular content management systems like
WordPress, Joomla and Drupal also use PHP as prime language.
Flexibility
PHP is also flexible as it can easily be integrated with HTML, XML, Javascript and many
more. PHP can run on multiple Operating Systems like Windows, Unix, MacOS, Linux, etc
and PHP scripts can easily execute on any device like laptops, mobiles, tablets, and
computer. Desktop applications are created using advanced PHP features. The
executable PHP can also be run on command-line as well as directly on the machine.
Heavyweight applications can be created without a server or browser.
Interpreted
It is an interpreted language, so there is no need for compilation.
Case Sensitive
PHP is case sensitive scripting language at time of variable declaration but all keywords
(e.g. if, else, while, echo, etc.), classes, functions, and user-defined functions are not
case-sensitive.
Server Side Scripting using PHP
 PHP is a server-side scripting language designed to be used for web
purposes.PHP codes are executed on the web server before the data is
passed to the user's browser.
 So PHP code never reaches to the user.
 After the PHP code is executed, the information it generates is embedded
in the HTML and is sent to the viewer's web browser.
 One way to see this in action is to open one of the PHP pages in a web
browser and then choose the "'View Source" option.
 HTML code is visible but PHP code is not visible.
 The result of the PHP code is there because it is embedded in the HTML on
the server before the web page is delivered to the browser.
Operators
Operators are used to perform operations on PHP variables and simple values.
The values in which the operator operates are called operands. The different
types of operators in PHP are
Arithmetic operators
Assignment operators
Relational/ Comparison operators
Unary operators
Logical operators
String operators
String Operators
String operators are used to perform operations on string. There are only two string operators.

Name Operator What does it Example


do?
Concatenatio . (a dot) It is used to $a.$b
n concatenate(
join
together) two
strings.
Concatenatio .= It is used to $a .= $b
n Assignment append one
string to
another.
Variables Manipulation

Variables are used to store data, like string of text, numbers, etc.
Variables are the name given to the memory location which are used to
store values. The value stored in the variable can change over the course
of a script. In PHP, a variable is declared using $ sign followed by
variable name. In PHP, a variable does not need to be declared before
adding a value to it. PHP automatically converts the variable to the
correct data type, depending on its value. After declaring a variable it
can be reused throughout the code. The assignment operator (=) used to
assign value to a variable.
Rules for declaring PHP variables
 A variable name always starts with the $ sign, followed by the
name of the variable.
 A variable name must start with a letter or the underscore
character after $ sign.
 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).
 A variable name cannot contain space.
Statements used in PHP
echo
echo statement is used to display the output on the screen. echo
statement is used to output one or more strings in a web browser. It is
not a function but a language construct and so can be used with or
without the parenthesis. However, if multiple parameters are to be
passed, they must not be enclosed in parenthesis as that will generate
an error. We can pass multiple strings separated by comma (,) in echo.
Syntax:
echo(Variable name)
Example:

<?php
$name="Dinesh";
echo $name;//or echo ($name);
?>
Example:
<?php
$name = "Ramesh";
$job = "PHP Developer";
$age = 35;
echo $name , $job , $age, " years old"; //passing multiple argument?>
print
rint statement is also used to display the output. print can be used with
p

or without parenthesis. It cannot be used to pass multiple arguments.


Syntax:
print(Variable name)
Example:
<?php
$name="John";
print $name;//orprint ($name);
?>
PHP method and Get method
PHP is server side scripting language, so all the data is processed by the
server. The data have to be send to the server in order to be processed.
A web browser communicates with the server typically using the
following HTTP methods.
 POST
 GET
POST
In POST method the data is sent to the server as a package in a separate
communication with the processing script. Data sent through POST
method will not visible in the URL. Since the data sent by the POST
method is not visible in the URL, it is not possible to bookmark the page
with specific query. PHP provide variable $_POST to access all the
information sent through post method or submitted through an HTML
form using the method="post".
Example:
<html>
<form method="post">
Name:<input type="text" name="name"> <br>
Password:<input type="password" name="password"><br>
<input type="submit" value="login">
</form>
<?php
$name=$_POST["name"]; //receiving name field value in $name variable
$password=$_POST["password"];
//receiving password field value in $password variable
echo "Welcome: $name, your password is: $password";
?>
</html>
GET
Get request is the default form request. The data passed
through get request is visible on the URL browser so it is not
secured. The limited amount of data can be send through
get request. PHP provides the variable $_GET to access all
the information sent either through the URL or submitted
through an HTML form using the method="get".
Example:
<html>
Form method="get">
Name: <input type="text" name="name"><br>
<input type="submit" value="submit"/>
</form>
</html>
<?php
$name=$_GET["name"];
//receiving name field value in $name variable
echo "Welcome, $name";
?>
Basic programming in PHP
1. Program to display the sum of two numbers
<?php
$n1 = 100;
$n2 = 200;
$sum = $n1 + $n2;
echo "Sum of two numbers= $sum";
?>
2. Program to display whether the number is even or odd
<?php
$n = 500;
if($n%2==0)
{
echo “The number is Even”;
}
else
{
echo “The number is odd”;
}
?>
Object based programming with
Java Script and Event handling
3. Program to display all the numbers from 1 to 10
<?php
for ($i=1; $i<=10; $i++)
{
echo $i ;
}
?>
Program to check whether the given number is divisible by 7 or not.
<html>
<form method="post">
<p> Enter the number: <input type="number" name="num"> <br>
<input type="submit" value="check">
</form>
<?php
$num=$_POST["num"];
if($num%7==0)
{
echo “The number is divisible by 7”;
}
else
{
echo “The number is not divisible by 7”;
}
?>
<html>
Form validation
Forms are normally used in webpages to enter the data so that it can be send
to the server for processing. A form is also known as a web form or HTML form.
Working with forms is an every day task for almost every web developer. The
data entered by the user in the form have to be checked and validated before
it is passed to the server. It is important to validate the form submitted by the
user because it can have inappropriate values. So, validation is must to
authenticate user. JavaScript provides facility to validate the form on the
client-side so data processing will be faster than server-side validation. Most
of the web developers prefer JavaScript form validation. Through JavaScript,
we can validate name, password, email, date, mobile numbers and more
fields.
In this example, we are going to validate the name and password. The name
can’t be empty and password can’t be less than 6 characters long. Here, we
are validating the form on form submit. The user will not be forwarded to the
next page until given values are correct.
<script>
function validateform(){
var name=document.myform.name.value;
var password=document.myform.password.value;
if (name==null || name==""){
alert("Name can't be blank");
return false;
}else if(password.length<6){
alert("Password must be at least 6 characters long.");
return false;
}
}
</script>
<body>
<form name="myform" method="post" action="abc.jsp" onsubmit="return valida
teform()" >
Name: <input type="text" name="name"><br/>
Password: <input type="password" name="password"><br/>
<input type="submit" value="register">
</form>
Database Connectivity

Database is an organized collection of related information. A Database


connection allows the client software to communicate with the database
server software. A database server software can be on the same machine or
can be on the separate machine. A connection is required to send commands
and receive answers, usually in the form of a result set. PHP can be used to
connect and manipulate databases.
MySQL is the most popular database system used with PHP. MySQL is a
Database Management System that allows the user to manage relational
databases. It is the open source Relational Database Management System
software based on Structured Query Language. MySQL can run on all platforms
including Windows, Linux and Unix. MySQL can be used in a wide range of
applications but it is often associated with web applications and online
publishing. The main characteristics of MySQL are
Characteristics of MySql

 It is open source software, so it is free to download and use.


 It is used on the web and runs on a server.
 It is platform independent and compiles on a number of platforms like
Novell NetWare, Windows, Linux and many varieties of UNIX.
 It consists of a data security layer that protects sensitive data from
intruders and passwords are also encrypted in MySQL.
 It allows transactions to be rolled back, commit, and crash recovery.
 It is flexible and supports a large number of embedded applications.
 It uses SQL queries to access and manage the records of the table.

Creating Connectivity
<?php
$servername = "localhost";
$database = "databasename";
$username = "username";
$password = "password";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $database);
// Check connection
if ($conn->connect_error)
{
die("Connection failed: " . $conn->connect_error);
}
else{
echo "Connected successfully"; }
mysqli_close($conn);
?>
The above script consists of four variables, server name, database, username,
and password and the corresponding values are stored in the variables. The
values stored should correspond to the connection details. The PHP function
mysqli_connect() establishes a connection with the specified database. “if
statement” shows whether the connection was established or not. When the
connection fails, it gives the message Connection failed. The die function
prints the message and then exits out of the script. If the connection is
successful, it displays “Connected successfully.” When the script ends, the
connection with the database also closes. If you want to end the code
manually, use the mysqli_close function can also be used to end the code
manually
The CREATE DATABASE statement is used to create a database in MySQL. The
following examples create a database named "mydatabase":
<?php
$servername = "localhost";
$username = "username";
$password = "password";
// Create connection
$conn = mysqli_connect($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Create database
$sql = "CREATE DATABASE mydatabase";
if ($conn->query($sql) === TRUE) {
echo "Database created successfully";
} else {
echo "Error creating database: " . $conn->error;
}
$conn->close();
?>
Adding records
 <?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO Student (firstname, lastname, email)
VALUES ('Shyam', 'Thapa', 'shyam@gmailcom')";

if ($conn->query($sql) === TRUE) {


echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}

$conn->close();
Displaying records

 <?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT id, firstname, lastname FROM Student WHERE lastname= “Thapa";
$result = $conn->query($sql);
while($row = $result->fetch_assoc())

{
echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"].
"<br>";
}

else

{
echo "0 results";
}
$conn->close();
?>

You might also like