Introduction To PHP
Introduction To PHP
PHP is a server-side and general-purpose scripting language that is especially suited for web
development.
PHP originally stood for Personal Home Page. However, now, it stands for Hypertext Preprocessor. It’s a
recursive acronym because the first word itself is also an acronym.
On the other hand, PHP is a general-purpose language because PHP can develop various applications.
Server-side scripting – PHP is well-suited for developing dynamic websites and web applications.
Command-line scripting – like Python and Perl, you can run PHP script from the command line to
perform administrative tasks like sending emails and generating PDF files.
First, the web browser sends an HTTP request to the web server, e.g., index.php.
Second, the PHP preprocessor that locates on the web server processes PHP code to generate the HTML
document.
Third, the web server sends the HTML document back to the web browser.
Advantages of PHP
Since PHP is designed for the web in the first place, it brings many advantages to web development:
Open-source and free – PHP is open source and free. It means that you don’t have to pay a license fee to
use PHP to develop software products.
Community support – PHP has an active online community that helps you whenever you face an issue.
Web Application Development involves communication between the client browser and the
server. The web applications are developed by using different scripting languages in addition to
HTML and CSS. The most commonly used scripting languages/ technologies are JavaScript,
VBScript, ASP, Perl, Python, PHP, Ruby on Rails to mention a few. These scripting technologies
can be categorized as Client Side and Server Side Scripting Technologies depending upon where
these scripts are executed.
Client-side Scripting Technology
In this category the browser of the user requesting a web page executes the scripts written or embedded
in the requested web document. When a user requests a web page from the server, the server sends the
source code of the requested page to the client machine over the internet. Browser processes the
source code and presents the page to the user. A user is able to see the code of a client-side scripted
page by opening the source (in browser window open view menu and select “View Source” or right
click in the document window in browser and select “View Source” or “View Page Source”).
HTML (Hypertext Markup Language) and CSS (Cascading Style Sheets). CSS is used for page formatting,
controlling text size, color, placement and layout. It can also be used to control the timing of appearance
of various elements on a webpage.
VBScript and JavaScript are used for event handling and controlling results of the defined event.
In this category when a user requests a webpage the script contained in it is executed by the web server
and only the results are returned to the client. The user request is responded by the server that runs the
script and generates HTML page. This dynamically generated HTML document is sent to the client
machine where the browser that displays it to the user. A user is not able to see the code of a server side
scripted page by opening the source.
Server-side scripting is a better alternative in case the web application needs to be customised
depending on the options chosen by the user or depending upon her requirements. This is the most
common mechanism to create interactive web sites like database applications, online stores, feedback or
order forms etc.
PHP
ASP
NET
Ruby on Rails
Perl
Python
Static Website Development and Dynamic Website Development
The difference between a static website and a dynamic website lies in the interactivity offered to the
user. A website is said to be static if its content does not change when a user interacts with it. It is
displayed on the user’s browser as it is fetched from the server. It presents same content to all its
users. On the other hand a dynamic website responds to the interaction of a user by presenting the
content requested by a user. The content of a dynamic website changes with the context of input
provided by a user. Dynamic WebPages include code scripts in addition to HTML to allow the user to fill
in or select the options and submit it. The content management systems like WordPress, Joomla are the
perfect examples of dynamic websites. Static Website Development is different in may way from
Dynamic Website Development.
To create a static website or page most commonly a collection of HTML files that may or may not be
linked using hyperlinks. Tools like MS FrontPage and Dreamweaver are used to create and manage static
website projects. Nowadays static site generators are used that offer a variety of templates to chose
from so that even a novice can create impressive WebPages by simply placing the content in different
sections of the template.
Just like a dynamic website a static website’s pages are as HTML files are organized in the file system.
When a page is requested by the user the web server transfers the file over HTTP. The bottom-line is that
the static website is of unchanging nature.
Advantages
Since no interaction is possible and it lacks in scripts so the static websites are better in security.
Static websites are presented in the form they are available in the web server so they take lesser time to
download and appear on a requesting browser. This results in better performance in terms of speed and
resources.
Appearance of static websites can be enhanced by using CSS but since these sites do not depend on
databases or scripts, they can be economical. The development can be done with simple HTML without
the need of other backend tools or application servers.
Disadvantages
Static websites lack functionality if the user wants to interact with them.
Dynamic website development requires the HTML code supported by client-side scripting or server-side
scripting to provide the interaction missing in the static website. A dynamic webpage can be created by
using both type of scripting. Dynamic website development is done with the help of tools, platforms and
programming languages like PHP, JSP, and ASP.NET. Many frameworks like Ruby on Rails, Django, or
Flex are also used by the web app developers.
Unlike static website development, dynamic website development is a skill that needs skills of a
programmer and experience of working with a database management system. A novice cannot create
dynamic website by using some templates offered by HTML based tools. He has to be comfortable with
programming in PHP, JavaScript or any other scripting language used in web application development.
Advantages
Dynamic websites are flexible, interactive and user specific. They present content as per the OS, user,
devices or input given by a user.
They offer more functionality to a specific website. Web applications made the business easier and
global for different companies across the globe.
Disadvantages
Dynamic website development needs coding and database implementation. It can introduce higher
financial implications for the owner since it requires the efforts at the end of the developers.
The web server needs for a dynamic website are more specific. So the issues of space and bandwidth for
server are important factors. Security is another important factor.
The complex functionalities of web applications can affect the performance of a dynamic website in
negative manner.
To make the HTML pages interactive, developers have a variety of scripting languages to choose from.
PHP has been a preferred scripting language due to its ease of learning, ability to connect with open
source databases like MySQL and MonoDB. PHP code library is an immense resource for developers to
create dynamic web applications with least efforts and high security. As a developer when you graduate
from creating simple HTML web pages to complex function oriented web applications, you will need
embedding PHP into HTML. This is not as complicated as it may look right now. PHP is designed to
seamlessly integrate within HTML by embedding PHP scripts of any size.
When a dynamic page has to be developed using HTML code, it has to be saved with .php extension. A
PHP file could be a mix of HTML, CSS, JavaScript and PHP sections or an all PHP script file that can be
called upon an action taken by a user on a pure HTML file.
When you are embedding PHP script in an HTML code, you need to save the file in the file system of your
web server like Apache. In the current case you need to save your php or HTML file in WAMP/bin folder.
When a user requests the server for this page by typing the URL, the server responds back with output
generated as HTML code after processing the PHP code. PHP code is never sent to the users’ browser.
You can embed the PHP script anywhere in an HTML code and any number of times. Server processes
only those scripts lines that are enclosed in <?php ?> tags. Rest of the code is ignored by the PHP
interpreter at the web server and returned as it is to the user’s web browser.
XML Style
This is also the preferred style of embedding PHP code in an HTML file since XML is widely accepted as
the next and convenient successor of HTML dues to ability to define your own tags. The desired PHP
statements are enclosed in PHP tags as given in example.
<HTML>
<?PHP
?>
</HTML>
This code is saved as first.php file in WAMP/bin folder. In the browser the file is executed by typing
“\\localhost\first.php”. The message “This is my first PHP page” will be displayed on the screen.
Another way to write PHP scripts in HTML is to use the short tag that do not require writing “php”
<HTML>
</HTML>
This is another version of the PHP file written in earlier example works perfectly with short tag. The short
tag style of writing the PHP scripts in HTML is very convenient. It also improves the readability of code.
This flexibility allows you to dynamically generate the title of your webpage by using the variable getting
value after a user interaction as shown in the below example
<html><head><title><?echo $dynTitle?></title></head></html>
In this example the PHP variable is assigned value and the user will see the contents of this variable as
title of the displayed page.
You are aware that the SCRIPT tag in HTML can be used for embedding any kind of script like JavaScript
or VBScript. The same tag can also be used to embed the PHP scripts as well. Developers prefer this style
when they need to create dynamic web pages with portability of various platforms.
<HTML>
<script language="php" > echo " This is my first PHP page ";</script>
</HTML>
ASP style
The short tag style and the XML tag style is not the accepted HTML code. Some HTML editors may fail to
parse it to present for color highlighting and context-sensitive help. If you are comfortable in editing your
web page codes on these modern day editors than it will be better to embed your PHP codes in
ASP(Active Server pages) style since most of these editors recognise it and parse it correctly for
presentation
The ASP-style embedding is the same as the short tag style where you have to use % in place of ?.
<HTML><% echo "This is my first PHP page "; %></HTML>