Web Programming Ktu Notes
Web Programming Ktu Notes
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
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
<?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;
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);
$a = 10;
$b = “10";
echo "Result 2: " . ($a===$b) . "<br>";
?>
Example 2
<> operator
<?php
$a = 10;
$b = 5;
$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)
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. 05
B. 55
C. 50
D. Error