What is PHP?
PHP stands for "PHP: Hypertext Preprocessor". It is a widely-used Open
Source general-purpose scripting language that is especially suited for
Web development and can be embedded into HTML. PHP allows you to create dynamic,
interactive Web-based applications such as customized web pages, "shopping
carts", session tracking with "cookies", database management,
and other interactive Web-based applications which communicate information between
the client-side (your computer and browser) and the server-side (your Web server).
PHP is a programming language, somewhat similar in its structure and appearance
to JavaScript, C++, and Perl. Both JavaScript and PHP instructions can be embedded
within the HTML instructions. However, PHP differs from JavaScript in some significant
ways:
- Client-side JavaScript is passed down to the client's PC as part of
the Web page, along with the HTML. PHP is processed by the Web server, and
is not passed down to the client's PC.
- Client-side JavaScript executes (runs) within the viewer's computer,
and has no interaction with the server-side. PHP is executed within the Web
server.
- Client-side JavaScript responds directly to events occurring in the client's
PC (most often initiated by the viewer of the web page), while PHP has no
direct interaction with client-side events. PHP can process information provided
to it via "variables" (named memory locations which hold data.)
This is often done by entering information into named text fields within forms.
When the user clicks the submit button, the information entered into a named
text field is supplied to the program specified in the <FORM> tag's
"action=" parameter, via a "variable" with the same name
as that used to name the text field. This is an established protocol for transferring
information to an awaiting CGI script. The variables (and their data) are
actually made available to the awaiting PHP instructions through a sort of
teamwork effort between the client's browser and the Web server. (*****
decide if the "GET" and "POST" methods should be discussed
here.*****)
- Like JavaScript, PHP instructions are placed into the Web page file.
A Web page file containing PHP instructions should have its extension changed
from ".htm" to ".php", in order for the PHP instructions
to be detected by the Web server software, and then executed by a PHP "interpreter"
within the server. Unlike JavaScript, the PHP instructions are not passed
down to the client's PC; they are, in fact, removed from the file after being
executed. Any HTML tags and text produced by the PHP instructions become part
of the HTML file, which is then passed down to the client's PC. In this way,
PHP can be used to create HTML on-the-fly, based upon information provided
by the viewer; producing dynamic, customized Web pages! The only indication
that Web page downloaded to the viewer contained PHP instructions, is the
file name visible in the location space at the top of the browser window.
Web pages which contained PHP instructions most often will have a ".php"
extension at the end of the file name, instead of the customary ".htm"
extension.
PHP also differs from C++ and Perl in one significant way, in that PHP instructions
are generally placed in the Web page file along with the HTML tags. In other
words, the PHP is embedded within the web page. In contrast, programs
written in C++ or Perl, are completely separate program files residing on the
server.
Any program (also known as a script), which executes within the Web server
and responds to and processes input provided by the viewer, is broadly referred
to as a CGI (Common Gateway Interface) script. So, PHP, C++, and Perl are used
to accomplish the same sorts of tasks.
Because PHP executes completely within the Web server, there are several
important considerations:
- PHP must be installed software on your Web server (by your Web server
administrator)
- PHP is not installed onto your PC or Mac. The type or version of browser
you use is not an issue.
- As mentioned above, generally, only the output of a PHP program is provided
to the viewer (for example, a customized web page). The PHP instructions are
not downloaded to viewer's browser.
- As mentioned above, PHP programs can be started by actions taken within
a Web page. For example, clicking on the Submit button after completing a
form can start up a PHP program which will evaluate and validate the information
provided by the viewer. The PHP program can be programmed to redisplay the
form if information is missing or inaccurate, or process a properly completed
form.
- Through the use of "cookies", PHP programs can track specific
viewer information within sessions and from one session to another. Cookies
are data stored on the viewer's computer; usually indicating choices or preferences
asserted by the viewer while visiting Web pages.
- Because PHP can read, write and create files on the Web server, great
care must be exercised
To find out if you have PHP on your Web server, try this
simple PHP program with these steps.
- Click on the link above to display the file. (It has been named with
a .txt extension so as not to launch automatically when you click on the link.)
- Download the file to your computer by clicking on "File", and
"Save As...". Note: Change the file extension from .txt to .php
then click on the "Save" button.
- Verify that the saved file is now named "doihavephp.php", and
then upload it to your Web account directory as you would any web page.
- Try to open the file via your favorite browser. If you see "Congratulations!
You have PHP.", you're in business! Consult your Web server administrator
for specific details.
As mentioned above, PHP is a natural-text, computer programming language
that can be placed directly into the HTML file. It is an interpreted
language; which means it is executed automatically by the built-in PHP interpreter
within the Web server. It does not end up as an additional file to be downloaded,
as does a Java applet.
What is PHP made of?
PHP is a full-featured programming language which can can manipulate both
simple "scalar"(single-value) or object-oriented data structures.
An object is an entity (eg. a database, a record, a field) that can be
treated as an individual unit or component. Relating this to Web pages, entities
within the Web page can be accessed, read, and have their values determined
by PHP instructions. Entities within a Web page which can be named (or are named
implicitly), are accessible by PHP.
The major Web page entities accessible by PHP include:
- window/frame - represents a browser's window or an individual frame within
a window
- document - represents the content of a browser's window
- form - represents forms created with <form> ... </form>
- image - represents images created with <img>
- date - is used to access the system time and date
- location - allows you to switch to a new Web page
- history - keeps track of the Web pages visited
- navigator - access information about the current Web browser
- array - ordered set of values associated with a single variable name
- string - represents a set of characters, phrase, sentence, etc. (eg.
"PHP is amazing!")
Objects have attributes (also called "properties"), which are
attached to the object with a "->" construct (eg. room->length
and $room->$width).
In addition to attributes, PHP objects may also have "functions" (a sequence
of programming instructions producing something specific) associated with them.
For example, if a room is defined as an object, with attributes "$length"
and "$width". The floor space area of the room could be a function
using length and width as parameters. The floor space function could be written
as
function floor_space($length,$width) {
$this->floorspace = $length * $width;}
and calculated for a specific room instance with the statement
$my_room->floor_space(10,12);
and printed in the Web page with
print("<p>",$my_room->floorspace,"/p");
References:
PHP 4Bible, Tim Converse and Joyce Park, Hungry Minds, Inc.
PHP Professional Projects, Ashish Wilfred, Meeta Gupta, and Kartik Bhatnager,
Premier Press.
Return to the PHP examples.