Module3 Php Web Technology NOTES
Module3 Php Web Technology NOTES
CHAPTER 10
The difference between the client side scripting and server side scripting is, the client side
scripting runs on a browser but server side scripting runs on a server softwares. There are a number
of server-side scripting languages available, such as PHP, JSP, Servlets, Node JS, and ASP.Net.
etc.
Server Softwares
Server software is a type of software that is designed to be used, operated and managed on
a computing server. It provides and facilitates the harnessing of underlying server computing
power for use with an array of high-end computing services and functions.
Server software is primarily built to interact with a server’s hardware infrastructure,
including the processor, memory, storage, input/output (I/O) and other communication ports.
Depending on the type or usage of the server, server software may be classified into various forms,
such as the following:
• Web Server software
• Application Server software
• Database server software
A web server is a system that delivers content or services to end users over the internet. A
web server consists of a physical server, server operating system (OS) and software used to
facilitate HTTP communication. A web server is also known as an internet server. The most simple
definition is that a web server runs a website by returning HTML files over an HTTP connection.
A better definition might be that a web server is any internet server that responds to HTTP requests
to deliver content and services. The following are the examples of web servers.
• Apache WebServer – (XAMPP) – PHP Support
• Microsoft Internet Information Server(IIS) – ASP Support
• Apache Tomcat Web Server – JSP Support
• Oracle iplanet Web Server
• Lighttpd
• IBM HTTP Server
Application servers provide a framework to build and deploy web applications and offer a
variety of services when running such applications. These services include security, transactions,
clustering for increased performance, and diagnostic capabilities, among others. These products
can also include servers that strictly host web applications.
Application servers are used by software developers who are trying to quickly build
applications and have them supported by the server environment in which they are deployed. These
products allow developers to focus on the application itself and spend less time focusing on outside
issues, such as performance or security. Application servers can run in conjunction with relational
databases and a variety of web frameworks. The following are the examples for Application
Servers.
• IBM WebSphere Application Server
• Oracle WebLogic
• Apache Tomcat
• Apache Geronimo
• appserver.io
What is XAMPP?
XAMPP start for Cross-Platform, PHP, Mysql, Perl, Apache. XAMPP is a very lightweight
and simple Apache distribution, So you can use it because this is available any platform and easily
as Windows, Linux, or Mac.
This is the first step for setup local hosts for server-side scripting. First go to google and type
XAMPP download and click the first link and download XAMPP and install. This is free and
easily install.
https://www.apachefriends.org/index.html
After that install you can look like as below image then you go to click start button of Apache and
MySql for start XAMPP server
You will be getting the following page! Now, we can ensure that the PHP has been installed
successfully.
The following page opens, which is a MySQL Database portal!. Now, you can ensure that the
MySQL has been successfully installed.
Next step is to install IDE for PHP application development, the Apache Netbeans IDE provides
a free Editor with HTML, CSS, JavaScript and PHP support. The procedure is as follows.
Step 2: After installing JDK software, install the netbeans IDE from the following site.
https://netbeans.apache.org/download/nb120/nb120.html
Step 5: Select “PHP” from categories and “PHP Application” from Projects.
Step 6: Give the project name and location and click “Finish”
Step 7:Now, the PHP project is created with default index.php page.
Step 8: Test the php Application by Right click and Select “Run File”
Step 9: Now, the browser automatically opens the PHP application and show the output as given
below.
PHP server side scripting language allows us to create dynamic Client and Server request
respnse model as given below. Client request a page on the server which contains the PHP script.
The PHP script is processed including if there is any datbase access and final response page will
be created and return to the client browser.
<?php
statements…
…………
…………
?>
Display Data in a web browser from PHP Script
• With PHP, there are two basic ways to get output: echo and print.
<!DOCTYPE html>
<html>
<body>
<?php
// This is a single-line comment
</body>
</html>
Multiline Comments
<!DOCTYPE html>
<html>
<body>
<?php
/*
This is a multiple-lines comment block
that spans over multiple
lines
*/
?>
</body>
</html>
⚫ In PHP, a variable must starts with the $ sign, followed by the name of the variable:
⚫ It is a faster memory access operator like pointer in C programming.
<?php
$name = "John";
$company = 'Infosys';
$number = 10;
$pi = 3.14;
?>
After the execution of the statements above, the variable $name will hold the value “John”, the
variable $company will hold the value “Infosys”, and the variable $number will hold the value
10 and the variable $pi will hold the value “3.14”.
<?php
$price = 1000;
echo "The product price is $price";
echo 'The product price is $price';
?>
String concatenation
In PHP, the dot(.) operator can be used to concatenate the strings as well as other data
values.
<?php
$name = 'John';
$number = 10;
$pi = 3.14;
⚫ Variables can store data of different types, and different data types can do different things.
Manifold Institute of Technical Education, M.G.Road, Thrissur Ph: 0487-2382342, 8590079111
292
Type Casting
Typecasting is the explicit conversion of data type because user explicitly defines the
data type in which he wants to cast. For example the following code divide 10 by 6 , result will
be 1.666 .. float value.
To convert this float to a Integer value, need to use type casting operator (data type)
Creating constant
⚫ To create a constant, use the define() function.
Operators are used to perform operations on variables and values. PHP divides the
operators in the following groups:
• Arithmetic operators
• Assignment operators
• Comparison operators
• Increment/Decrement operators
• Logical operators
• String operators
• Array operators
• Conditional assignment operators
The PHP arithmetic operators are used with numeric values to perform common arithmetical
operations, such as addition, subtraction, multiplication etc.
<?php
$x = 5;
$y = 3;
$z = $x ** $y;
echo $z;
?>
The PHP assignment operators are used with numeric values to write a value to a variable.
The basic assignment operator in PHP is "=". It means that the left operand gets set to the value of
the assignment expression on the right.
<?php
$x = 5;
$x += 2; // $x = $x + 2
echo $x;
?>
Manifold Institute of Technical Education, M.G.Road, Thrissur Ph: 0487-2382342, 8590079111
296
The PHP comparison operators are used to compare two values (number or string):
Example:
<?php
$x = 5;
$x++; //$x increments by 1
++$x; //$x increments by 1
echo $x;
?>
<?php
$a = 10;
$b = 4;
$c = 15;
echo $a>$b and $b<$c;
?>
PHP has two operators that are specially designed for strings.
The PHP conditional assignment operators are used to set a value depending on conditions:
Example:
<?php
$a = 10;
$b = 4;
$result = $a>$b ? $a : $c;
echo $result;
?>
• Conditional Statements
• Loop Statements
Conditional Statments
The if Statement
Syntax
if (condition) {
code to be executed if condition is true;
}
Example: -
<?php
$t = 21;
if ($t < "20") {
echo "Have a good day!";
}
?>
Syntax
if (condition) {
code to be executed if condition is true;
} else {
code to be executed if condition is false;
}
<?php
$t = 10;
if ($t < "20") {
echo "Have a good day!";
} else {
echo "Have a good night!";
}
?>
The if...elseif...else statement executes different codes for more than two conditions.
Syntax
if (condition) {
code to be executed if this condition is true;
} elseif (condition) {
code to be executed if first condition is false and this condition is true;
} else {
code to be executed if all conditions are false;
}
<?php
$t = 9;
Use the switch statement to select one of many blocks of code to be executed.
Syntax
switch (n) {
case label1:
code to be executed if n=label1;
break;
case label2:
code to be executed if n=label2;
break;
Manifold Institute of Technical Education, M.G.Road, Thrissur Ph: 0487-2382342, 8590079111
303
case label3:
code to be executed if n=label3;
break;
...
default:
code to be executed if n is different from all labels;
}
<?php
$favcolor = "red";
switch ($favcolor) {
case "red":
echo "Your favorite color is red!";
break;
case "blue":
echo "Your favorite color is blue!";
break;
case "green":
echo "Your favorite color is green!";
break;
default:
echo "Your favorite color is neither red, blue, nor green!";
}
?>
PHP Loops
Often when you write code, you want the same block of code to run over and over again a
certain number of times. So, instead of adding several almost equal code-lines in a script, we can
use loops.
The while loop executes a block of code as long as the specified condition is true.
Syntax
while (condition is true) {
code to be executed;
}
<?php
$x = 1;
while($x <= 5) {
echo "The number is: $x <br>";
$x++;
}
?>
do...while Loop
The do...while loop will always execute the block of code once, it will then check the
condition, and repeat the loop while the specified condition is true.
Syntax
do {
code to be executed;
} while (condition is true);
<?php
$x = 1;
do {
echo "The number is: $x <br>";
$x++;
} while ($x <= 5);
?>
The for loop is used when you know in advance how many times the script should run.
Syntax
for (init counter; test counter; increment counter) {
code to be executed for each iteration;
}
<?php
for ($x = 0; $x <= 10; $x++) {
echo "The number is: $x <br>";
}
?>
Indexed arrays
Empty Array
$fruits = array();
1. count() function used to get the length of the array.
2. for loop can be used to run through the array elements.
⚫ The foreach loop - Loops through a block of code for each element in an array.
⚫ Associative arrays are arrays that use named keys that you assign to them.
To loop through and print all the values of an associative array, you could use a foreach loop.
Besides the built-in PHP functions, it is possible to create your own functions.
• A function is a block of statements that can be used repeatedly in a program.
• A function will not execute automatically when a page loads.
• A function will be executed by a call to the function.
• In PHP functions are created using function keyword.
Syntax
function functionName() {
code to be executed;
}
Example:
<?php
function writeMsg() {
echo "Hello world!";
}
Information can be passed to functions through arguments. An argument is just like a variable.
Arguments are specified after the function name, inside the parentheses. You can add as many
arguments as you want, just separate them with a comma.
<?php
function add($a,$b) {
echo $a + $b;
}
add(4,5);
add(11,23);
?>
Example:
<?php
function add($a=0,$b=0) {
echo $a + $b;
}
add(4); //Result = 4
add(2,2); //Result = 4
?>
<?php
function add($a,$b) {
return $a + $b;
}
$result1=add(4,1); //Result = 5
$result2=add(2,2); //Result = 4
?>
<?php
function add_five(&$value) {
$value += 5;
}
$num = 2;
add_five($num);
echo $num;
?>
⚫ PHP is an object-oriented programming language, which means that you can create objects,
which can contain variables and functions. When creating a program to use objects, you
need to design a composite of data and code called a class.
⚫ Each new object based on this class is called an instance (or occurrence) of that class.
⚫ The data associated with an object are called its properties;
⚫ the functions it uses are called methods.
⚫ In defining a class, you supply the names of its properties and the code or its methods.
Declaring a Class
⚫ Before you can use an object, you must define a class with the class keyword.
⚫ Class definitions contain the class name (which is case-sensitive), its properties, and its
methods.
<?php
class Fruit {
// Properties
public $name;
public $color;
// Methods
function set_name($name) {
$this->name = $name;
}
function get_name() {
return $this->name;
}
}
echo $apple->get_name();
echo "<br>";
echo $banana->get_name();
?>
A constructor allows you to initialize an object's properties upon creation of the object. If you
create a __construct() function, PHP will automatically call this function when you create an object
from a class.
Notice that the construct function starts with two underscores (__)!
<?php
class Fruit {
public $name;
public $color;
function __construct($name) {
$this->name = $name;
}
function get_name() {
return $this->name;
}
}
A destructor is called when the object is destructed or the script is stopped or exited. If you create
a __destruct() function, PHP will automatically call this function at the end of the script.
Notice that the destruct function starts with two underscores (__)!
<?php
class Fruit {
public $name;
public $color;
function __construct($name) {
$this->name = $name;
}
function __destruct() {
echo "The fruit is {$this->name}.";
}
}
Access Modifiers
Properties and methods can have access modifiers which control where they can be accessed.
There are three access modifiers:
• public - the property or method can be accessed from everywhere. This is default
• protected - the property or method can be accessed within the class and by classes derived
from that class
• private - the property or method can ONLY be accessed within the class
Example
<?php
class Fruit {
public $name;
protected $color;
private $weight;
}
Inheritance
The child class will inherit all the public and protected properties and methods from the parent
class. In addition, it can have its own properties and methods.
<?php
class Fruit {
public $name;
public $color;
public function __construct($name, $color) {
$this->name = $name;
$this->color = $color;
}
public function intro() {
echo "The fruit is {$this->name} and the color is {$this->color}.";
}
}
⚫ Several predefined variables in PHP are "superglobals", which means that they are always
accessible, regardless of scope.
⚫ These variables are associative arrays by default. It maintains data as Name– Value pairs.
⚫ Few PHP superglobal variables are:
⚫ $_POST
⚫ $_GET
⚫ $_REQUEST
⚫ $_SERVER
⚫ $_COOKIE
⚫ $_SESSION
⚫ $GLOBALS
⚫ $_FILES
$GLOBALS
$GLOBALS is a PHP super global variable which is used to access global variables from anywhere
in the PHP script
<?php
$x = 75;
$y = 25;
function addition() {
$GLOBALS['z'] = $GLOBALS['x'] + $GLOBALS['y'];
}
addition();
echo $z;
?>
$_SERVER
$_SERVER is a PHP super global variable which holds information about headers, paths, and
script locations.
<?php
echo $_SERVER['PHP_SELF'];
echo "<br>";
echo $_SERVER['SERVER_NAME'];
echo "<br>";
echo $_SERVER['HTTP_HOST'];
echo "<br>";
echo $_SERVER['HTTP_REFERER'];
echo "<br>";
echo $_SERVER['HTTP_USER_AGENT'];
echo "<br>";
echo $_SERVER['SCRIPT_NAME'];
?>
$_SESSION
$_SESSION is a PHP super global variable which is used for Server Side Session Tracking.
$_COOKIES
$_COOKIES is a PHP super global variable which is used for Client Side Session Tracking.
$_FILES
$_FILES is a PHP super global variable which is used for File Uploading.
$_GET
$_GET is a PHP super global variable which is used for receiving data from HTML form that is
send by HTML Form method = GET
$_POST
$_POST is a PHP super global variable which is used for receiving data from HTML form that is
send by HTML Form method = POST
$_REQUEST
$_R EQUEST is a PHP super global variable which is used for receiving data from HTML form
that is send by HTML Form method = GET / method=POST
The PHP superglobals $_GET, $_POST and $_REQUEST is used to collect form-data.
The example below displays a simple HTML form with two input fields and a submit button:
<html>
<body>
</body>
</html>
When the user fills out the form above and clicks the submit button, the form data is sent for
processing to a PHP file named "welcome.php". The form data is sent with the HTTP POST
method.
To display the submitted data you could simply echo all the variables. The "welcome.php" looks
like this:
<html>
<body>
</body>
</html>
<html>
<body>
</body>
</html>
<html>
<body>
</body>
</html>
It is used to read form data send by HTTP method both GET and POST
<html>
<body>
</body>
</html>
<html>
<body>
</body>
</html>
A session is a way to store information (in variables) to be used across multiple pages. A
session is started with the session_start() function.
Session variables are set with the PHP global variable: $_SESSION.
Example:
The following example illustrate the usage of $_SESSION super global variable for tracking the
user id in all pages of the application.
Home.php [This PHP program collect the User Name using HTML form which will be
submitted to first.php.
<!DOCTYPE html>
<html>
<head>
<title>TODO supply a title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-
scale=1.0">
</head>
<body>
<form action="first.php" method="POST">
Name : <input type="text" name ="name"><br><br>
<input type="submit" value="submit">
</form>
</body>
</html>
In First.php program, the username is read by $_POST variable and it will be stored in a
Session variable using $_SESSION. In order to use this global variabe, the session_start()
method must be called.
<!DOCTYPE html>
<html>
<head>