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

WT Module 5

Uploaded by

Devansh Shukla
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)
24 views

WT Module 5

Uploaded by

Devansh Shukla
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/ 49

Mahatma Education Society’s

Pillai HOC College of Engineering and


Technology, Rasayani
Department of Electronics and Computer
Science

Module 5: Server Side


Scripting (PHP)

By Pooja Shukre-Kulkarni
Table of Contents
 Introduction to PHP
 PHP Tags
 PHP Variables
 Data types
 Operators
 Conditionals
 Control Structures
 Iteration constructs
 Using arrays
 String manipulations and regular expressions
 PHP Forms
 Session Control in PHP
 Designing and creating your web database
 Accessing MySQL database from the web with PHP
 Session Control in PHP
Introduction to PHP
 The full form of PHP is Hypertext Preprocessor. It was
abbreviated previously as Personal Home Page.

 It is the server-side scripting language encoded with


HTML to develop Dynamic website, Static website or
Web applications.

 The main difference between PHP and HTML is HTML


codes are rendered on the browser directly, whereas
PHP codes are executed on the server.

 Initially, PHP codes are executed on a server and the


result is transferred to the browser afterwards.
Specification of PHP
 PHP design is simple since libraries like C or C++ are
not used.
 It contains plenty of predefined functions to protect
your information.
 It is an open-source language so that it can be freely
downloaded.
 In addition to session management features, PHP uses
resource allocation mechanisms and object-oriented
programming.
 PHP supports many authentication functions to back
up the data.
 It is a very flexible language because it can be
embedded in many other languages, including CSS,
HTML, JavaScript, XML.
 PHP code can also be executed on any device such as
Tabs Phone, Laptops etc.
Advantages of PHP
 It is integrated and database with many other
programming languages such as CSS, HTML,
JavaScript etc.

 All Operating Systems such as Linux, Windows, Unix,


etc. support PHP.

 Connecting to the database is easy to store and retrieve


data. PHP can also be built via multiple databases.

 Compared with other scripting languages, it is the


speediest programming language.

 The PHP structures and tools are used to safeguard


web apps from external attacks and threats.
Disadvantages of PHP
 Error handling is not suitable for a PHP framework.

 Since its maintenance is difficult, it is not appropriate


for broad applications.

 PHP is open-source so all programmers can see its


code. If there are any bugs in the program code, then its
weak point can be discovered by other programmers.
PHP Tags
 Default syntax
 Short open Tags
 Omit the PHP closing tag at the end of the file

 Default Syntax
The default syntax starts with "<? php" and ends with
"?>".
Example:

<?php
echo "Default Syntax";
?>
 Short open Tags
The short tags starts with "<?" and ends with "?>". Short
style tags are only available when they are enabled in
php.ini configuration file on servers.
Example:
<?
echo "PHP example with short-tags";
?>
 Omit the PHP closing tag at the end of the file
It is recommended that a closing PHP tag shall be omitted
in a file containing only PHP code so that occurrences of
accidental whitespace or new lines being added after the
PHP closing tag, which may start output buffering
causing uncalled for effects can be avoided.

Example:
<?php
echo "PHP example with short-tags";
PHP Statement separation
 In PHP, statements are terminated by a semicolon (;)
like C or Perl. The closing tag of a block of PHP code
automatically implies a semicolon, there is no need to
have a semicolon terminating the last line of a PHP
block.
Example
<?php
echo 'This is a test string';
?>
 In the above example, both semicolon(;) and a closing
PHP tag are present.

<?php echo 'This is a test' ?>


 In the above example, there is no semicolon(;) after the
last instruction but a closing PHP tag is present.
PHP Case sensitivity
 In PHP the user defined functions, classes, core language
keywords (for example if, else, while, echo etc.) are case-
insensitive.
Example
<?php
echo("We are learning PHP case sensitivity <br/>");
ECHO("We are learning PHP case sensitivity <br/>");
EcHo("We are learning PHP case sensitivity <br/>");
?>
Output:
We are learning PHP case sensitivity
We are learning PHP case sensitivity
We are learning PHP case sensitivity
On the other hand, all variables are case-sensitive.
Example
<?php
$amount = 200;
echo("The Amount is : $amount <br />");
echo("The Amount is : $AMOUNT <br />");
echo("The Amount is : $amoUNT <br />");
?>
Output:
The Amount is : 200
The Amount is :
The Amount is :
PHP Variables
 In PHP, a variable starts with the $ sign, followed by
the name of the variable.

Example:

<?php
$txt = "Hello world!";
$x = 5;
$y = 10.5;
?>
Output Variables
 The PHP echo statement is often used to output data
to the screen.

Example:

<?php
$txt = "W3Schools.com";
echo "I love $txt!";
?>

OR
<?php
$txt = "W3Schools.com";
echo "I love " . $txt . "!";
?>
 PHP is a Loosely Typed Language
 In the example above, notice that we did not have
to tell PHP which data type the variable is.
 PHP automatically associates a data type to the
variable, depending on its value. Since the data
types are not set in a strict sense, you can do
things like adding a string to an integer without
causing an error.
 In PHP 7, type declarations were added. This gives
an option to specify the data type expected when
declaring a function, and by enabling the strict
requirement, it will throw a "Fatal Error" on a type
mismatch.
 You will learn more about strict and non-
strict requirements, and data type declarations in
the PHP Functions chapter.
Scope of PHP Variables
 PHP has three different variable scopes:
1. local
2. global
3. Static

Example (Global Scope)


<?php
$x = 5; // global scope
function myTest()
{
echo "Variable x inside function is: $x </br>";//error
}
myTest();
echo "<p>Variable x outside function is: $x</p>";
?>
Example (Local Scope)
<?php
function myTest()
{
$x = 5; // local scope
echo "<p>Variable x inside function is: $x</p>";
}

myTest();

// using x outside the function will generate an


error

echo "<p>Variable x outside function is: $x</p>";


?>
The ‘global’ keyword
 The global keyword is used to access a global
variable from within a function.
<?php
$x = 5;
$y = 10;

function myTest()
{
global $x, $y;
$y = $x + $y;
}

myTest();
echo $y; // outputs 15
?>
The ‘static’ Keyword
 When a function is completed/executed, all of its
variables are deleted. However, sometimes we want a
local variable NOT to be deleted. We need it for a
further job.

 To do this, use the static keyword when you first


declare the variable:
Example:
<?php
function myTest() {
static $x = 0;
echo $x;
$x++;
}
myTest();
myTest();
myTest();
?>
PHP print statement
<?php
$txt1 = "Learn PHP";
$txt2 = "W3Schools.com";
$x = 5;
$y = 4;
print "<h2>" . $txt1 . "</h2>";
print "Study PHP at " . $txt2 . "<br>";
print $x + $y;
?>

Learn PHP
Study PHP at W3Schools.com
9
PHP Data Types
 String : $x = "Hello world!";
 Integer : $x = 5985;
 Float (also called double) : $x = 10.365;
 Boolean : $x = true; $y = false;
 Array :$cars= array("Volvo","BMW","Toyota");
 Object
 NULL
 Resource
Classes and Objects
 Classes and objects are the two main aspects of object-oriented programming.
 A class is a template for objects, and an object is an instance of a class.
 When the individual objects are created, they inherit all the properties and
behaviors from the class, but each object will have different values for the
properties.
 Let's assume we have a class named Car. A Car can have properties like
model, color, etc. We can define variables like $model, $color, and so on, to
hold the values of these properties.
 When the individual objects (Volvo, BMW, Toyota, etc.) are created, they
inherit all the properties and behaviors from the class, but each object will
have different values for the properties.
 If you create a __construct() function, PHP will automatically call this function
when you create an object from a class.
<?php
class Car {
public $color;
public $model;
public function __construct($color, $model) {
$this->color = $color;
$this->model = $model;
}
public function message() {
return "My car is a " . $this->color . "
" . $this->model . "!";
}
}

$myCar = new Car("black", "Volvo");


echo $myCar -> message();
echo "<br>";
$myCar = new Car("red", "Toyota");
echo $myCar -> message();
?>
PHP Operators
 Arithmetic operators
 Assignment operators
 Comparison operators
 Increment/Decrement operators
 Logical operators
 String operators
 Array operators
 Conditional assignment operators
Arithmetic operators
Operator Name Example Result

+ Addition $x + $y Sum of $x and $y

- Subtraction $x - $y Difference of $x and $y

* Multiplicatio $x * $y Product of $x and $y


n

/ Division $x / $y Quotient of $x and $y

% Modulus $x % $y Remainder of $x divided


by $y

** Exponentiati $x ** $y Result of raising $x to


on the $y'th power
Assignment Operators
Assignme Same as... Description
nt

x=y x=y The left operand gets set to the value of


the expression on the right
x += y x=x+y Addition

x -= y x=x-y Subtraction

x *= y x=x*y Multiplication

x /= y x=x/y Division

x %= y x=x%y Modulus
PHP String Operators
Operator Name Example Result

. Concatenation $txt1 . $txt2 Concatenation of


$txt1 and $txt2
.= Concatenation $txt1 .= $txt2 Appends $txt2 to
assignment $txt1
The PHP array operators are used to compare arrays.

PHP Array Operators


Operator Name Example Result

+ Union $x + $y Union of $x and $y


== Equality $x == $y Returns true if $x and $y
have the same key/value
pairs
=== Identity $x === $y Returns true if $x and $y
have the same key/value
pairs in the same order and of
the same types
!= Inequality $x != $y Returns true if $x is not equal
to $y
<> Inequality $x <> $y Returns true if $x is not equal
to $y
!== Non-identity $x !== $y Returns true if $x is not
identical to $y
PHP Conditional Assignment
Operators
Opera Name Example Result
tor
?: Ternary $x Returns the value of $x.
= expr1 ? expr2 : ex The value of $x
pr3 is expr2 if expr1 = TRUE.
The value of $x
is expr3 if expr1 = FALSE
?? Null $x = expr1 ?? expr2 Returns the value of $x.
coalescing The value of $x
is expr1 if expr1 exists,
and is not NULL.
If expr1 does not exist, or
is NULL, the value of $x
is expr2.
Introduced in PHP 7
PHP Conditional Statements
 if statement - executes some code if one condition is
true

 if...else statement - executes some code if a condition is


true and another code if that condition is false

 if...elseif...else statement - executes different codes for


more than two conditions

 switch statement - selects one of many blocks of code


to be executed
Example: if statement

<?php
$age = 62;

if ($age > 60) {


echo "The person is senior citizen";
}
?>
Example: if … else statement

<?php
$age = 6;

if ($age > 60)


{
echo " The person is senior citizen";
}
else
{
echo " The person is not a senior citizen";
}
?>
Example: if ….elseif …. else statement
<?php

$age = 6;

if ($age > 60)


{
echo " The person is senior citizen";
}
elseif ($age < 10)
{
echo " The person is a child ";
}
else
{
echo " The person is not a senior citizen";
}
?>
Example: Switch statement

<?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
 while - loops through a block of code as long as the
specified condition is true

 do...while - loops through a block of code once, and


then repeats the loop as long as the specified condition
is true

 for - loops through a block of code a specified number


of times

 foreach - loops through a block of code for each


element in an array.
Example: while loop

<?php
$x = 1;

while($x <= 5) {
echo "The number is: $x <br>";
$x++;
}
?>
Output:
The number is: 1
The number is: 2
The number is: 3
The number is: 4
The number is: 5
Example: do….while loop

<?php
$x = 1;

do {
echo "The number is: $x <br>";
$x++;
} while ($x < 2);
?>

Output:

The number is: 2


Example: for loop
Syntax:
foreach ($array as $value)
{
Statements;

}
<?php
for ($x = 0; $x <= 5; $x++) {
echo "The number is: $x <br>";
}
?>

Output:
The number is: 0
The number is: 1
The number is: 2
The number is: 3
The number is: 4
The number is: 5
Example: foreach loop

<?php
$colors
= array("red", "green", "blue", "yellow");

foreach ($colors as $value) {


echo "$value <br>";
}
?>
Example: Break and Continue
 <?php
for ($x = 0; $x < 10; $x++) {
if ($x == 4) {
break;
}
echo "The number is: $x <br>";
}
?>

 <?php
for ($x = 0; $x < 10; $x++) {
if ($x == 4) {
continue;
}
echo "The number is: $x <br>";
}
?>
PHP Functions
 PHP has more than 1000 built-in functions, and in
addition you can create your own custom functions.

<?php
function writeMsg()
{
echo "Hello world!";
}

writeMsg(); // call the function


?>
Example: A Function with arguments
<?php
function familyName($fname) {
echo "$fname Patil.<br>";
}
familyName(“Kedar");
familyName(“Priya");
familyName(“Meena");
familyName(“Seema");
familyName(“Dilip");
?>
Output:
Kedar Patil.
Priya Patil.
Meena Patil.
Seema Patil.
Dilip Patil.
<?php
function familyName($fname, $year) {
echo "$fname Patil. Born in $year <br>";
}
familyName(“Kedar", "1995");
familyName(“Meena", “2000");
familyName(“Seema", “2015");
?>
 In the example above, notice that we did not have to tell
PHP which data type the variable is.
 PHP automatically associates a data type to the variable,
depending on its value. Since the data types are not set in
a strict sense, you can do things like adding a string to an
integer without causing an error.

Output:
Kedar Patil. Born in 1995
Meena Patil. Born in 2000
Seema Patl. in 2015
Array
 An array is a special variable, which can hold more
than one similar type of values at a time.
 In PHP, there are three types of arrays:

1. Indexed arrays - Arrays with a numeric index


2. Associative arrays - Arrays with named keys
3. Multidimensional arrays - Arrays containing one or
more arrays
Example: Indexed arrays

<?php
$cars = array("Volvo", "BMW", "Toyota");
echo "I like " . $cars[0] . ", " . $cars[1] . "
and " . $cars[2] . ".";
?>
Example: Associative arrays

<?php
$age
= array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43"
);
echo "Peter is " . $age['Peter'] . " years
old.";
?>
Example: Multi-dimensional arrays

<?php
$cars = array (
array("Volvo",22,18),
array("BMW",15,13),
array("Saab",5,2),
array("Land Rover",17,15)
);
echo $cars[0][0].": In stock: ".$cars[0][1].", sold:
".$cars[0][2].".<br>";
echo $cars[1][0].": In stock: ".$cars[1][1].", sold:
".$cars[1][2].".<br>";
echo $cars[2][0].": In stock: ".$cars[2][1].", sold:
".$cars[2][2].".<br>";
echo $cars[3][0].": In stock: ".$cars[3][1].", sold:
".$cars[3][2].".<br>";
?>
PHP Sessions
 A session is a way to store information (in
variables) to be used across multiple pages.
 Unlike a cookie, the information is not stored on
the users computer.
 When you work with an application, you open it,
do some changes, and then you close it.
 This is much like a Session.
 The computer knows who you are. It knows when
you start the application and when you end.
 But on the internet there is one problem: the web
server does not know who you are or what you do,
because the HTTP address doesn't maintain state.
 Session variables solve this problem by storing user
information to be used across multiple pages (e.g.
username, favorite color, etc). By default, session
variables last until the user closes the browser.

 So; Session variables hold information about one single


user, and are available to all pages in one application

 Refer the link below:

 https://www.slideshare.net/netgainssolutions/session
s-and-cookies

You might also like