PHP
PHP: Hypertext Preprocessor (or simply PHP) is a server-
side scripting language designed for Web development, but
also used as a general-purpose programming language. It was
originally created by Rasmus Lerdorf in 1994,the
PHP reference implementation is now produced by The PHP
Group.PHP originally stood for Personal Home Page, but it
now stands for the recursive initialism PHP: Hypertext
Preprocessor.
PHP code may be embedded into HTML code, or it can be
used in combination with various web template systems, web
content management systems, and web frameworks. PHP
code is usually processed by a PHP interpreter implemented
as a module in the web server or as a Common Gateway
Interface (CGI) executable. The web server combines the
results of the interpreted and executed PHP code, which may
be any type of data, including images, with the generated web
page. PHP code may also be executed with a command-line
interface (CLI) and can be used to
implement standalone graphical applications.
The standard PHP interpreter, powered by the Zend Engine,
is free software released under the PHP License. PHP has
been widely ported and can be deployed on most web servers
on almost every operating system and platform, free of
charge.
The PHP language evolved without a written formal
specification or standard until 2014, with the original
implementation acting as the de facto standard which other
implementations aimed to follow. Since 2014 work has gone
on to create a formal PHP specification.
During the 2010s there have been increased efforts towards
standardisation and code sharing in PHP applications by
projects such as PHP-FIG in the form of PSR initiatives as
well as the Composer dependency manager and
associated Packagist repository. PHP hosts a diverse array of
web frameworks requiring framework-specific knowledge,
with Laravel, released in June 2011, recently emerging as a
popular option by incorporating ideas made popular from
other competing non-PHP web frameworks, like Ruby on
Rails.
HISTORY
EARLY HISTORY
PHP development began in 1994 when Rasmus Lerdorf wrote
several Common Gateway Interface (CGI) programs in
C,which he used to maintain his personal homepage. He
extended them to work with web forms and to communicate
with databases, and called this implementation "Personal
Home Page/Forms Interpreter" or PHP/FI.
PHP/FI could be used to build simple, dynamic web
applications. To accelerate bug reporting and improve the
code, Lerdorf initially announced the release of PHP/FI as
"Personal Home Page Tools (PHP Tools) version 1.0" on
the Usenet discussion group
comp.infosystems.www.authoring.cgi on June 8, 1995. This
release already had the basic functionality that PHP has today.
This included Perl-like variables, form handling, and the
ability to embed HTML. The syntax resembled that of Perl,
but was simpler, more limited and less consistent.
Early PHP was not intended to be a new programming
language, and grew organically, with Lerdorf noting in
retrospect: "I don't know how to stop it, there was never any
intent to write a programming language [...] I have absolutely
no idea how to write a programming language, I just kept
adding the next logical step on the way."A development team
began to form and, after months of work and beta testing,
officially released PHP/FI 2 in November 1997.
The fact that PHP was not originally designed, but instead
was developed organically has led to inconsistent naming of
functions and inconsistent ordering of their parameters. In
some cases, the function names were chosen to match the
lower-level libraries which PHP was "wrapping", while in
some very early versions of PHP the length of the function
names was used internally as a hash function, so names were
chosen to improve the distribution of hash values.
PHP 3 and 4
Zeev Suraski and Andi Gutmans rewrote the parser in 1997
and formed the base of PHP 3, changing the language's name
to the recursive acronym PHP: Hypertext
Preprocessor.Afterwards, public testing of PHP 3 began, and
the official launch came in June 1998. Suraski and Gutmans
then started a new rewrite of PHP's core, producing the Zend
Engine in 1999. They also founded Zend
Technologies in Ramat Gan, Israel.
On May 22, 2000, PHP 4, powered by the Zend Engine 1.0,
was released. As of August 2008 this branch reached version
4.4.9. PHP 4 is no longer under development nor will any
security updates be released.
PHP 5
On July 14, 2004, PHP 5 was released, powered by the new
Zend Engine II.PHP 5 included new features such as
improved support for object-oriented programming, the PHP
Data Objects (PDO) extension (which defines a lightweight
and consistent interface for accessing databases), and
numerous performance enhancements.In 2008 PHP 5 became
the only stable version under development. Late static
binding had been missing from PHP and was added in version
5.3.
Many high-profile open-source projects ceased to support
PHP 4 in new code as of February 5, 2008, because of the
GoPHP5 initiative, provided by a consortium of PHP
developers promoting the transition from PHP 4 to PHP 5.
Over time, PHP interpreters became available on most
existing 32-bit and 64-bit operating systems, either by
building them from the PHP source code, or by using pre-built
binaries.For the PHP versions 5.3 and 5.4, the only
available Microsoft Windows binary distributions were 32-
bit x86 builds, requiring Windows 32-bit compatibility mode
while using Internet Information Services (IIS) on a 64-bit
Windows platform. PHP version 5.5 made the 64-bit x86-
64 builds available for Microsoft Windows.
PHP 6 and Unicode
PHP received mixed reviews due to lacking
native Unicode support at the core language level.In 2005, a
project headed by Andrei Zmievski was initiated to bring
native Unicode support throughout PHP, by embedding
the International Components for Unicode (ICU) library, and
representing text strings as UTF-16 internally.Since this
would cause major changes both to the internals of the
language and to user code, it was planned to release this as
version 6.0 of the language, along with other major features
then in development.
However, a shortage of developers who understood the
necessary changes, and performance problems arising from
conversion to and from UTF-16, which is rarely used in a web
context, led to delays in the project.As a result, a PHP 5.3
release was created in 2009, with many non-Unicode features
back-ported from PHP 6, notably namespaces. In March 2010,
the project in its current form was officially abandoned, and a
PHP 5.4 release was prepared containing most remaining non-
Unicode features from PHP 6, such as traits and closure re-
binding. Initial hopes were that a new plan would be formed
for Unicode integration, but as of 2014 none had been
adopted.
PHP 7
During 2014 and 2015, a new major PHP version was
developed, which was numbered PHP 7. The numbering of
this version involved some debate.
While the PHP 6 Unicode experiment had never been
released, several articles and book titles referenced the PHP 6
name, which might have caused confusion if a new release
were to reuse the name.After a vote, the name PHP 7 was
chosen.
Because of major internal changes in phpng, it must receive a
new major version number of PHP, rather than a minor PHP 5
release, according to PHP's release process. Major versions of
PHP are allowed to break backward-compatibility of code and
therefore PHP 7 presented an opportunity for other
improvements beyond phpng that require backward-
compatibility breaks. In particular, it involved the following
changes:
Many fatal- or recoverable-level legacy PHP error
mechanisms were replaced with modern object-
oriented exceptions
The syntax for variable dereferencing was reworked to be
internally more consistent and complete, allowing the use
of the operators ->, [], (), {}, and :: with arbitrary
meaningful left-hand-side expressions
Support for legacy PHP 4-style constructor methods was
deprecated
The behavior of the foreach statement was changed to be
more predictable
Constructors for the few classes built-in to PHP which
returned null upon failure were changed to throw an
exception instead, for consistency
Several unmaintained or deprecated server application
programming interfaces (SAPIs) and extensions were
removed from the PHP core, most notably the
legacy mysqlextension
The behavior of the list() operator was changed to remove
support for strings
Support for legacy ASP-style PHP code delimiters
(<% and %>, <script language=php> and </script>) was
removed
An oversight allowing a switch statement to have
multiple default clauses was fixed
Support for hexadecimal number support in some implicit
conversions from strings to number types was removed
The left-shift and right-shift operators were changed to
behave more consistently across platforms
Conversions between integers and floating point numbers
were tightened and implemented more consistently across
platforms
PHP 7 also included new language features. Most notably, it
introduces return type declarations for functions which
complement the existing parameter type declarations, and
support for the scalar types (integer, float, string, and boolean)
in parameter and return type declarations.
RELEASE HISTORY
Color Meaning Development
Red Old release No development
yellow Stable release Security fixes
Green Stable release Bug and security fixes
Blue Future release New features
MASCOT
The mascot of the PHP project is the elePHPant, a
blue elephant with the PHP logo on its side, designed by
Vincent Pontier in 1998."The (PHP) letters were forming the
shape of an elephant if viewed in a sideways angle."The
elePHPant is sometimes differently colored when in plush
toy form.
Many variations of this mascot have been made over the
years. Only the elePHPants based on the original design by
Vincent Pontier are considered official by the community.
These are highly collectible and some of them are extremely
rare. Different variations are listed on A Field Guide to
Elephpants.
SYNTAX
The following "Hello, World!" program is written in PHP
code embedded in an HTML document:
<!DOCTYPE html>
<html>
<head>
<title>PHP Test</title>
</head>
<body>
<?php echo '<p>Hello World</p>'; ?>
</body>
</html>
However, as no requirement exists for PHP code to be
embedded in HTML, the simplest version of Hello,
World! may be written like this, with the closing tag omitted
as preferred in files containing pure PHP code.
<?='Hello world';
The PHP interpreter only executes PHP code within
its delimiters. Anything outside its delimiters is not processed
by PHP, although non-PHP text is still subject to control
structuresdescribed in PHP code. The most common
delimiters are <?php to open and ?> to close PHP sections.
The shortened form <? also exists. This short delimiter makes
script files less portable, since support for them can be
disabled in the local PHP configuration and it is therefore
discouraged. However, there is no recommendation against
the use of the echo short tag <?=. Prior to PHP 5.4.0, this
short syntax for echo() only works with
the short_open_tag configuration setting enabled, while for
PHP 5.4.0 and later it is always available. The purpose of all
these delimiters is to separate PHP code from non-PHP
content, such as JavaScript code or HTML markup.
The first form of delimiters, <?php and ?>, in XHTML and
other XML documents, creates correctly formed XML
processing instructions.This means that the resulting mixture
of PHP code and other markup in the server-side file is itself
well-formed XML.
Variables are prefixed with a dollar symbol, and a type does
not need to be specified in advance. PHP 5 introduced type
hinting that allows functions to force their parameters to be
objects of a specific class, arrays, interfaces or callback
functions. However, before PHP 7.0, type hints could not be
used with scalar types such as integer or string.
Unlike function and class names, variable names are case
sensitive. Both double-quoted ("") and heredoc strings provide
the ability to interpolate a variable's value into the string. PHP
treats newlines as whitespace in the manner of a free-form
language, and statements are terminated by a semicolon.PHP
has three types of comment syntax:
/* */marks block and inline comments;// are used for one-line
comments.The echo statement is one of several facilities PHP
provides to output text, e.g., to a web browser.
In terms of keywords and language syntax, PHP is similar to
the C style syntax. if conditions, for and while loops, and
function returns are similar in syntax to languages such as C,
C++, C#, Java and Perl.
FUNCTIONS
PHP defines a large array of functions in the core language and many
are also available in various extensions; these functions are well
documented in the online PHP documentation.However, the built-in
library has a wide variety of naming conventions and associated
inconsistencies, as described under history above.
Custom functions may be defined by the developer, e.g.:
// define a function, this one is named "myAge"
function myAge(Int $birthYear) : String {
// calculate the age
$yearsOld = date('Y') - $birthYear;
// return the age in a descriptive form
return $yearsOld . ' year' . ($yearsOld != 1 ? 's' : '');
}
// Next, output the text concatenated
// with the return value of myAge()
// As the result of the syntax "myAge()", the myAge function
is called.
echo 'I am currently ' . myAge(1995) . ' old.';
In 2018, the output of the above sample program is ‘I am
currently 23 years old’.
PHP Objects
Basic object-oriented programming functionality was added in
PHP 3 and improved in PHP 4.This allowed for PHP to gain
further abstraction, making creative tasks easier for
programmers using the language. Object handling was
completely rewritten for PHP 5, expanding the feature set and
enhancing performance.
In previous versions of PHP, objects were handled like value
types. The drawback of this method was that code had to
make heavy use of PHP's "reference" variables if it wanted to
modify an object it was passed rather than creating a copy of
it. In the new approach, objects are referenced by handle, and
not by value.
PHP 5 introduced private and protected member variables and
methods, along with abstract classes, final classes, abstract
methods, and final methods. It also introduced a standard way
of declaring constructors and destructors, similar to that of
other object-oriented languages such as C++, and a
standard exception handling model. Furthermore, PHP 5
added interfaces and allowed for multiple interfaces to be
implemented. There are special interfaces that allow objects to
interact with the runtime system. Objects implementing
ArrayAccess can be used with array syntax and objects
implementing Iterator or IteratorAggregate can be used with
the foreach language construct. There is no virtual
tablefeature in the engine, so static variables are bound with a
name instead of a reference at compile time.
If the developer creates a copy of an object using the reserved
word clone, the Zend engine will check whether a __clone()
method has been defined. If not, it will call a default __clone()
which will copy the object's properties. If a __clone() method
is defined, then it will be responsible for setting the necessary
properties in the created object. For convenience, the engine
will supply a function that imports the properties of the source
object, so the programmer can start with a by-value replica of
the source object and only override properties that need to be
changed.
The visibility of PHP properties and methods is defined using
the keywords public, private, and protected. The default is
public, if only var is used; var is a synonym for public. Items
declared public can be accessed everywhere. protected limits
access to inherited classes (and to the class that defines the
item). private limits visibility only to the class that defines the
item. Objects of the same type have access to each other's
private and protected members even though they are not the
same instance.
SECURITY
In 2017, 3% of all vulnerabilities listed by the National
Vulnerability Database were linked to PHP; historically, about
30% of all vulnerabilities listed since 1996 in this database are
linked to PHP. Technical security flaws of the language itself
or of its core libraries are not frequent (22 in 2009, about 1%
of the total although PHP applies to about 20% of programs
listed). Recognizing that programmers make mistakes, some
languages include taint checking to automatically detect the
lack of input validation which induces many issues. Such a
feature is being developed for PHP,but its inclusion into a
release has been rejected several times in the past.
There are advanced protection patches such
as Suhosin and Hardening-Patch, especially designed for web
hosting environments.
Advantages of PHP
Open source: It is developed and maintained by a large
group of PHP developers, this will helps in creating a
support community, abundant extension library.
Speed: It is relative fast since it uses much system
resource.
Easy to use: It uses C like syntax, so for those who are
familiar with C, it's very easy for them to pick up and it
is very easy to create website scripts.
Stable: Since it is maintained by many developers, so
when bugs are found, it can be quickly fixed.
Powerful library support: You can easily find functional
modules you need such as PDF, Graph etc.
Built-in database connection modules: You can connect
to database easily using PHP, since many websites are
data/content driven, so we will use database frequently,
this will largely reduce the development time of web
apps.
Can be run on many platforms, including Windows,
Linux and Mac, it's easy for users to find hosting service
providers.
Disadvantages of PHP
Security : Since it is open sourced, so all people can see
the source code, if there are bugs in the source code, it
can be used by people to explore the weakness of PHP.
Not suitable for large applications: Hard to maintain
since it is not very modular.
Weak type: Implicit conversion may surprise unwary
programmers and lead to unexpected bugs.
For example, the strings "1000" and "1e3" compare equal
because they are implicitly cast to floating point numbers
PHP Simple Syntax
Basically, a PHP file is a text file with the extension .php
which consists of:
Text
HTML tags
PHP Scripts
To work with PHP you need to know HTML but we are going
to generate dynamic web pages with PHP. PHP is not
difficult! On the contrary, PHP is often very similar to plain
English.
How Does PHP Work
The most important thing to understand is the difference
between HTML and PHP. In HTML you write your code,
deployed it, and the user's will subsequently download that
page along with all the code. The browser interprets this code
and shows the user the page as you intended it. In other words
HTML code goes to the user as it is and is interpreted by the
browser.
But with PHP it works quite differently because you don't
actually download the code. What happens when user request
an PHP page, the code in the requested file is first processed
by the server, and you download the output of the code (in
HTML form), as opposed to the whole code as is.
With PHP the goal is to use the processing powers of the
server to build (usually) dynamic web pages.
This means that if you click "view source" on a PHP page,
you do not see the PHP codes - only basic HTML tags.
Therefore, you cannot see how a PHP page is made by using
"view source"
Features of PHP
The main features of php is; it is open source scripting
language so you can free download this and use. PHP is a
server site scripting language. It is open source scripting
language. It is widely used all over the world. It is faster than
other scripting language. Some important features of php are
given below;
It is most popular and frequently used world wide scripting
language, the main reason of popularity is; It is open source
and very simple.
Simple
Faster
Interpreted
Open Source
Case Sensitive
Simplicity
Efficiency
Platform Independent
Security
Flexibility
Familiarity
Error Reporting
Loosely Typed Language
Real-Time Access Monitoring
SIMPLE
It is very simple and easy to use, compare to other scripting
language it is very simple and easy, this is widely used all
over the world.
Interpreted
It is an interpreted language, i.e. there is no need for
compilation.
Faster
It is faster than other scripting language e.g. asp and jsp.
Open Source
Open source means you no need to pay for use php, you can
free download and use.
Platform Independent
PHP code will be run on every platform, Linux, Unix, Mac
OS X, Windows.
Case Sensitive
PHP is case sensitive scripting language at time of variable
declaration. In PHP, all keywords (e.g. if, else, while, echo,
etc.), classes, functions, and user-defined functions are NOT
case-sensitive.
Error Reporting
PHP have some predefined error reporting constants to
generate a warning or error notice.
Real-Time Access Monitoring
PHP provides access logging by creating the summary of
recent accesses for the user.
Loosely Typed Language
PHP supports variable usage without declaring its data type.
It will be taken at the time of the execution based on the type
of data it has on its value.
Uses of php
It is used for create dynamic website.
To Interacting with web server (Apache etc)
To interacting with any back-end / database server e.g.
MySQL
To interaction with the native file system of the OS
To implement the business logical layers (one or more)
It can Encrypt Data
Access Cookies variable and set cookies
Using php you can restrict user to access specific web
page
PHP usually used to output HTML code to the browser
Used for connect web application with DataBase
It is used for send and receive E-Mails.
You can use PHP to find today's date, and then build a
calendar for the month.
If you host banner advertisements on your website, you
can use PHP to rotate them randomly.
Using php you can count your visitors on your website.
You can use PHP to create a special area of your website
for members.
Using php you can create login page for your user.
Using php you can add, delete, modify elements within
your database thru PHP. Access cookies variables and set
cookies.
Using PHP, you can restrict users to access some pages of
your website.
It can encrypt data.
PHP performs system functions, i.e. from files on a
system it can create, open, read, write, and close them.
It can handle forms, i.e. gather data from files, save data
to a file.
Why use php
It runs on different platforms such as Windows, Linux,
Unix, etc.
This language is very simple to learn and runs efficiently
on the server side.
It is compatible with almost all servers used today,such as
Apache, IIS, etc.
It supports many databases such as MySQL, Oracle,
PostgreSQL etc.
It is perfectly suited for Web development and can be
embedded directly into the HTML code.
PHP can also be used to create dynamic web pages.
It is often used together with Apache (web server) on
various operating systems. It can be also used with
Microsoft's IIS on Windows.
It is open source and it is free downloadable.
How to Install PHP Server
WAMP for Windows
LAMP for Linux
MAMP for Mac
SAMP for Solaris
FAMP for FreeBSD
XAMPP (Cross, Apache, MySQL, PHP, Perl) for Cross
Platform: It includes some other components too such as
FileZilla, OpenSSL, Webalizer, OpenSSL, Mercury Mail
etc.
If you are on Windows and don't want Perl and other features
of XAMPP, you should go for WAMP. In a similar way, you
may use LAMP for Linux and MAMP for Macintosh.
Variable in PHP
Variable is an identifier which holds data or another one
variable and whose value can be changed at the execution
time of script. In PHP, a variable starts with the $ sign,
followed by the name of the variable.
Example of variable in php
<?php
$str="Hello world!";
$a=5;
$b=10.5;
echo "String is: $str <br/>";
echo "Integer is: $x <br/>";
echo "Float is: $y <br/>";
?>
OUTPUT
String is: Hello world!
Integer is: 5
Float is: 10.5
Rules to declare variable in PHP
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 can't start with a number.
A variable name can only contain alpha-numeric
characters and underscores (A-z, 0-9, and _ )
Variable names are case-sensitive ($str and $STR both are
two different).
Constant in PHP
Constants are name or identifier that can't be changed during
the execution of the script. In php constants are define in two
ways;
Using define() function
Using const keyword
In php decalare constants follow same rule variable
declaration. Constant start with letter or underscore only.
Define constant using cons keyword in php
The const keyword defines constants at compile time. It is a
language construct not a function. It is bit faster than define().
It is always case sensitive.