0% found this document useful (0 votes)
45 views17 pages

UNIT-4 Financial Services

PHP is a programming language used for web development that allows developers to create dynamic web content. Some key advantages of PHP include that it is integrated with popular databases like MySQL, supports common web protocols, and has a forgiving syntax. PHP code is embedded into HTML and processed on the server-side. Variables, arrays, and functions are the basic building blocks of PHP programs. Cookies are text files stored on the client computer that PHP can use for session tracking and personalization.

Uploaded by

Dev Joshi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views17 pages

UNIT-4 Financial Services

PHP is a programming language used for web development that allows developers to create dynamic web content. Some key advantages of PHP include that it is integrated with popular databases like MySQL, supports common web protocols, and has a forgiving syntax. PHP code is embedded into HTML and processed on the server-side. Variables, arrays, and functions are the basic building blocks of PHP programs. Cookies are text files stored on the client computer that PHP can use for session tracking and personalization.

Uploaded by

Dev Joshi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 17

UNIT-4

PHP
The PHP Hypertext Preprocessor (PHP) is a programming language that allows web
developers to create dynamic content that interacts with databases. PHP is basically used for
developing web based software applications. This tutorial helps you to build your base with
PHP.

I will list down some of the key advantages of learning PHP:

 PHP is a recursive acronym for "PHP: Hypertext Preprocessor".

 PHP is a server side scripting language that is embedded in HTML. It is used to


manage dynamic content, databases, session tracking, even build entire e-commerce
sites.

 It is integrated with a number of popular databases, including MySQL, PostgreSQL,


Oracle, Sybase, Informix, and Microsoft SQL Server.

 PHP is pleasingly zippy in its execution, especially when compiled as an Apache


module on the Unix side. The MySQL server, once started, executes even very
complex queries with huge result sets in record-setting time.

 PHP supports a large number of major protocols such as POP3, IMAP, and LDAP.
PHP4 added support for Java and distributed object architectures (COM and
CORBA), making n-tier development a possibility for the first time.

 PHP is forgiving: PHP language tries to be as forgiving as possible.

 PHP Syntax is C-Like.

Characteristics of PHP:

Five important characteristics make PHP's practical nature possible −

 Simplicity
 Efficiency
 Security
 Flexibility
 Familiarity

Hello World using PHP:

Just to give you a little excitement about PHP, I'm going to give you a small conventional
PHP Hello World program, You can try it using Demo link.
<html>

<head>
<title>Hello World</title>
</head>

<body>
<?php echo "Hello, World!";?>
</body>

</html>

Applications of PHP:

As mentioned before, PHP is one of the most widely used language over the web. I'm going
to list few of them here:

 PHP performs system functions, i.e. from files on a system it can create, open, read,
write, and close them.

 PHP can handle forms, i.e. gather data from files, save data to a file, through email
you can send data, return data to the user.

 You add, delete, modify elements within your database through PHP.

 Access cookies variables and set cookies.


 Using PHP, you can restrict users to access some pages of your website.

 It can encrypt data.

Commenting PHP Code:

A comment is the portion of a program that exists only for the human reader and stripped out
before displaying the programs result. There are two commenting formats in PHP −

Single-line comments − They are generally used for short explanations or notes relevant to
the local code. Here are the examples of single line comments.
<?
# This is a comment, and
# This is the second line of the comment

// This is a comment too. Each style comments only


print "An example with single line comments";
?>

Multi-lines printing − Here are the examples to print multiple lines in a single print
statement –
<?
# First Example
print <<<END
This uses the "here document" syntax to output
multiple lines with $variable interpolation. Note
that the here document terminator must appear on a
line with just a semicolon no extra whitespace!
END;

# Second Example
print "This spans
multiple lines. The newlines will be
output as well";
?>
HP is whitespace insensitive:

Whitespace is the stuff you type that is typically invisible on the screen, including spaces,
tabs, and carriage returns (end-of-line characters).

PHP whitespace insensitive means that it almost never matters how many whitespace
characters you have in a row.one whitespace character is the same as many such characters.

For example, each of the following PHP statements that assigns the sum of 2 + 2 to the
variable $four is equivalent −
$four = 2 + 2; // single spaces
$four <tab>=<tab2<tab>+<tab>2 ; // spaces and tabs
$four =
2+
2; // multiple lines

PHP is case sensitive:

Yeah it is true that PHP is a case sensitive language. Try out following example −
<html>
<body>

<?php
$capital = 67;
print("Variable capital is $capital<br>");
print("Variable CaPiTaL is $CaPiTaL<br>");
?>

</body>
</html>

This will produce the following result −


Variable capital is 67
Variable CaPiTaL is

Statements are expressions terminated by semicolons:

A statement in PHP is any expression that is followed by a semicolon (;).Any sequence of


valid PHP statements that is enclosed by the PHP tags is a valid PHP program. Here is a
typical statement in PHP, which in this case assigns a string of characters to a variable called
$greeting −
$greeting = "Welcome to PHP!";

Expressions are combinations of tokens:

The smallest building blocks of PHP are the indivisible tokens, such as numbers (3.14159),
strings (.two.), variables ($two), constants (TRUE), and the special words that make up the
syntax of PHP itself like if, else, while, for and so forth

Braces make blocks:

Although statements cannot be combined like expressions, you can always put a sequence of
statements anywhere a statement can go by enclosing them in a set of curly braces.

Here both statements are equivalent −


if (3 == 2 + 1)
print("Good - I haven't totally lost my mind.<br>");

if (3 == 2 + 1) {
print("Good - I haven't totally");
print("lost my mind.<br>");
}

Running PHP Script from Command Prompt:

Yes you can run your PHP script on your command prompt. Assuming you have following
content in test.php file

<?php
echo "Hello PHP!!!!!";
?>

It will produce the following result −


Hello PHP!!!!!

Variable:

The main way to store information in the middle of a PHP program is by using a variable.

Here are the most important things to know about variables in PHP.

 All variables in PHP are denoted with a leading dollar sign ($).

 The value of a variable is the value of its most recent assignment.

 Variables are assigned with the = operator, with the variable on the left-hand side and
the expression to be evaluated on the right.

 Variables can, but do not need, to be declared before assignment.

 Variables in PHP do not have intrinsic types - a variable does not know in advance
whether it will be used to store a number or a string of characters.
 Variables used before they are assigned have default values.

 PHP does a good job of automatically converting types from one to another when
necessary.

 PHP variables are Perl-like.

PHP has a total of eight data types which we use to construct our variables −

 Integers − are whole numbers, without a decimal point, like 4195.

 Doubles − are floating-point numbers, like 3.14159 or 49.1.

 Booleans − have only two possible values either true or false.

 NULL − is a special type that only has one value: NULL.

 Strings − are sequences of characters, like 'PHP supports string operations.'

 Arrays − are named and indexed collections of other values.

 Objects − are instances of programmer-defined classes, which can package up both


other kinds of values and functions that are specific to the class.

 Resources − are special variables that hold references to resources external to PHP
(such as database connections).

Array:

An array is a data structure that stores one or more similar type of values in a single value.
For example if you want to store 100 numbers then instead of defining 100 variables its easy
to define an array of 100 length.

There are three different kind of arrays and each array value is accessed using an ID c which
is called array index.

 Numeric array − An array with a numeric index. Values are stored and accessed in
linear fashion.
 Associative array − An array with strings as index. This stores element values in
association with key values rather than in a strict linear index order.

 Multidimensional array − An array containing one or more arrays and values are
accessed using multiple indices

Function:

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.

You already have seen many functions like fopen() and fread() etc. They are built-in
functions but PHP gives you option to create your own functions as well.

There are two parts which should be clear to you −

 Creating a PHP Function


 Calling a PHP Function

In fact you hardly need to create your own PHP function because there are already more than
1000 of built-in library functions created for different area and you just need to call them
according to your requirement.

Creating PHP Function:

Its very easy to create your own PHP function. Suppose you want to create a PHP function
which will simply write a simple message on your browser when you will call it. Following
example creates a function called writeMessage() and then calls it just after creating it.

Note that while creating a function its name should start with keyword function and all the
PHP code should be put inside { and } braces as shown in the following example below –

<html>

<head>
<title>Writing PHP Function</title>
</head>

<body>

<?php
/* Defining a PHP Function */
function writeMessage() {
echo "You are really a nice person, Have a nice time!";
}
/* Calling a PHP Function */
writeMessage();
?>
</body>
</html>

This will display following result −


You are really a nice person, Have a nice time!

Cookies:

Cookies are text files stored on the client computer and they are kept of use tracking purpose.
PHP transparently supports HTTP cookies.

There are three steps involved in identifying returning users −

 Server script sends a set of cookies to the browser. For example name, age, or
identification number etc.

 Browser stores this information on local machine for future use.

 When next time browser sends any request to web server then it sends those cookies
information to the server and server uses that information to identify the user.

This chapter will teach you how to set cookies, how to access them and how to delete them.

The Anatomy of a Cookie:


Cookies are usually set in an HTTP header (although JavaScript can also set a cookie directly
on a browser). A PHP script that sets a cookie might send headers that look something like
this −
HTTP/1.1 200 OK
Date: Fri, 04 Feb 2000 21:03:38 GMT
Server: Apache/1.3.9 (UNIX) PHP/4.0b3
Set-Cookie: name=xyz; expires=Friday, 04-Feb-07 22:03:38 GMT;
path=/; domain=tutorialspoint.com
Connection: close
Content-Type: text/html

As you can see, the Set-Cookie header contains a name value pair, a GMT date, a path and a
domain. The name and value will be URL encoded. The expires field is an instruction to the
browser to "forget" the cookie after the given time and date.

If the browser is configured to store cookies, it will then keep this information until the
expiry date. If the user points the browser at any page that matches the path and domain of
the cookie, it will resend the cookie to the server.The browser's headers might look
something like this −
GET / HTTP/1.0
Connection: Keep-Alive
User-Agent: Mozilla/4.6 (X11; I; Linux 2.2.6-15apmac ppc)
Host: zink.demon.co.uk:1126
Accept: image/gif, */*
Accept-Encoding: gzip
Accept-Language: en
Accept-Charset: iso-8859-1,*,utf-8
Cookie: name=xyz

A PHP script will then have access to the cookie in the environmental variables $_COOKIE
or $HTTP_COOKIE_VARS[] which holds all cookie names and values. Above cookie can
be accessed using $HTTP_COOKIE_VARS["name"].

Setting Cookies with PHP:


PHP provided setcookie() function to set a cookie. This function requires upto six arguments
and should be called before <html> tag. For each cookie this function has to be called
separately.
setcookie(name, value, expire, path, domain, security);

Here is the detail of all the arguments −

 Name − This sets the name of the cookie and is stored in an environment variable
called HTTP_COOKIE_VARS. This variable is used while accessing cookies.

 Value − This sets the value of the named variable and is the content that you actually
want to store.

 Expiry − This specify a future time in seconds since 00:00:00 GMT on 1st Jan 1970.
After this time cookie will become inaccessible. If this parameter is not set then
cookie will automatically expire when the Web Browser is closed.

 Path − This specifies the directories for which the cookie is valid. A single forward
slash character permits the cookie to be valid for all directories.

 Domain − This can be used to specify the domain name in very large domains and
must contain at least two periods to be valid. All cookies are only valid for the host
and domain which created them.

 Security − This can be set to 1 to specify that the cookie should only be sent by
secure transmission using HTTPS otherwise set to 0 which mean cookie can be sent
by regular HTTP.

Following example will create two cookies name and age these cookies will be expired after
one hour.
<?php
setcookie("name", "John Watkin", time()+3600, "/","", 0);
setcookie("age", "36", time()+3600, "/", "", 0);
?>
<html>
<head>
<title>Setting Cookies with PHP</title>
</head>

<body>
<?php echo "Set Cookies"?>
</body>

</html>

Accessing Cookies with PHP:

PHP provides many ways to access cookies. Simplest way is to use either $_COOKIE or
$HTTP_COOKIE_VARS variables. Following example will access all the cookies set in
above example.
<html>

<head>
<title>Accessing Cookies with PHP</title>
</head>

<body>

<?php
echo $_COOKIE["name"]. "<br />";

/* is equivalent to */
echo $HTTP_COOKIE_VARS["name"]. "<br />";

echo $_COOKIE["age"] . "<br />";

/* is equivalent to */
echo $HTTP_COOKIE_VARS["age"] . "<br />";
?>

</body>
</html>

You can use isset() function to check if a cookie is set or not.


<html>

<head>
<title>Accessing Cookies with PHP</title>
</head>

<body>

<?php
if( isset($_COOKIE["name"]))
echo "Welcome " . $_COOKIE["name"] . "<br />";

else
echo "Sorry... Not recognized" . "<br />";
?>

</body>
</html>

Deleting Cookie with PHP:

Officially, to delete a cookie you should call setcookie() with the name argument only but
this does not always work well, however, and should not be relied on.

It is safest to set the cookie with a date that has already expired −
<?php
setcookie( "name", "", time()- 60, "/","", 0);
setcookie( "age", "", time()- 60, "/","", 0);
?>
<html>

<head>
<title>Deleting Cookies with PHP</title>
</head>

<body>
<?php echo "Deleted Cookies" ?>
</body>

</html>

Ajax :

Ajax is used to communicate with web pages and web servers. Below example demonstrate a
search field using with Ajax.
<html>
<head>

<style>
span {
color: green;
}
</style>

<script>
function showHint(str) {
if (str.length == 0) {
document.getElementById("txtHint").innerHTML = "";
return;
}else {
var xmlhttp = new XMLHttpRequest();

xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById("txtHint").innerHTML = xmlhttp.responseText;
}
}
xmlhttp.open("GET", "php_ajax.php?q=" + str, true);
xmlhttp.send();
}
}
</script>

</head>
<body>

<p><b>Search your favourite tutorials:</b></p>

<form>
<input type = "text" onkeyup = "showHint(this.value)">
</form>

<p>Entered Course name: <span id="txtHint"></span></p>

</body>
</html>

Above code opens a file, name called as php_ajax.php by using with GET method, so we
need to create a file, name called as php_ajax.php in the same directory and out put will be
attached with txtHint.

php_ajax.php:

It contained array of course names and it returns the value to web browser.
<?php
// Array with names
$a[] = "Android";
$a[] = "B programming language";
$a[] = "C programming language";
$a[] = "D programming language";
$a[] = "euphoria";
$a[] = "F#";
$a[] = "GWT";
$a[] = "HTML5";
$a[] = "ibatis";
$a[] = "Java";
$a[] = "K programming language";
$a[] = "Lisp";
$a[] = "Microsoft technologies";
$a[] = "Networking";
$a[] = "Open Source";
$a[] = "Prototype";
$a[] = "QC";
$a[] = "Restful web services";
$a[] = "Scrum";
$a[] = "Testing";
$a[] = "UML";
$a[] = "VB Script";
$a[] = "Web Technologies";
$a[] = "Xerox Technology";
$a[] = "YQL";
$a[] = "ZOPL";

$q = $_REQUEST["q"];
$hint = "";

if ($q !== "") {


$q = strtolower($q);
$len = strlen($q);

foreach($a as $name) {

if (stristr($q, substr($name, 0, $len))) {


if ($hint === "") {
$hint = $name;
}else {
$hint .= ", $name";
}
}
}
}
echo $hint === "" ? "Please enter a valid course name" : $hint;
?>

You might also like