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

unit4_php

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

unit4_php

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 43

Unit-4

XML : Introduction to XML, uses of XML, simple XML, XML


key components, DTD and Schemas, Using XML with
application. Transforming XML using XSL and XSLT.
PHP: Introduction and basic syntax of PHP, decision and
looping with examples, PHP and HTML, Arrays, Functions,
Browser control and detection, string, Form processing, Files,
Advance Features: Cookies and Sessions, Object Oriented
Programming with PHP
What is PHP ?
 PHP is an acronym for "PHP: Hypertext Preprocessor.

 PHP is a widely-used, open source scripting language.

 PHP scripts are executed on the server.

 PHP is a server scripting language, and a powerful tool


for making dynamic and interactive Web pages.
 You can embed PHP scripting within normal Html
coding.

 PHP includes a comprehensive set of database


access functions.

 High Performance/ease of learning/low cost.

 Platform Independent
What is a PHP File?

 PHP files can contain text, HTML, CSS, JavaScript,


and PHP code.

 PHP code is executed on the server, and the result is


returned to the browser as plain HTML.

 PHP files have extension ".php"


Characteristics of PHP

 Simplicity

 Efficiency

 Security

 Flexibility

 Familiarity
PHP Syntax
 A PHP script can be placed anywhere in the
document.

 A PHP script starts with <?php and ends with ?>:

 <?php
// PHP code goes here
?>
PHP Case Sensitivity
 In PHP, keywords (e.g. if, else, while, echo, etc.), classes,
functions, and user-defined functions are not case-sensitive.
 In the example below, all three echo statements below are equal
and legal:
 Example
<!DOCTYPE html>
<html>
<body>
<?php
ECHO "Hello World!<br>";
echo "Hello World!<br>";
EcHo "Hello World!<br>";
?></body>
</html>
Comments in PHP

 A comment in PHP code is a line that is not executed as a part of


the program.

 Its only purpose is to be read by someone who is looking at the


code.

 <?php
// This is a single-line comment

# This is also a single-line comment


?>
 <?php
/*
This is a multiple-lines comment block
that spans over multiple
lines
*/
?>
PHP Variables

 Variables are "containers" for storing information.

Creating (Declaring) PHP Variables

 In PHP, a variable starts with the $ sign, followed by


the name of the variable:
Rules for PHP variables:

 A variable starts with the $ sign, followed by the


name of the variable
 A variable name must start with a letter or the
underscore character
 A variable name cannot start with a number
 A variable name can only contain alpha-numeric
characters and underscores (A-z, 0-9, and _ )
 Variablenames are case-sensitive ($age and $AGE are
two different variables)
PHP Variables Scope

 In PHP, variables can be declared anywhere in the


script.
 The scope of a variable is the part of the script where
the variable can be referenced/used.
 PHP has three different variable scopes:
 local
 global
 static
 A variable declared outside a function has a GLOBAL
SCOPE and can only be accessed outside a function:

<?php
$x = 5; // global scope
function myTest();
echo "<p>Variable x outside function is: $x</p>";
?>
 A variable declared within a function has a LOCAL
SCOPE and can only be accessed within that
function:

<?php
function myTest() {
$x = 5; // local scope
echo "<p>Variable x inside function is: $x</p>";
}
?>
 use the static keyword when you first declare the variable

<?php
function myTest() {
static $x = 0;
echo $x;
$x++;
}

myTest();
echo "<br>";
myTest();
echo "<br>";
myTest();
?>
PHP echo and print Statements

 There are two basic ways to get


output: echo and print.

 The differences are small: echo has no return value


while print has a return value of 1 so it can be used in
expressions.

 echo can take multiple parameters (although such


usage is rare) while print can take one
argument. echo is marginally faster than print.
PHP Decision & Looping
 PHP allows us to perform actions based on some type of
conditions that may be logical or comparative. Based on the
result of these conditions i.e., either TRUE or FALSE, an
action would be performed as asked by the user.
 PHP provides us with four 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
• if (condition){
// if TRUE then execute this code
}
Eg:
<?php
$x = 12;
if ($x > 0) {
echo "The number is positive";
}
?>
• if (condition) {
// if TRUE then execute this code
}
else{
// if FALSE then execute this code
}
• ex:
<?php
$x = -12;
if ($x > 0) {
echo "The number is positive";
}
else{
echo "The number is negative";
}
?>
• if (condition) {
// if TRUE then execute this code
}
elseif
{
// if TRUE then execute this code
}
elseif {
// if TRUE then execute this code
}
else {
// if FALSE then execute this code
}
• <?php
$x = "August";
if ($x == "January") {
echo "Happy Republic Day";
}
elseif ($x == "August") {
echo "Happy Independence Day!!!";
}
else{
echo "Nothing to show";
}
?>
switch(n) {
case statement1:
code to be executed if n==statement1;
break;
case statement2:
code to be executed if n==statement2;
break;
case statement3:
code to be executed if n==statement3;
break;
case statement4:
code to be executed if n==statement4;
break;
......
default: code to be executed if n != any case;
 Loops are used to execute the same block of code
again and again, as long as a certain condition is true.
In PHP, we have the following loop types:
 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
• while (condition is true) {
code to be executed;
}
The example below displays the numbers from 1 to 5:
• <?php
$x = 1;

while($x <= 5) {
echo "The number is: $x <br>";
$x++;
}
?>
 $x = 1; - Initialize the loop counter ($x), and set the
start value to 1
 $x <= 5 - Continue the loop as long as $x is less than
or equal to 5
 $x++; - Increase the loop counter value by 1 for each
iteration
• 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 Arrays
 An array stores multiple values in one single variable.

 <?php
$cars = array("Volvo", "BMW", "Toyota");
echo "I like " . $cars[0] . ", " . $cars[1] . " and " . $cars[2] .
".";
?>
Create an Array in PHP
 In PHP, the array() function is used to create an array:
array();
 In PHP, there are three types of arrays:

 Indexed arrays - Arrays with a numeric index

 Associative arrays - Arrays with named keys

 Multidimensional arrays - Arrays containing one or


more arrays
 There are two ways to create indexed arrays:
 The index can be assigned automatically (index
always starts at 0), like this:
 $cars = array("Volvo", "BMW", "Toyota");
 or the index can be assigned manually:
 $cars[0] = "Volvo";
$cars[1] = "BMW";
$cars[2] = "Toyota“;
 Loop Through an Indexed Array.
<?php
$cars = array("Volvo", "BMW", "Toyota");
$arrlength = count($cars);
for($x = 0; $x < $arrlength; $x++) {
echo $cars[$x];
echo "<br>";
}
?>
PHP FUNCTIONS
 PHP functions are similar to other programming
languages.
 A function is a piece of code which takes one more
input in the form of parameter and does some
processing and returns a value.
• Types of Functions:
Built-in- function.
User-Defined- function.
 PHP has over 1000 built-in functions that can be
called directly, from within a script, to perform a
specific task.

 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.
Create a User Defined Function in PHP
 A user-defined function declaration starts with the
word function:
Syntax
function functionName() {
code to be executed;
}
PHP Strings
 A string is a sequence of characters, like "Hello
world!“
 PHP String Functions:

 strlen() - Return the Length of a String


 str_word_count() - Count Words in a String
 strrev() - Reverse a String
 strpos() - Search For a Text Within a String
 str_replace() - Replace Text Within a String
PHP Form Handling
 We can create and use forms in PHP. To get form data, we need to use PHP
superglobals $_GET and $_POST.

 The form request may be get or post. To retrieve data from get request, we
need to use $_GET, for post request $_POST.

PHP Get Form:

 Get request is the default form request. The data passed through
get request is visible on the URL browser so it is not secured. You
can send limited amount of data through get request.
PHP Post Form

 Post request is widely used to submit form that have


large amount of data such as file upload, image upload,
login form, registration form etc.

 The data passed through post request is not visible on


the URL browser so it is secured. You can send large
amount of data through post request.
PHP FILE HANDLING

• PHP File System allows us to create file, read file line


by line, read file character by character, write file,
append file, delete file and close file

PHP Open File - fopen()


• The PHP fopen() function is used to open a file.
• resource fopen ( string $filename , string $mode [, bo
ol $use_include_path = false [, resource $context ]] )
PHP Cookie
 PHP cookie is a small piece of information which is
stored at client browser.
 It is used to recognize the user.
 Cookie is created at server side and saved to client
browser.
 Each time when client sends request to the server,
cookie is embedded with request. Such way, cookie
can be received at the server side.
 PHP transparently support HTTP cookies. Cookies
are a mechanism for storing data in the remote
browser & thus tracking or identifying return users.
Cookies are part of HTTP header.

• Creating Cookies
Object Oriented Programming with PHP

• Classes.
• Objects.
• Inheritance.
• Encapsulation.
Class & Objects
 A class is a template for objects, and an object is an instance of class.

 A class is defined by using the class keyword, followed by the name


of the class and a pair of curly braces ({}). All its properties and
methods go inside the braces:
 Syntax:
<?php
class Fruit {
// code goes here...
}
?>
In a class, variables are called properties and functions are called methods!
 Classes are nothing without objects! We can create multiple
objects from a class.

 Each object has all the properties and methods defined in the
class, but they will have different property values.

 Objects of a class is created using the new keyword.

 In the example below, $apple and $banana are instances of the


class Fruit:
Inheritance

 Inheritance in OOP = When a class derives from


another class.

 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.

 An inherited class is defined by using the extends


keyword.

You might also like