Está en la página 1de 5

Introduction to Server-Side Programming

A. What is PHP?
Rasmus Lerdorf released the first version of PHP in 1995. In a little over a decade since, it has progressed
through five versions (the sixth is in development) and has become one of the most popular scripting
languages on the web.
PHP is an embeddable, server-side scripting language. What, exactly, does that mean, and how does it
make PHP different from HTML or JavaScript? Let's examine that:
1. PHP is embeddable
This simply means that PHP can be mixed in with your HTML. Opening and closing tags enclose the PHP
script and identify it as something different from HTML. We'll show how this is done in a minute.
2. PHP is a server-side language
When you surf the web, there is an invisible electronic conversation going on between your browser and
the web sites you visit. The two "speakers" in this conversation are your browser, referred to as the
client, and the computer which hosts the web site you're visiting, referred to as the web server.
The conversation takes the form of requests from the client (your browser) and responses from the web
server. These are called HTTP requests and responses. HTTP stands for "hypertext transfer protocol." All
of this is just a technical way of saying that your browser and the web server follow a strict, standardized
method of communicating. (Refer to introductory notes)
When we say PHP is a server-side language, we mean that all of its instructions are carried out on the
web server, before the page is served to your browser. By contrast, JavaScript is a client-side language.
All of the work of a JavaScript is carried out by your browser, after the HTML is served to it.
a. PHP vs. HTML
A fast food restaurant provides a good analogy for the difference between static HTML and PHP.
HTML pages are made up ahead of time and stored on the server. When the customer (client, your
browser) requests an HTML page all the server has to do is grab a pre-made page and hand it over.
By contrast, PHP page unlike the HTML, isn't made up until the customer orders it. The server is notified
of the special order and serves it to the customer.

b. PHP vs. JavaScript


Javascript is a client side scripting language that is used to add functionality to the html pages.

c. What are the implications of using a server-side language?


You can't use your browser' to view feature of PHP source code. PHP is executed on the server. The PHP
script creates HTML output, which is then sent to the browser.
You can't use PHP to manipulate browser or HTML objects. That's what JavaScript does. Although PHP
can generate both HTML and JavaScript, it can't directly affect the DOM
PHP is independent of the browser. This means that you don't have to rely on the user's browser to be
sure your script functions. You don't have to worry about PHP being disabled or about whether the
browser will support a particular PHP capability. (Of course, the user's browser does affect the display of
any HTML output, the same way it would any plain HTML page.)
Server-side languages pose security issues that client-side languages don't, because PHP's growing in
popularity and attracts crackers.

Creating your first php page.


1. Opening and closing tags
All elements within an HTML document are enclosed in identifying tags. Javascripts begin with <SCRIPT>
and end with </SCRIPT>, comments begin and end with <!-- and -->, respectively. PHP uses opening and
closing tags, too.
There are several recognized tag styles for enclosing PHP, but in this class we will use XML style: <?php
(to open) and ?> (to close). There are three very good reasons for this:

XML style assures better compatibility.


Given the general trend toward XML, your code will last longer without changes.
XML is the most widely used style.

To embed PHP code in an HTML document, simply include the code within the opening and closing tags,
like this:

This example illustrates PHP used inline. That means that it is buried within the HTML. The opening and
closing tags mark where we enter or exit PHP mode.
2. Identifying a document that contains PHP with the .php extension
The opening and closing tags identify PHP code within the document, but the server must have some
way of knowing which documents contain PHP code. In technical terms, examining a document for
programming code and executing it is called parsing the document.
Web servers don't generally parse pages with the .htm or .html extension. They just send them,
unexamined, to the browser. A server can be configured to parse every document, but it slows the
server; instead, most are configured to parse documents with particular extensions, in this case, the
.php extension.

3. A short example script


Now that you know how to tag PHP within a document and how to identify that document for parsing,
it's time to put that information to use to create your first PHP script.
Open a new HTML document in your text editor and add the code shown in listing 1.1.
Listing 1.1: Display PHP Configuration (phpinfo.php)
<?php phpinfo(); ?>

That's all! Save the document as phpinfo.php.


Explanation of the Code
The <?php and ?> opening and closing tags
The <?php and ?> tags mark the beginning and ending of the embedded PHP code. The tags tell the
server to treat everything in between these two tags as executable code.

The phpinfo() function


phpinfo() function is a built-in function. We'll discuss built-in functions later in this lesson. For now, what
you need to know is that it prints your server's PHP configuration as a properly formatted HTML page.
These settings are stored in a file called php.ini.
The semi-colon (;) termination character
The semi-colon is used to terminate a PHP statement. It functions like a period at the end of a sentence.
It tells the PHP engine that it has reached the end of a command.

C. Examining your PHP configuration


The output of this simple script is a nicely formatted version of the information stored in your server's
PHP configuration file; it also includes some of the server's environmental settings. This printout is full of
useful information. Be sure to bookmark it or print it and take some time to review it later. For now, let's
concentrate on two important settings that will be critical to this class.
In order to help programmers test and debug their scripts, PHP can provide error messages when it runs
into a problem. Whether or not and to what extent it does are controlled by two settings. Look down
your display or printout until you find the following settings and note their current values.
display_errors
The display_errors setting controls whether or not errors are sent to your monitor.
error_reporting
The error_reporting setting determines the level (degree of risk) of the reported errors. This setting only
works if the first one is turned on. If display_errors is turned off, the error_reporting setting is
completely immaterial because no errors will be displayed.
In a live environment, we suppress error messages for security reasons. In fact, some web hosts turn off
display_errors by default. For learning and development, however, tight error reporting is necessary, so
we will use some code to control these settings.
Some configuration options (not all) can be changed at the script level. Such options are referred to as
run-time settable. Both display_errors and error_reporting are run-time settable. To be sure you receive
all of your error messages (regardless of the server setting), we will insert the following code snippet at
the beginning of all assignment scripts.
Listing 1.2: Error reporting settings for class.

Explanation of the Code


Single-line comment
The double forward slash (//) is used to indicate a single-line comment. We will discuss commenting in
greater detail in a minute. For now, it is important for you to know that this line is ignored by the parser.
The ini_set() function
The ini_set() function is another built-in function. As you may have already guessed, it is used to control
run-time settable configuration options. It requires two pieces of information in order to work: the
name of the configuration element to be set ('display_errors') and the desired setting. In this case, the
setting is either '0' (off) or '1' (on).
The error_reporting() function
You may be wondering why we didn't use ini_set() to change this setting, too. PHP has a special function
called error_reporting() for controlling it. It requires only one piece of information: one of a set of predefined constants (listed in the online manual) that set the risk reporting level. The code above (E_ALL)
turns on all errors and warning notices.

También podría gustarte