WT UNIT-IV
WT UNIT-IV
UNIT-IV
SYLLABUS
Introduction to PHP: The problem with other Technologies (Servlets
and JSP), Downloading, installing, configuring PHP, Programming in a
Web environment and The anatomy of a PHP Page. Overview of PHP .
Data types and Concepts: Variables and data types, Operators,
Expressions and Statements, Strings, Arrays and Functions.
5) Open the browser and type localhost in the address bar of the
browser, then press enter.
Step4:
For testing PHP Application, type the following URL in the browser and
press enter. http://localhost/phpfilename.php where phpfilename is name
of PHP file.
Test your installation
PROGRAMMING IN WEB ENVIRONMENT:
Web development is a broad term for the work involved in
developing a web site for the Internet (World Wide Web) or an
intranet (a private network).
Web development can range from developing the simplest static
single page of plain text to the most complex web-based internet
applications, electronic businesses, and social network services.
Among web professionals, "web development" usually refers to
the main non-design aspects of building web sites: writing markup
and coding (scripting).
Web Development can be split into many areas and a typical and
basic web development hierarchy might consist of:
1. Client side coding
2. Server side coding
Client side coding:
The nature of client side coding allows you to alter the HTML on a local
client and refresh the pages with updated. If a server side script accepts
content from a locally modified client side script, the web development of
that page is poorly sanitized with relation to security.
With the help of client side programming or coding we can do the
following:
1. Make interactive WebPages.
2. Make stuff happen dynamically on the web page.
3. Interact with temporary storage and local storage (cookies, local storage)
4. Sends request to server and retrieve data from it.
Some of the client side languages are :
1. JSP
2. PHP
3. ASP
ANATOMY OF PHP:
<?php
//php statements
?>
Short Tags:
For less motivated typists, even shorter delimiter syntax is
available. Known as short-tags, this syntax forgoes the php
reference required in the default syntax, you may also
encounter code where the opening and closing syntax used is
like this:
<?
statements…
?>
Comments:
<?php
// This is a single-line comment
<?php
/*
This is a multiple-lines comment block
that spans over multiple
lines
*/
?>
VARIABLES:
• Variable is used to store information.
• In PHP developer is not forced to assign a given variable a data
type and then assign a value to it.
• Instead, PHP automatically assigns a data type to a variable when
a value is allocated to it. This makes PHP is simple to use.
• PHP variable follows certain rules like identifiers .
All variables must begin with a $ and must be immediately followed by
a letter or an underscore
Variables in PHP are indeed case sensitive and can contain any number
of letters, numbers, or underscores after the initial $ and first letter or
underscore.
A variable name should not contain spaces. If a variable name is more
than one word, it should be separated with an underscore ($my_string),
or with capitalization ($myString).
Variables are assigned by a value in the versions earlier to PHP4.
Later in PHP4 and PHP5 we can assign variables by reference.
Uninitialized variables have a default value
<html>
<body>
<?php
$x = 5;
$y = 500;
echo $x + $y;
?>
</body>
</html>
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.
Output:
•A variable declared within a function has a LOCAL
SCOPE and can only be accessed within that function:
Output:
Global keyword:
The global keyword is used to access a global variable from
within a function.
To do this, use the global keyword before the variables
(inside the function):
15
Static Keyword:
• Normally, 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:
Output:
PHP’s Supported Datatypes:
A datatype is the generic name assigned to any data sharing a
common set of characteristics.
………
OPERATORS :
An operator is a symbol that tells the compiler to perform specific
mathematical or logical manipulations.
. PHP language supports following type of operators.
Arithmetic Operators
Comparison Operators
Logical (or Relational) Operators
Assignment Operators
Conditional (or ternary) Operators
String Operators
Increment/Decrement Operators
BitWise Operators
Assignment Operators: The assignment operators assign a data value
to a variable. The simplest form of assignment operator just assigns
some value, while others (known as shortcut assignment operators)
perform some other operation before making the assignment.
String Operators:
PHP’s string operators provide a convenient way in which to
concatenate strings together.
There are two such operators,
including the concatenation operator (.) and
the concatenation assignment operator (.=).
Increment and Decrement Operators:
The increment (++) and decrement (--) operators present a
minor convenience in terms of code clarity, providing shortened
means by which you can add 1 to or subtract 1 from the current value
of a variable.
Bitwise Operators: Bitwise operators examine and manipulate
integer values on the level of individual bits that make up the integer
value.
Conditional Operator:
?:
Ternary. ($a == $b) ? 5 : 10
Examples:
i) $a = 10; // This expression assigns integer value 10 to the variable $a
ii) $a = ―123‖; // This expression assigns string value ―123‖ to the variable $a
iii) $menu = ―PUSH‖; // This expression assigns ―PUSH‖ to the variable $menu
iv) $c=$a+$b; // This expression assigns sum of the value of $a and $b to the variable $c
v) $i++; // This expression post-increment the variable $a by 1.
STATEMENTS
Conditional / Selection Statements in PHP
: Conditional / Selection statements are used to perform different
actions based on different conditions.
In PHP we have the following conditional statements:
OUTPUT
(ii) if…else statement:
If...else statement are a conditional statement which is use to
execute a statement when condition of valuable is true and
another statement will execute if valuable is false.
The if....else statement to execute some code if a condition is true
and another code if the condition is false.
Syntax: if (condition)
{ // code to be executed if condition is true; }
else { // code to be executed if condition is false; }
(iii) if...elseif....else statement:
If...elseif...else statement are a conditional statement which is use to select one
of several condition and execute the selected statement of condition.
The if....elseif...else statement to specify a new condition to test, if the first
condition is false.
Syntax:
if (condition)
{ // code to be executed if condition is true; }
else if(condition) { // code to be executed elseif condition is true; }
else { // code to be executed if condition is false; }
(iv) switch…case statement:
Switch statement are same with If...elseif...else statement, but
switch statement only use to comparing single variables with some
values is there. The switch statement is to select one of many blocks of
code to be executed.
Syntax: switch (n)
{
case label1: code to be executed if n=label1; break;
case label2: code to be executed if n=label2; break;
case label3: code to be executed if n=label3; break;
... …
default: code to be executed if n is different from all labels;
}
Looping Statements:
(i) while loop statement: The while statement will execute a block of
code if and as long as a test expression is true.
If the test expression is true then the code block will be executed.
After the code has executed the test expression will again be evaluated
and the loop will continue until the test expression is found to be false.
Syntax:
while (condition)
{
// code to be executed;
}
(ii)The do...while Statement:
It is a variant of while but it verifies the loop conditional at the
conclusion of the block rather than at the beginning.
syntax: do
{ statements }
while (expression);
Both while and do...while are similar in function.
The only real difference is that the code embedded within a while
statement possibly could never be executed, whereas the code
embedded within a do...while statement will always execute at
least once.
(iii).The for Statement:
The for statement offers a somewhat more complex looping
mechanism than does while.
syntax:
Return Value:
This function returns:0 - if the two strings are equal
<0 - if string1 is less than string2
>0 - if string1 is greater than string2
strcasecmp() Function:
<?php
$cars = array("Volvo", "BMW", "Toyota");
echo count($cars);
?>
Indexed Arrays:
There are two ways to create indexed arrays:
The index can be assigned automatically (index always starts at 0), like
this:
(i) $cars = array("Volvo", "BMW", "Toyota");
Example:
Accessing an Array Element:
Accessing of array elements is the same as in other languages.
The brackets with the subscript of the key are used to access an
individual array element.
This is irrespective of the integer key or the string key.
Ex:
$score[‘sub1’] = 55;
$day[0] = “Sunday”;
Output:
print_r() function prints the information about a variable in a
more human-readable way.
Adding and Removing Array Elements:
i) Adding a value to the Front of an Array:
The array_unshift() function inserts new elements to an array. The
new array values will be inserted in the beginning of the array.
ii) Adding a value at the End of an Array:
The array_push() function inserts one or more elements to the end of
an array.
iii) Removing a value from the Front of an Array:
The array_shift() function removes the first element from an array, and
returns the value of the removed element.
iv) Removing a value at the End of an Array:
The array_pop() function deletes the last element of an array.
Locating / Searching Array Elements:
PHP introduces several functions that enable to search arrays in order to
locate array elements
Cookies that have not been assigned a time to die will simply be
removed when the browser window closes.
Example: setcookie("cookie_user", $value);
A user will want to be able to clear the cookies on a site. When
deleting a cookie you should assure that the expiration date is in the past,
to trigger the removal mechanism in your browser.
PHP predefined Mathematical functions:
: PHP has several predefined functions, for number values. Some of the most common
functions are,
Function Description
preg_match() Returns 1 if the pattern was found in the string and 0 if
not
preg_match_all() Returns the number of times the pattern was found in
the string, which may also be 0
preg_replace() Returns a new string where matched patterns have
been replaced with another string
The preg_match() function will tell you whether a string contains matches
of a pattern.
The preg_match_all() function will tell you how many matches were
found for a pattern in a string.
The preg_replace() function will replace all of the matches of the pattern
in a string with another string.
Regular Expression Modifiers:
Modifiers can change how a search is performed.
Modifier Description
i Performs a case-insensitive search
m Performs a multiline search (patterns that search for
the beginning or end of a string will match the
beginning or end of each line)
u Enables correct matching of UTF-8 encoded patterns
Regular Expression Patterns:
Brackets are used to find a range of characters:
Expression Description
Metacharacter Description
| Find a match for any one of the patterns separated by | as
in: cat|dog|fish
. Find just one instance of any character
^ Finds a match as the beginning of a string as in: ^Hello
$ Finds a match at the end of the string as in: World$
\d Find a digit
\s Find a whitespace character
\b Find a match at the beginning of a word like this: \
bWORD, or at the end of a word like this: WORD\b
\uxxxx Find the Unicode character specified by the hexadecimal
number xxxx
Quantifiers
Quantifiers define quantities:
Quantifier Description
n+ Matches any string that contains at least one n
n* Matches any string that contains zero or more occurrences
of n
n? Matches any string that contains zero or one occurrences
of n
n{x} Matches any string that contains a sequence of X n's
n{x,y} Matches any string that contains a sequence of X to Y n's
n{x,} Matches any string that contains a sequence of at least
X n's
Example:
Using HTTP Headers:
HTTP headers are powerful sets of functionality supported
by PHP.
The most important aspect to remember about headers is
that they can be called only before any output has been
written to the web page.
We can use them to control everything, including setting the
current page location, finding out what file format is being
displayed, and managing all aspects of the browser cache.
The header() function sends a raw HTTP header to a client.
It is important to notice that the header() function must be called
before any actual output is sent!
Syntax:
header(header, replace, http_response_code)
Parameter Description
header Required. Specifies the header string to send
replace Optional. Indicates whether the header should replace a
previous similar header or add a new header of the same
type. Default is TRUE (will replace). FALSE allows
multiple headers of the same type
http_respons Optional. Forces the HTTP response code to the
e_code specified value
header
The header string.
There are two special-case header calls. The first is a header that starts
with the string "HTTP/" (case is not significant), which will be used
to figure out the HTTP status code to send.
The second special case is the "Location:" header. Not only does it send
this header back to the browser, but it also returns a REDIRECT (302)
status code to the browser unless the 201 or a 3xx status code has already
been set.
USING SESSIONS