100% found this document useful (1 vote)
282 views55 pages

Web Programming Ktu Notes

S7 ktu notes

Uploaded by

gnanjitha
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
100% found this document useful (1 vote)
282 views55 pages

Web Programming Ktu Notes

S7 ktu notes

Uploaded by

gnanjitha
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 55

COMPUTER SCIENCE

SUBJECT NAME : WEB TECHNOLOGY

CHAPTER NO : 8
CHAPTER NAME : Server Side Programming
with PHP
LECTURE NO: 1

1
Server Side Scripting
 Server-side scripting is a technique used in web development
which involves employing scripts on a web server which
produce a response customized for each user's (client's)
request to the website.
 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 software's.
Server Side Scripting Languages
 PHP
 JSP
 Servlets
 Node JS
 ASP.Net.
 Python
 Ruby on Rails
Dynamic Website or Web Application
 A dynamic website is one where some of the response
content is generated dynamically, only when needed.
 On a dynamic website HTML pages are normally created by
inserting data from a database into placeholders in HTML
templates.
 Most of the code to support a dynamic website must run on
the server.
 Creating this code is known as "server-side programming"
Architecture for a dynamic website

The browsers send HTTP requests to the server, then the


server processes the requests and returns appropriate
HTTP responses.
Server Software's
 Server software is a type of software that is designed to
be used, operated and managed on a computing server.
 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
Web Server Software's
 A web server is a system that delivers content or services
to end users over the internet.
 The most simple definition is that a web server runs a
website by returning HTML files over an HTTP connection.
 Examples:
 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 Server software
 Application servers provide a framework to build and deploy
web applications and offer a variety of services when running
such applications.
 IBM WebSphere Application Server
 Oracle WebLogic
 Apache Tomcat
 Apache Geronimo
 appserver.io
Web Server vs Web Application Server
Database Server Software
 A database server is a server which uses a database application that
provides database services to other computer programs or to
computers, as defined by the client–server model.
 Database management systems (DBMSs) frequently provide
database-server functionality, and some database management
systems (such as MySQL) rely exclusively on the client–server model
for database access (while others e.g. SQLite are meant for using as
an embedded database).
Configuration of Apache, MySQL and PHP
What is XAMPP?
XAMPP start for Cross-Platform, PHP, Mysql, Perl, Apache.
XAMPP is a very lightweight and simple Apache distribution

XAMPP has four main components:


 Apache: is a web server process that converts PHP files into simple HTML and
sends it to the web browser of a user. Apache is the most usable and famous server
today, with more than 50% website using it.
 MySql: means database almost all websites that are dynamic needs to store data
for that purpose we uses a database. MySQL is most famous database
management system and an open source.
 PHP: is a server-side scripting language that is used by world 70% websites as
Facebook is also an example.
 Perl: is a family of two high-level, general-purpose, interpreted, dynamic
programming languages.
Installation of XAMPP
 First go to google and type XAMPP download and click the first link
and download XAMPP and install.
 https://www.apachefriends.org/index.html
XAMPP Control Panel
 After that install you can look like as below image then you go to click
start button of Apache and MySql for starting XAMPP server.
PHP Admin Page
 Click on the Apache “Admin” button in the control panel
PHP Admin Page
 Now, we can ensure that the PHP has been installed successfully.
MySQL Admin Page
 Also, Click on the MySQL “Admin” button in the control panel
MySQL Admin Page
 The following page opens, which is a MySQL Database portal!.
 Now, you can ensure that the MySQL has been successfully installed.
IDE for PHP Web Application Development
 Step 1: Install JDK software from the following site.
 https://www.oracle.com/java/technologies/javase-jdk16-downloads.html

 Step 2: After installing JDK software, install the Apache Netbeans IDE from the
following site.
 https://netbeans.apache.org/download/nb120/nb120.htm
Create New PHP Project
Create New PHP Project
Execute the PHP Application
 Test the php Application by Right click and Select “Run File”
Overview of PHP
 PHP: Hypertext Preprocessor.
 PHP is a server-side scripting language, and a powerful tool for making
dynamic and interactive Web pages.
 It is a program resides in a web server and respond to client requests.
 It was originally created by Rasmus Lerdorf in 1994.
 He developed Perl/CGI Script Toolset, called as Personal Home Page(PHP).
 Later, renamed as Hypertext Preprocessor
Dynamic Client/Server Request/Response Sequence
 PHP server side scripting language allows us to create dynamic
Client and Server request response model as given below.
The PHP File
 PHP files may contain text, HTML, CSS, JavaScript, and PHP
code
 PHP code are executed on the server, and the result is returned
to the browser as a plain HTML
 PHP files have extension ".php"
Incorporating PHP Within HTML
 The PHP code script can be embedded inside the HTML
using the tag called <?php and ?> as given below.

<?php
statements…
…………
…………
?>
Display Data in a web browser from PHP Script
 With PHP, there are two basic ways to get output:
 echo and print.
 The PHP echo Statement
<?php
echo "Welcome to PHP Programming!!";
?>
Display Data in a web browser from PHP Script

 The print statement


<?php
print "Welcome to PHP Programming!!";
?>
Creating Comments in PHP Program
Single Line Comments: Can be created using // or # Multiline Comments

<!DOCTYPE html> <!DOCTYPE html>


<html> <html>
<body> <body>

<?php <?php
// This is a single-line comment /*
This is a multiple-lines comment block
# This is also a single-line comment that spans over multiple
?> lines
*/
</body> ?>
</html>
</body>
</html>
Creating PHP file with only PHP Code
index.php
<html>
<head>
</head>
<body>
<a href="logic.php">Get Info</a>
</body>
</html>
logic.php

<?php
echo "Real logic is here";
?>
Including PHP file in another PHP program

 It is possible to insert the content of one PHP file into another PHP file
 The include (or require) statement takes all the text/code/markup that exists
in the specified file and copies it into the file that uses the include statement.

Syntax

include 'filename';

or

require 'filename';
Example
<!DOCTYPE html>
<?php include 'logic.php' ?>
<html>
<head>

<meta charset="UTF-8">
<title></title>
</head>

<body>
<?php require 'logic.php' ?>

</body>
</html>
Creating Variables in PHP
 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;
?>
Display variable data
 We can use echo or print command to display variable data.

<?php
$name = 'John';
$number = 10;
$pi = 3.14;

echo $name;
echo $number;
echo $pi;
?>
Difference between ‘ ‘ and “ “ for string data.
 String data is most commonly used type which can be
represented in two ways.
 Using single quotes ‘ ‘
 It creates un interpretable text, where variables will not be resolved.
 Using double quotes “ “
 It creates interpretable text where variables value will be resolved.

<?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;

echo "Name: " . $name . "<br>";


echo "Number: " . $number. "<br>";
echo "PI Value: " . $pi . "<br>";
?>
Without (dot .) operator …
 Using double quotes we can perform the same concatenation.
<?php
$name = 'John';
$number = 10;
$pi = 3.14;

echo "Name: $name <br>";


echo "Number: $number <br>";
echo "PI Value: $pi <br>";
?>
PHP Data Types
 Explicit type declaration is not needed in PHP
 PHP supports the following data types:
 string
 integer
 float (floating point numbers)
 boolean
 array
 object
 NULL
 The gettype() method used to find the type of the given data.
PHP Data Types
<?php
$name = 'John';
$number = 10;
$pi = 3.14;
$flag = false;
$value = NULL ;

echo gettype($name) . "<br>";


echo gettype($number) . "<br>";
echo gettype($pi) . "<br>";
echo gettype($flag) . "<br>";
echo gettype($value) . "<br>";
?>
Type Casting
 Typecasting is the explicit conversion of data type because user explicitly defines the
data type in which he wants to cast.

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.
 define(name, value, case-insensitive)
 case-insensitive – default is false
<?php
#creating constant
define('pi',3.14);

#finding area of a circle


$radius = 5;
$area = pi * $radius ** 2;
echo $area;
?>
PHP Operators
 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
Arithmetic operators
Example
 Exponentiation Operator
<?php
$x = 5;
$y = 3;
$z = $x ** $y;
echo $z;
?>
PHP Assignment Operators
Example: Addition Assignment
<?php
$x = 5;
$x += 2; // $x = $x + 2
echo $x;
?>
PHP Comparison Operators
Example 1
=== comparison (Same type and same value)
<?php
$a = 10;
$b = 10;
echo "Result 1: " . ($a===$b) . "<br>";

$a = 10;
$b = “10";
echo "Result 2: " . ($a===$b) . "<br>";
?>
Example 2
 <> operator
<?php
$a = 10;
$b = 5;

echo "Result 1: " . ($a<>$b) . "<br>";


echo "Result 2: " . ($a!=$b) . "<br>";
?>
Comparison Operators
Example 3
 <=> spaceship operator - return 0 or -1 or +1
 Compare the order of numbers used for sorting
<?php
$a = 10;
$b = 10;

echo "Result 1: " . ($a<=>$b) . "<br>";

$a = 5;
$b = 10;
echo "Result 2: " . ($a<=>$b) . "<br>";

$a = 10;
$b = 5;
echo "Result 3: " . ($a<=>$b) . "<br>";
?>
PHP Increment / Decrement Operators

Example:

<?php
$x = 5;
$x++; //$x increments by 1
++$x; //$x increments by 1
echo $x;
?>
Questions
1. Which of the following PHP statement/statements will store 111 in variable num?
i) int $num = 111;
ii) int num = 111;
iii) $num = 111;
iv) 111 = $num;
A) All of the mentioned.
B) Both i) and ii)
C) Only i)
D) Only iii)

2. Select the statement that is to be used to include a file in PHP.


A. #include ‘filename’;
B. include ‘filename’;
C. @include ‘filename’;
D. #include <filename>;
Questions
3. What will be the output of the following PHP code?
<?php
$num = 1;
$num1 = 2;
print $num . "+". $num1;
?>

A) 1+2
4. Predict the output of the following PHP code.
B) 3
C) 1.+.2
<?php
D) Error
$string = "learning";
echo 'This is a portal for $string. ';
?>

A. This is a portal for learning


B. Error
C. No output
D. This is a portal for $string.
Questions
5. PHP scripts are enclosed within _______
A.<php> . . . </php>
B.<?php . . . ?>
C.?php . . . ?php
D.<p> . . . </p>

6. What will be the output of the following PHP code?


<?php
echo "Hello world </br> I am learning PHP at Manifold";
?>
A. Hello world
B. Hello world I am learning PHP at Manifold
C. Hello world
I am learning PHP at Manifold
D. Error
Questions
7. What will be the output of the following PHP code?
<?php
$five = 5;
print($five);
print $five;
?>

A. 05
B. 55
C. 50
D. Error

You might also like