0% found this document useful (0 votes)
2 views

Chapter 10 Server Side Scripting Using PHP

Chapter 10 covers server-side scripting using PHP, highlighting its benefits such as speed, cross-platform compatibility, and ease of learning. It explains the structure of PHP scripts, data types, operators, control structures, and arrays, along with examples for clarity. Additionally, it discusses the three-tier architecture in PHP and the differences between GET and POST methods for data submission.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Chapter 10 Server Side Scripting Using PHP

Chapter 10 covers server-side scripting using PHP, highlighting its benefits such as speed, cross-platform compatibility, and ease of learning. It explains the structure of PHP scripts, data types, operators, control structures, and arrays, along with examples for clarity. Additionally, it discusses the three-tier architecture in PHP and the differences between GET and POST methods for data submission.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 38

Chapter 10

Server Side Scripting


Using PHP
(3 marks)

Manu Ratheesh – GTHSS, Kattappana 1


Server side scripts
• Server side scripting languages: ASP, JSP, PHP
• It is used for creating dynamic web pages.
• PHP stands for PHP Hypertext Preprocessor.

Benefits of using PHP

1. Faster than other scripting languages.


2. It can run across Windows, Linux or Mac servers.
3. It supports most current web servers.
4. It is simple and easy to learn.

Manu Ratheesh – GTHSS, Kattappana 2


Writing a PHP script
• A PHP script begins with <?php and ends with ?>
• File extension is .php
• Each line is terminated with a semi colon (;)

<?php
echo “My First PHP Program”;
?>

Manu Ratheesh – GTHSS, Kattappana 3


PHP Script in HTML document

Manu Ratheesh – GTHSS, Kattappana 4


Comments in PHP
• Two types of comments: Single line comment (//)

• Multi line comments (/* */)

Manu Ratheesh – GTHSS, Kattappana 5


Fundamentals of PHP
I. Variables
• In PHP, a variable name starts with the $ (dollar)
sign.
• In PHP, a variable does not need to be declared.

Ex: <?php
$a = “Hari”;
$b = 25;
?>

Manu Ratheesh – GTHSS, Kattappana 6


Rules for constructing variable names

1. A variable name must start with the $, followed by


the variable name.

2. Name can be a combination of letters, digits and


underscore (_).

3. The first character must be a letter or


underscore(_)

4. No other characters are allowed.

5. Uppercase and lower case letters are treated


differently.

7
II. Data types

Manu Ratheesh – GTHSS, Kattappana 8


a. Core data types
1. Integer: These are numbers without decimal point.
(Ex: -23, 34)

2. Float: These are numbers with decimal point.


(Ex: -34.5, 1.34)

3. String: It is a group of characters enclosed within


double quotes. Ex: “Apple”

4. Boolean: It has two values: true and false.

Manu Ratheesh – GTHSS, Kattappana 9


b. Special data types
1. Null: It can have only one special value: null
$x = null;

2. Array: It can store multiple values. In PHP, we have 3


types of arrays: Indexed array, Associative array and
Multidimensional array.

3. Object: It is similar to an object in C++. An object can


contain data and functions.

4. Resources: These are used to store addresses.


(like pointer)

Manu Ratheesh – GTHSS, Kattappana 10


III. Operators in PHP
1. Assignment operators
2. Arithmetic operators
3. Relational operators
4. Logical operators
5. Arithmetic assignment operators
6. Increment and decrement operators
7. String operators

Manu Ratheesh – GTHSS, Kattappana 11


1. Assignment operator (=)

2. Arithmetic operators: + , - , *, /, %

3. Relational operators: ==, !=, <, >, >=, <=

4. Logical operators:
Logical AND operator: &&
Logical OR operator: ||
Logical NOT operator: !

5. Arithmetic assignment operators:


+=, -=, *=, /=, %=

Manu Ratheesh – GTHSS, Kattappana 12


6. Increment and decrement operators:
++ and ––
Ex: $x = 2;
$x++;
echo $x; //displays 3

Escape sequences:\n, \t, \b etc..

Manu Ratheesh – GTHSS, Kattappana 13


7. String operators

• There are two types of string operators


1. Concatenating operator (.) : It joins two strings
into a single string.
Ex: $s1 = “Good”;
$s2 = “ Morning”;
echo $s1.$s2;

//displays Good Morning

2. Concatenating assignment operator (.=)

Ex: $s1 = “Good”;


$s2 = “ Morning”;
$s1 .= $s2; //$s1 = $s1.$s2;
echo $s1; //displays “Good Morning”
Manu Ratheesh – GTHSS, Kattappana 14
Output functions in PHP
• In PHP, two functions echo and print are used to
display output on a web page.

• The echo and print can be used with or without


parenthesis.

Manu Ratheesh – GTHSS, Kattappana 15


Difference between echo and print

echo print
echo can take more than Takes only one value
one values when used
without parenthesis.
Does not return any value Returns TRUE on
successful print, FALSE if
unable to print.
Little faster than print Little bit slower than
echo

Manu Ratheesh – GTHSS, Kattappana 16


var_dump()

• Displays both data type and value of variables.


Ex: $a = 40;
var_dump($a); //displays int(40)

$b = “Apple”;
var_dump($b);
//displays string(5) “Apple”

Manu Ratheesh – GTHSS, Kattappana 17


Control structures in PHP
I. Conditional statements:
1. if else
2. if statement
3. switch statement

II. Loops in PHP


1. while loop
2. do-while loop
3. for loop

Manu Ratheesh – GTHSS, Kattappana 18


I. Conditional statements in PHP
a) if else statement
Ex: <?php
$mark = 30;
if($mark >= 40)
echo “Passed”;
else
echo “Failed”;
?>
Syntax:
<?php
if(condition)
{
//Statements to be executed if the condition is
true
}
else
{
//Statements to be executed if the condition is
false.
}
?>
Manu Ratheesh – GTHSS, Kattappana 19
b) if statement

Ex: <?php
$mark = 30;
If ($mark > = 40)
{
echo “Passed”;
}
?>

Syntax:
<?php
if(condition)
{
//Statements to be executed if the
condition is true
}
?> Manu Ratheesh – GTHSS, Kattappana 20
c) switch statement
Ex: Print the day name corresponding to a day number

<?php
$d =2;
switch($d)
{
case 1: echo “Sunday”;
break;
case 2: echo “Monday”;
break;
……………………………………
……………………………………
……………………………………
default:
echo “invalid”;
}
?>

Manu Ratheesh – GTHSS, Kattappana 21


II. Loops in PHP
a) for loop
Ex: To print the numbers from 1 to 100.
<?php
for($i = 1; $i <=100; $i ++)
{
echo $i;
}
?>

Syntax:
for(initialization; condition; update)
{
Statements
}

Manu Ratheesh – GTHSS, Kattappana 22


b) while loop

Ex: To print the numbers from 1 to 100.


<?php
$i = 1;
while($i<=100)
{
echo $i;
$i ++;
}
?>

Syntax:
initialization;
while(condition)
{
Statements;
update statement;
}

Manu Ratheesh – GTHSS, Kattappana 23


Ex: To print the even numbers within 100.
<?php
for($i = 2; $i<=100; $i = $i + 2)
{
echo $i;
}
?>

Manu Ratheesh – GTHSS, Kattappana 24


c) do - while loop
Ex: To print the numbers from 1 to 100.
<?php
$i = 1;
do
{
echo $i;
$i ++;
}while($i<=100);
?>

Syntax:
initialization;
do
{
Statements
update;
} while(condition);
Manu Ratheesh – GTHSS, Kattappana 25
Arrays in PHP
• An array is a collection of same type of data.

• In PHP, we can use either non-negative integers or


strings as index.

Two types of arrays in PHP:


1. Indexed arrays
2. Associative arrays

Manu Ratheesh – GTHSS, Kattappana 26


1. Indexed arrays
• Arrays with numeric index are called indexed arrays.
• They use non-negative integers as index.
• By default, array index starts from zero.
• In PHP, the function array() can be used to create an array.

Syntax: $arrayname = array (value1, value2, value3….);

Ex1: $A = array(40, 60, 50, 70, 30);

echo $A[1]; //displays 60


echo $A[3]; //displays 70

Ex2: $colors = array(“Red”, “Blue”, “Grey”);

echo $colors[1]; //displays Blue


echo $colors[0]; //displays Red

Manu Ratheesh – GTHSS, Kattappana 27


2. Associative arrays

• Arrays with string /named index are called associative


arrays.

Ex1:
$age = array(“Joe” => ”35”, “John” => ”30”, “Mini” => ”28”);

echo $age[“John”]; //displays 30

Ex2:
$cost = array(“Pen” => ”15”, “Pencil” => ”10”, “Brush” => ”20”);

echo $cost[“Pen”]; //displays 15

Manu Ratheesh – GTHSS, Kattappana 28


Functions in PHP

Two types of functions in PHP:


1. User defined functions
2. String functions

Manu Ratheesh – GTHSS, Kattappana 29


1. User defined functions in PHP
• It starts with the word “function”.
• Syntax: function functionName()
{
//statements
}

Ex: <?php
function sample()
{
echo “Hello..welcome !!!”;
}
sample(); //function call
?>
Manu Ratheesh – GTHSS, Kattappana 30
2. String functions in PHP

a) chr() : It takes an ASCII value as input and returns the


equivalent character.
Ex: chr(65) returns A
Syntax: chr(ASCII)

b) strlen(): Returns the length of a string.


Ex: strlen(“apple”) returns 5
Syntax: strlen(string)

c) strcmp(): Compares two strings.


Ex: strcmp(“He”, “She”) returns false.
Syntax: strcmp(string1, string2)

d) strrev(): Reverses the given string.


Ex: strrev(“Hello”) returns olleH
Syntax: strrev(string)

Manu Ratheesh – GTHSS, Kattappana 31


Three tier architecture in PHP
• It is an architecture with three layers/tiers.
• Browser (client), Web Server and Database are the 3 layers in a
web based application.

Manu Ratheesh – GTHSS, Kattappana 32


Tier 1: Browser sends URL to the server. Server sends back the
html file of the web page. Browser renders it to display the web
page.

Tier 2: Web server accepts the request from the browser, process
it and sends back the output to browser.

Tier 3: It consists of a database and the DBMS software that


manages the database. They communicate using SQL.

Manu Ratheesh – GTHSS, Kattappana 33


Difference between GET and POST methods

GET POST
The data visible in the The data is not visible in the
address bar. address bar.
Data is submitted as part of Data is submitted as part of
the URL http request.
Data sending is not secure. Data sending is secure.

Manu Ratheesh – GTHSS, Kattappana 34


2023

2022

2022 Say

2021

Manu Ratheesh – GTHSS, Kattappana 35


2020

2023 Say

2021 Say

Manu Ratheesh – GTHSS, Kattappana 36


2020 Say

Manu Ratheesh – GTHSS, Kattappana 37

You might also like